Andy Murray survived a major scare to progress to the quarter-finals of the Mutua Madrid Open with a 2-6 6-4 7-6 (8-6) victory over Gilles Simon.