https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113271
Bug ID: 113271 Summary: [Regression] Wrong code at -O1/2/3 since GCC-9 (could be due to wrong optimization) Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: 652023330028 at smail dot nju.edu.cn Target Milestone: --- Hello, we note that gcc may introduce erroneous code due to optimization: https://godbolt.org/z/8vbn3KdqP #include <iostream> int a, b, c; void func() { a=-b; //std::cout<<-a<<std::endl; //Line 5 c=(a)/(-2147483647-1); } int main(){ b= -2147483647 - 1; func(); std::cout<<c; } The output of GCC-O0 is: 1 The output of GCC-O3 is: 0 If we uncomment the fifth line: #include <iostream> int a, b, c; void func() { a=-b; std::cout<<-a<<std::endl; //Line 5 c=(a)/(-2147483647-1); } int main(){ b= -2147483647 - 1; func(); std::cout<<c; } we get the correct output (GCC-O3): -2147483648 1 This issue is probably due to a bad optimization of "func" by gcc -O3: func(): mov DWORD PTR c[rip], 0 mov eax, DWORD PTR b[rip] neg eax mov DWORD PTR a[rip], eax ret Thank you very much for your time and effort! We look forward to hearing from you.