http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250
Bug #: 56250 Summary: Wrong constant folding on unsigned int Classification: Unclassified Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: ishiura-compi...@ml.kwansei.ac.jp GCC 4.7.2 and 4.8.0 miscompile the following code where UINT_MAX == 4294967295. $ cat bug.c int main(void) { unsigned x = 2; return ((unsigned) 0 - ( x / 2 ) ) / 2; } $ gcc bug.c $ ./a.out $ echo $? 0 Unsigned (0-(x/2))/2 should evaluate to 2147483647 instead of 0. The following code with variable x replaced by "(unsigned) 2" is correctly compiled. $ cat nobug.c int main(void) { return ((unsigned) 0 - ( (unsigned) 2 / 2 ) ) / 2; } $ gcc nobug.c $ ./a.out $ echo $? 255 The miscompile seems to occur on many versions of gcc (4.2 - 4.8, and even 4.0 and 3.1 on i686-apple-darwin, 4.4 - 4.6 on i686-linux-gnu-gcc, etc.)