Exactly why should it be able to optimize the code? You’re assuming that any transformation that works will be done. That’s not at all how optimizers work. They’re not Artificial Intelligences. They simply work by parametrically replacing known patterns. E.g. the “Common Subexpression Elimination” scans an expression for common subexpressions, and moves them forwards, if that does not change side effects.
(BTW, CSE shows that optimizers are already quite aware of what code movement is allowed in the possible presence of side effects. They know that you have to be careful with &&. Whether expr && expr can be CSE-optimized or not depends on the side effects of expr.)
So, in summary: which pattern do you think applies here?