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

    <tr>
        <th>Summary</th>
        <td>
            clang13: Produces broken code for cmocka test with -O2
        </td>
    </tr>

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

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

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

<pre>
    clang version 13.0.1

Code:
https://gitlab.com/cmocka/cmocka

`cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DPICKY_DEVELOPER=ON -DUNIT_TESTING=ON -DCMAKE_C_COMPILER=clang ..`

tests/test_exception_handler.c

Code snippet:
```c
#define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }

int main(void) {
    const struct CMUnitTest exception_tests[] = {
        cmocka_unit_test(test_segfault_recovery1),
        cmocka_unit_test(test_segfault_recovery2),
        cmocka_unit_test(test_segfault_recovery3),
    };

    return cmocka_run_group_tests(exception_tests, NULL, NULL);
}
```

The preprocessed code with clang looks correct:

```c
int main(void) {
    const struct CMUnitTest exception_tests[] = {
        { "test_segfault_recovery1", test_segfault_recovery1, ((void*)0), ((void*)0), ((void*)0) },
        { "test_segfault_recovery2", test_segfault_recovery2, ((void*)0), ((void*)0), ((void*)0) },
        { "test_segfault_recovery3", test_segfault_recovery3, ((void*)0), ((void*)0), ((void*)0) },
    };

    return _cmocka_run_group_tests("exception_tests", exception_tests, sizeof(exception_tests) / sizeof((exception_tests)[0]), ((void*)0), ((void*)0));
}
```


Correctly compiled code with clang (-g -O0):

```gdb
$ gdb ./tests/test_exception_handler
(gdb) b main
...
(gdb) p exception_tests
$1 = {[0] = {
    name = 0x408004 "test_segfault_recovery1",
    test_func = 0x401363 <test_segfault_recovery1>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }, [1] = {
    name = 0x40801c "test_segfault_recovery2",
    test_func = 0x40137e <test_segfault_recovery2>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }, [2] = {
    name = 0x408034 "test_segfault_recovery3",
    test_func = 0x401399 <test_segfault_recovery3>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }}
```

Code with clang (-g -O2):
```gdb
$ gdb ./tests/test_exception_handler
(gdb) b main
...
(gdb) p exception_tests
$1 = {[0] = {
    name = 0x407004 "test_segfault_recovery1",
    test_func = 0x401400 <_skip>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }, [1] = {
    name = 0x40701c "test_segfault_recovery2",
    test_func = 0x401400 <_skip>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }, [2] = {
    name = 0x407034 "test_segfault_recovery3",
    test_func = 0x401400 <_skip>,
    setup_func = 0x0,
    teardown_func = 0x0,
    initial_state = 0x0
  }}
```

The `test_func` points to a completely different function ...
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVV99z2jgQ_mvMiyYeWTIYP_ghMaTDlCRMj9xNnzz-IYOKsTySnDT9629tYyCACdfMNS3jwZa0Wu23u5-0ikTy4sVZmC_QE5OKixxZ1MSmZeCRga-bf18kzKCbxlLrQlUtcgvPgussjMxYrKERr0W8CncfeyqMAY7X4Yqhq5F_d_15HNw8TqajYP51Njbo6AvL_uF6OWLRJE8FyMwm_uevwWj893j6MBt_AZGHe-h-vJ_Mg_n4r_nk_lPb16jzA__hbjaZ1rINHtOERfdt0ExpBdZV74B9j1mhAW-wDPMkY9KMDyEjlfOiYHoLvdJXP60ooQlLec5QAzgoc66DSr1BhqlBXGQ4Nwik4NtH9d_943R6-Aap0f7aPNdoHfIclDwJnmz0NGMIfrHIlUZKyzLWyL97hEXnsCbaQWqQ9m-M_giBP15Pr1Uc21t7RbFFGpaZDiSLBSTEiwWrg50_OZu8azY9nF35id7su6rqlUyXMm_VyjIPFlKUxcYJZHjolq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