I don't think GCC is doing something special, rather that -O3 might be optimizing out the subtraction. Try with -O0, I suspect you will get the same result as clang.
I tried, but frankly, typing "pointer provenance" into Google gets you a much better explanation than my attempt. Google's AI analysis might be questionable, but it's less questionable than my first attempt.
To oversimplify, provenance is the metadata a compiler keeps about a pointer during the compilation process.
> First, does this C program execute undefined behavior? It does not! Computing out-of-bounds pointers is UB but we are free to do whatever we like with integer-typed values that were derived from pointers. Second, does the C standard mandate the behavior that we saw here? It does not!
Behaviour that is not defined is undefined behaviour. Integer to pointer conversion is IDB, which is the same as UB except that a conforming compiler documents what could happen. I believe it's typically documented as unpredictable, so the same as plain UB.
There is a vast difference between IDB and UB. IDB is perfectly fine to rely on for your compiler, but it means your program may not be portable to other compilers.
UB means your program is not a C program and has no semantics whatsoever past the point it reaches the UB. The compiler is thus free to assume that the point that the UB would happen can't be reached and optimize in accordance to that assumption - which many modern compilers do, with often unexpected consequences (such as the infamous case of removing an integer overflow check in Linux because an_int+1>an_int is guaranteed to be true in the C abstract model).
call __printf_chk@PLT mov ecx, DWORD PTR 0[rbp] mov rdx, rbp mov edi, 2 mov DWORD PTR -96[rbx], 7
To oversimplify, provenance is the metadata a compiler keeps about a pointer during the compilation process.
Yes, I'm a C programmer, how did you know?
Behaviour that is not defined is undefined behaviour. Integer to pointer conversion is IDB, which is the same as UB except that a conforming compiler documents what could happen. I believe it's typically documented as unpredictable, so the same as plain UB.
UB means your program is not a C program and has no semantics whatsoever past the point it reaches the UB. The compiler is thus free to assume that the point that the UB would happen can't be reached and optimize in accordance to that assumption - which many modern compilers do, with often unexpected consequences (such as the infamous case of removing an integer overflow check in Linux because an_int+1>an_int is guaranteed to be true in the C abstract model).