[bug][clang] with optimization -O1 will get wrong result

clang version: 15.0.4 aarch64

#include <stdio.h>

unsigned long xL0; // 8 byte
unsigned long xL1; 

int main() {
	xL0 = 0x2;
	xL1 = 0x2;
	xL0 >>= (xL1 + 0xffffffff);
	printf("%lx\n",xL0); // 1
}

result

clang

-O0: 1
-O1: 491e88
and with different verion of clang, the result of -O1 will be different

gcc

-O0, -O1: 1

This code has undefined behaviour and therefore has no correct result to begin with:

/app/example.cpp:9:6: runtime error: shift exponent 4294967297 is too large for 64-bit type ‘long unsigned int’

截图
but I can get result

You should never rely on Undefined Behavior. It can break between versions of the compiler or behave very differently between different compilers.

thank you!!

If it is “undefined”, any result, as well as no result, is correct because you do not have a defined result. You might have an “expected” result, but that is not guaranteed by the standard.