Why do I get "Missing END IF" error at the first END IF in the following code
The unhelpful (but accurate) answer is because the ENDIF is missing! Apart from doing a simple count (there are two multi-line IF statements but only one matching ENDIF statement) it's evident from the indentation that the first ('outer') IF clause is never terminated.
But presumably that's not the answer you wanted since it's stating the obvious, so what is it that you really want to know? Is the question related to the recent Cascaded IF clauses and the semicolon thread?