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

    <tr>
        <th>Summary</th>
        <td>
            Global Variable Not Updated Despite Being Included in Return Statement
        </td>
    </tr>

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

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

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

<pre>
    The global variable `counter` in the return statement at line 34 does not update to `9 or any non-zero value` when the binary is compiled with optimization levels "-O0", "-O1", "-O2", and "-O3." Goldbot link is [here.](https://godbolt.org/). Additionally, the same issue occurs with another global variable,  `counter_shorter`.

As a result, the main function's return code remains unaffected, and all compiled binaries return 0 consistently.

We can reproduce this issues in LLVM18.1.8 and 16.0.3.

```
clang -g -O0 -o 435_O0.out 435.c
clang -g -O1 -o 435_O1.out 435.c
clang -g -O2 -o 435_O2.out 435.c
clang -g -O3 -o 435_O3.out 435.c

./435_O0.out
echo $?
0

./435_O1.out
echo $?
0

./435_O2.out
echo $?
0

./435_O3.out
echo $?
0
```

For LLVM17.0.6, it is slight different. The -O0 binary works correctly while the other binaries keep the same issue.
```
clang -g -O0 -o 435_O0.out 435.c
./435_O0.out
echo $?
1

```


`cat 435.c`
```
#include <stdint.h>
union Data {
   uint32_t f0;
 unsigned f1 : 6;
   const uint16_t f2;
   int64_t f3;
   volatile int32_t f4;
};

static int counter = 1;
static unsigned short counter_short = 0;
static union Data data_union = {0xD3D8E1CE};
static union Data data_matrix[8][3] = {
    {{4}, {4}, {0xD757520C}}, {{0x0C01EF4F}, {1}, {0x0C01EF4F}},
    {{4}, {0xD757520C}, {0xD757520C}}, {{0x7DB40437}, {1}, {0x7DB40437}},
    {{4}, {4}, {0xD757520C}}, {{0x0C01EF4F}, {1}, {0x0C01EF4F}},
    {{4}, {0xD757520C}, {0xD757520C}}, {{0x7DB40437}, {1}, {0x7DB40437}}
};

static union Data calculate_data(void) {
    char temp = -1;
    int temp_array[2];
 for (int i = 0; i < 2; i++)
        temp_array[i] = -7;
    for (counter = 0; counter <= 9; counter++) {
        temp_array[1] &= 0x116E074F;
        counter_short++;
        return data_union;
 }
    return data_matrix[6][2];
}

int main(void) {
 calculate_data();
    return counter;
}

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcFtuO2jj0a8zLEZHjXHngYYChWqm7lfbSfRyZ-EC8NTayHWbo16_sQAhMZ9pq3xZFxj53n5sPd07uNOKcFAtSrCa8862xcxTd3hg_2Rhxmv_ZIuyU2XAFR24l3ygEUtLGdNqjJSUFqcG3CBZ9ZzU4zz3uUXvgHpTUCFkOwqADbTx0B8E9gjdBxgyMBa5PoI2efkVr4MhVh0Hmc4u91I3U3J5AOmjM_iAVCniWvgVz8HIvv3IvjQaFR1QOCGPTT5QwRtiyP6TjAzsfuBY9IEsIY_DBKLEx0dYvQQ8pFi1aTEixIqxuvT84kj0QtiZsvTNiY5RPjN1FwCyBByFkMIIrdQrSg9GO7xGkcx2CaZrOut5mro1v0d67M3CNXPrkWmN71yaErgh96NcHBxwsuk75i549lxq2nW6CAYRV7hKExogQkIB30Gm-3WLjUVxuz5W6ujN6WOLAS6Ex2knnUXt1ujHhb4SGa7B4sEZ0DYJvpesv6kIafPz4-de0TtKkjmrSMqFJdiOBlPT8xWOjuN7BdAfTTxSmBvKsePpEE9P5sE2aV1TpQJW-Q8UGKvYOVTZQZfdU_ZoQtr5a1MOwaQ0QlpNs3QPoNznSn-ZgP82R_QDHrbv7dW1sH6kqoUkZckL6kPhOyV3rQcjtFi1qn0Co_RCZcxE-G_sl1KG12Hh1gudWKoyJ2Of1kElfEA93hZD8h_j_UBzSd9LsDtXwi_jyDT-xTOpGdQKBZEvnhdQ-aUn22KM7HZrOinsOpFr0MADopPYZe_KwpSS7gDsdW6yAbQoke4DyioJYaD7ypWXgY2Ok1L7MAzQbQ49GcR_cPijLBzSpVt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