<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/143379>143379</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [X86_64] Incorrect value of local variable after longjmp with optimizatio
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          hutuhutong
      </td>
    </tr>
</table>

<pre>
    Summary

When compiling the following C code with optimization level -O1, the local variable x exhibits unexpected behavior. In principle, under optimization levels -O1, -O2, and -O3, Clang should treat the allocation and modification of the variable x in a consistent manner. However, when the statement printf("Address of x: %p\n", (void*)&x); is included, a different result is observed at -O1, indicating inconsistent optimization behavior. This suggests that Clang handles the optimization of the printf call involving the address of x in a non-uniform way.



Details

========test.c========
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>


void test_gamma() {
    int x=0;
    jmp_buf env;
    if (setjmp(env)) {
 printf("Jumped here\n");
    } else {
        x = 42;
 longjmp(env, 1);
    }
    printf("Value of x: %d   Address of x: %p\n", x, (void*)&x);
}

void test_gamma1() {
    int x=0;
    jmp_buf env;
 if (setjmp(env)) {
        printf("Jumped here\n");
    } else {
 x = 42;
        longjmp(env, 1);
    }
    int a = x*1;
 printf("Value of x: %d %d\n", x, a);
}

int main() {
 test_gamma();
    test_gamma1();
    return 0;
}

========output========
$ clang -O0 test.c -o test
$ ./test
Jumped here
Value of x: 42   Address of x: 0x7ffc7979655c
Jumped here
Value of x: 42 42

$ clang -O1 test.c -o test
$ ./test
Jumped here
Value of x: 42 Address of x: 0x7ffd6dc09afc
Jumped here
Value of x: 0 0

$ clang -O2 test.c -o test
$ ./test
Jumped here
Value of x: 0   Address of x: 0x7ffceb8ec25c
Jumped here
Value of x: 0 0

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVk2P8jYQ_jXmMgIlTgLkkAMLR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