https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104971
Bug ID: 104971 Summary: Optimisation for __builtin_ia32_readeflags corrupts the stack Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: andrew.cooper3 at citrix dot com Target Milestone: --- Full example: https://godbolt.org/z/xGq3c4Mnc Given: int broken(void) { int fl = __builtin_ia32_readeflags_u64(); } gcc -O2 generates: broken: pushfq ret Which is going explode very quickly. Code generation appears to be safe without optimisation, but even -O alone is enough to create problems. At a guess, the optimiser has concluded that the result is unused, drops the `pop %reg`, but fails to also drop the `pushf` too. Looking through history on Godbolt, it appears that GCC 4.9 (which introduced this builtin) has correct optimised code generation, and it regressed between 4.9 and 5.1.