https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105598
Bug ID: 105598 Summary: Flag -O2 causes code to misbehave Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: greenfoo at u92 dot eu Target Milestone: --- Created attachment 52974 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52974&action=edit Small example that triggers the issue when using -O2 in gcc 11.x I found a situation where the "-O2" flag was causing trouble at least in x86_64. The attached file "example.c" code is expected to print "4" when compiled and executed, but it would print "2" when compiled with "-02". This happens with gcc 11.1, 11.2 and 11.3, but works correctly with gcc 10.3 and 12.1: > $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:10.3 gcc -o > example example.c && ./example > 4 > $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:11.1 gcc -o > example example.c && ./example > 2 > $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:11.2 gcc -o > example example.c && ./example > 2 > $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:11.3 gcc -o > example example.c && ./example > 2 > $ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp gcc:12.1 gcc -o > example example.c && ./example > 4 I don't know if this deserves further investigation. If not, please feel free to discard this bug report. Thanks!