Well, the second sentence doesn't make any sense. It's certainly incorrect.
The "having failed twice" phrase is called a participle clause, and it usually refers to the actions of the subject.
"Having failed twice, he doesn't want to take the driving test again," has the same meaning as "He's failed twice, so he doesn't want to take the driving test again," or "He doesn't want to take the driving test again because he's failed twice."
As you see, it's much more economical to make one phrase a participle clause ("having failed twice") and add it to the other sentence.
"To have failed twice" doesn't make any sense because that phrase imagines the situation of having failed twice, meaning the failures only exist in theory.