https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124658

            Bug ID: 124658
           Summary: Explicit register variable bound to eax is corrupted
                    by function return values under -O2 optimization
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 522024330006 at smail dot nju.edu.cn
  Target Milestone: ---

To do the quick verification:  https://godbolt.org/z/rnv5GWsGY

The program:
```c
#include <stdio.h>
#include <string.h>

int a[256]; 
unsigned b = 4294967295;

void func(char *f) { 
    for (int e = 0; e < 2; e++)
        b = b ^ a[(f[e]) & 255];
}

char f[56] = "I";

int main() {
    for (int c = 0; c < 256; c++) {
        a[c] = c;
    }

    register int g __asm__("eax") = 5; 
    g++;

    func(f);
    sprintf(f, "%d\n", g);
    func(f);

    printf("%d\n", b);
    return 0;
}
```
Steps to reproduce:
Compile with GCC: gcc -O0 test.c -o test_O0 && ./test_O0
Compile with GCC: gcc -O2 test.c -o test_O2 && ./test_O2
Compare the output.

Both -O0 and -O2 should have produced the same output, as g is explicitly bound
to a register and its increment should be preserved across the call to func.But
the result of -O2 significantly differs from the -O0.

Reply via email to