https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111870
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> --- Or maybe the issue is you don't understand the cmpxchg instruction and how it gives back the original value too. The RTL form for the "lock;cmpxchg " is: (insn:TI 14 10 17 5 (parallel [ (set (reg:DI 0 ax [108]) (unspec_volatile:DI [ (mem/v:DI (reg/v/f:DI 5 di [orig:105 map ] [105]) [-1 S8 A64]) (reg:DI 0 ax [108]) (reg:DI 4 si [107]) (const_int 32773 [0x8005]) ] UNSPECV_CMPXCHG)) (set (mem/v:DI (reg/v/f:DI 5 di [orig:105 map ] [105]) [-1 S8 A64]) (unspec_volatile:DI [ (const_int 0 [0]) ] UNSPECV_CMPXCHG)) (set (reg:CCZ 17 flags) (unspec_volatile:CCZ [ (const_int 0 [0]) ] UNSPECV_CMPXCHG)) ]) "/app/example.c":15:22 9336 {atomic_compare_and_swapdi_1} (expr_list:REG_DEAD (reg:DI 4 si [107]) (nil)))