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

    <tr>
        <th>Summary</th>
        <td>
            Possible miscompilation with volatile in loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    I compiled the following test from the linux test project with clang 14.0.6 (`clang -D_FORTIFY_SOURCE=2 -g -O2 -pthread  signal06.c -o signal06`):
https://gist.github.com/edliaw/a176f4be8455a067bded6fbc9359bd1b

And was running into errors that seemed to suggest that `loop` was being optimized away, despite being evaluated with D (which is tagged volatile) in `while (D == VALUE && loop < LOOPS) {`.  By tagging `volatile int loop = 0`, I was able to get the test to work.

In the disassembly, incrementing `loop` is using `leal` instead of `addl` (in the working case and gcc).  I'm not familiar with how these instructions work, but hoping someone that knows more can help:

* unmodified with clang https://gist.github.com/edliaw/8a01ca56144672791e3150b0ab7dd0e5
* volatile int loop with clang https://gist.github.com/edliaw/6d4f73a58fb42c10fd78c974d0145c10
* unmodified with gcc https://gist.github.com/edliaw/45e0e14ae55225f4b28deef11e37d590
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVNuO3CgQ_Rr7BY2FbfDlwQ890xmppUgd5SblKQJTtkmwaRk8ndmv3wJPJ6NEK0UrIWOqqKpzTgHSqufuRHo7X7QBRfwEZLDG2KteRuLBeTKsdo52o5ftx267rPYb9J5ctZ9IbwTuzVlGs4okRZNUdDfdHb8-nt9_PD1--frh_On9w5ukPBbkDh1nnC5-WkEoQpweF2FolfXkzv5cYZakaJPykNBjQg-T9xcXVsUjjlE7n41YfJMZYkcLKKPFFX9EXlcDk9AwzgWtaqlAVYPs25K3UuVyT7d_D4siV-HIui1L4KsXbwmsq10dMhaeOIA5qIKwtnEMzKMZsRlrLzjFcAkh2F68nvU_uF1cxXNSPBAF7qI9vPjhSZhNePRH1Y5Bquuk-4lorCYwvSJP1giPjUDmCCbUwR0GwlbcXx5xkM-Ht5_eoKXCQQIMdDyQt-fzuw8hLKnvMSwj5P45Zg2l0XDLHDjeoo6ERpUfyCnyEBL9yHUEHxseW43rq12_Z691Oy3Rr7QTzsEsTaSrl35FuRb_UvImEdLb3M0GwkTb4nxovh2CVSgVrUhT76lDyRDSCwdEYJvGvkd2SOuUFPVMFosHU8wam77uek72GiIdxNzr1nttFxcTBXBy87jlEnI6O4NdYG_l98VeHZntClhrIROYy88z9_ItDmRbZqv0oG_N28_3X5_JRtC8F7zKGavqom5zKHNOJRWyVooC_1Xozzb9n3qVYkNdCt4MkhV9TgdVN31bM0VzxnH938RQ5r8vwzhQyJkAzouC46UrGgUw5EivVrylKXR5VdGG13j3UtWVqi1bkXrtDXTvrHM6HLhZu_35EaFjO4xXMkQV0m013e-4XkEy5uk23b28TbjUzm3g8IfXRcnSqSuZbOTQF7KpSkEl8HpQVQNKtW3OJGtSIyQY1yX8PuHHVHcFLQra5E2R55y3GctrlkMLilaUMsUSRmEW2mShcGbXMV27iEFuo0OnQe3cLydeFnzcAG75xeYnu3a7nmlE20Wo_wKQ-MlE">