With -O2 or higher, the following little C loop does not terminate: short i; for (i=-1; i; i--) ;
The generated assembler code is: .L2: jmp .L2 which means that the exit condition has been completely eliminated. The same loop works as expected (terminates after 0xffff iterations) with gcc-4.0 or earlier, with -O1 or lower, and with unsigned types. This is an i386 style system with a P4 2.8 GHz, running Debian SID/i386 with gcc-4.1 1:4.1.0-0 from Debian experimental. -- Summary: Invalid code: loop optimization removes exit condition Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: steffen dot list dot account at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26566