when one of you is in doubt about his prayer (i.e, how much he has prayed), he should throw away his doubt and base his prayer on what he is sure of. When he is sure about the completion of his prayer, he should make two prostrations (at the end of the prayer). If the prayer is complete, the additional rak’ah and the two prostrations will be supererogatory prayer. If the prayer is incomplete, the additional rak’ahs will compensate it, and the two prostrations will be a disgrace for the devil. (Using translation from Abū Dāʾūd 1024)
"إِذَا شكّ أحدُكُمْ في صَلاتِه فْليُلقِ الشَّكَ، وليَبْنِ على اليقينِ، فإِن استيقَنَ التمام سجدَ سجْدتين، فإِنْ كانتْ صلاتُه تامة كانتْ الركْعةُ نافلة والسجْدتان نافلةً، وإنْ كانت ناقصةً كانتْ الركعةُ تمامَ الصَلاةِ، والسجْدتان تُرْغمان أنْفَ الشَّيطان".