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

    <tr>
        <th>Summary</th>
        <td>
            Elision of write to allocated storage before longjmp
        </td>
    </tr>

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

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

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

<pre>
    ```c
#include <setjmp.h>
#include <stdlib.h>

static int *alloc(void) {
    int *p = malloc(sizeof(int));
    *p = 111;
    return p;
}

static void set_value(jmp_buf env, int *p) {
    *p = 42;
    longjmp(env, 1);
}

int main(int argc, char **argv) {
    int *p = alloc();
    jmp_buf env;
    if (!setjmp(env)) {
        set_value(env, p);
    } else {
        return *p;
    }
    return 5;
}
```

`p` is not changed between `setjmp` and `longjmp`, and `*p` is not of automatic storage duration, so AFAICT `longjmp` shouldn't affect either one.  But:

```
$ clang --version
clang version 14.0.5 (Fedora 14.0.5-5.fc37)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ clang                           -o longjmp longjmp.c && ./longjmp ; echo $?
42
$ clang     -fno-inline-functions -o longjmp longjmp.c && ./longjmp ; echo $?
42
$ clang -O2                       -o longjmp longjmp.c && ./longjmp ; echo $?
111
$ clang -O2 -fno-inline-functions -o longjmp longjmp.c && ./longjmp ; echo $?
42
```

`42` and `malloc` have both been optimized out of the asm:

```asm
Dump of assembler code for function main:
   0x0000000000401140 <+0>:       sub    $0xc8,%rsp
   0x0000000000401147 <+7>:       mov    %rsp,%rdi
   0x000000000040114a <+10>:      call   0x401030 <_setjmp@plt>
   0x000000000040114f <+15>:      test   %eax,%eax
   0x0000000000401151 <+17>:      je     0x401160 <main+32>
   0x0000000000401153 <+19>:      mov    $0x6f,%eax
   0x0000000000401158 <+24>:      add    $0xc8,%rsp
   0x000000000040115f <+31>:      ret    
   0x0000000000401160 <+32>:      mov    %rsp,%rdi
   0x0000000000401163 <+35>:      mov    $0x1,%esi
   0x0000000000401168 <+40>:      call   0x401040 <longjmp@plt>
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VUtvqzgU_jVkY4Ewz2TBIm1upbuazd1XBh_AlYORbdLM_Po5NpCmTVLNjOYiQsDH_s533rXif1ZBEc93E8SHIN4HSSqGRk4cSJA-G7BvxzHqg_THXbHlUtTXYv80llnREDFYEiR7JqVqgmR7UoIHyY4E5dO8jeC17BkR7UCO61Yj_gLV4guK8Yi706tDlwOU0k8CDXbSAxkvi0F5uEPMMSFo2uuJyQlQDdr4Wk8tgeEUJM8XUjdsL4qz5JNeqYYOMRBpQaDXlL-QcOhHJobZPMJ017gjTc-0U-A8prvT955aHfXVM9eGXK-LlvjddA7oSnR3o8Vd155Z7BlvQlAeCEgDt6eXGHj_fTlxE6j8jo_WfLx2GX6O-CPCkEFZ56qhA05qsO8AqKuIF7twDxu4W1hDgkDIf1n0pD5wVEvYZNXRJ4WxSrMOCJ80fqvBHTOK7F_2P59_fUYkpleT5LilxPi1LTSWgLA9aKIGiAh5mmyQ7r9Y8MmsJCONRCtIGJ5AG6fPC-bFZYnQLIqj3IXuBTjSWxbCPGqbtHQx8Yd-YcKA00jO2-K1yEINvGc2lGKYzmE3TMu2XgPj5Kg4SLd5VEacZ9HPAUtDSuAHoZ0oSF4mo_FZi-Er48dXqNZKWP-jBqEKvEmEYKsQo06g6RXKsiB9mRVgSd1RFLaDCsWAlkDYTkPjImP-b0XhH8nvsMh1pzuafqdJD4oH5R-VsXRZXOjZCUitbI-VhGWkRiuO2Hk5UZMvDsxowszxUSo7kV85TEjMFZMxcKwllkGDOUZapclq4NzxViD0bHyOL1cWU5rFbqAEyVPshonbuDNTPTfdLD432LxQmmszPoYoF4jyAnFUpxnCH5wRuHiMwBYE-sGiQXf5rbghTj3L16XbZPEo7WX23cNrV7z8gmfB2JkSsPNMyb08gsjpCvFh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