Correct optimization of if expressions