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

--- Comment #1 from 신재형 <shinwogud12 at gmail dot com> ---
(In reply to 신재형 from comment #0)
> The provided C code includes a main function and a helper function i(),
> along with a struct definition and several global variables. The code
> primarily involves conditional logic and a loop to manipulate these global
> variables.
> 
> PoC(Proof of Concept)
> 
> #include <stdint.h>
> #include <stdio.h>
> 
> struct a {
>   uint64_t b;
> };
> int c = 1;
> int d, e, h, f, g, l = 0;
> volatile struct a k[1];
> 
> void i() {
>   for (; l < 1; l++)
>     f = d <= 0;
> }
> 
> int main() {
>   for (e = 9; e; --e){
>     i();
>     c && (g = ((int16_t)(k[0], f)) <= e);
>   }
>   printf("g_200 value %d\n", g);
>   return 0;
> }
> 
> 
> Struct Definition
> 
> 
> struct a {
>   uint64_t b;
> };
> 
> - Defines a struct a with a single member b of type
> uint64_t.
> 
> Global Variables
> 
> 
> int c = 1;
> int d, e, h, f, g, l = 0;
> volatile struct a k[1];
> 
> 
> - A loop that runs once, setting **`f`** to 1 if **`d`** is less than or
> equal to 0.
> 
> Main Function
> 
> 
> cCopy code
> int main() {
>   for (e = 9; e; --e){
>     i();
>     c && (g = ((int16_t)(k[0], f)) <= e);
>   }
>   printf("g_200 value %d\n", g);
>   return 0;
> }
>
> 
> 
> - A loop starts with e at 9, decrementing until it reaches 0.
> - Calls function i(), which sets f based on the condition d
> <= 0
> - The expression c && (g = ((int16_t)(k[0], f)) <= e) uses the comma
> operator, resulting in f being evaluated and cast to int16_t.
> Since c is always 1, g is set to 1 if f is less than or
> equal to e.
> - The final value of g is printed.
> 
> ### Expected Result
> 
> - Since d is initialized to 0 and never modified, f will always
> be set to 1 in the i() function.
> - In the main function, g is set to 1 in each iteration of the loop
> because f (which is 1) is always less than or equal to e (which
> decrements from 9 to 1).
> - The final output of the program will be g_200 value 1.
> 
> Why does optimization level 2 in GCC output 0?
> 
> Godbolt Link: https://www.godbolt.org/z/r6c4oo18a

Reply via email to