bool always_true (bool a, bool b) { return (a == b) == (~a ^ b); } is optimized to xorl %eax, %eax ret instead return 1.
I don't think that's true. Note, that with -Wall you get the warning: true.c: In function 'always_true': true.c:3:29: warning: '~' on a boolean expression [-Wbool-operation] 3 | return (a == b) == (~a ^ b); | ^ true.c:3:29: note: did you mean to use logical not? 3 | return (a == b) == (~a ^ b); | ^ |
Yeah, folding it to return false; is the right thing. ~a ^ b is ~(int) (a ^ b) in C++, so if a == b, it is ~0 (on two's complement -1), and if a != b, then it is ~1 (on two's complement -2). 1 != -1 and 0 != -2 so it is always false.
Sorry for false report.
.