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

    <tr>
        <th>Summary</th>
        <td>
            Misoptimization issues with variables related to unions when compiling with “-g -O3” and “-g   -O2"
        </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>
    While debugging binaries compiled with "-g -O3" and "-g -O2",  dereferencing the global pointer `g_18` at line 51 returned **0**. In contrast, the same operation yielded **-5** when debugging binaries compiled with "-g -O1" and "-g -O0".

Here are the steps to reproduce the issue:
```
clang -g -O3   bug_03.c -o bug_03_O3.out
clang -g -O1   bug_03.c -o bug_03_O1.out
...
(lldb) file bug_03_O3.out
(lldb) b 51
Breakpoint 1: where = bug_03_O3.out`main + 28 at bug_03.c:51:5, address = 0x000000000000114c
(lldb) r
[...]
 thread #1, name = 'bug_03_O3.out', stop reason = breakpoint 1.1
    frame #0: 0x000055555555514c bug_03_O3.out`main at bug_03.c:51:5
   48   int main(void)
   49   {
   50       func_1();
-> 51            return 0;
   52   }
(lldb) p *g_18
(const int) 0
(lldb) p **g_17
(const int) 0


(lldb) file bug_03_O1.out
(lldb) b 51
Breakpoint 1: where = bug_03_O1.out`main + 36 at bug_03.c:51:5, address = 0x0000000000001154
(lldb) r
[...]
* thread #1, name = 'bug_03_O1.out', stop reason = breakpoint 1.1
    frame #0: 0x0000555555555154 bug_03_O1.out`main at bug_03.c:51:5
   48   int main(void)
   49   {
   50       func_1();
-> 51            return 0;
   52   }
(lldb) p *g_18
(const int) -5
(lldb) p **g_17
(const int) -5

```

`cat bug_03.c`
```
union U1 {
   signed f0 : 11;
   unsigned int f1;  
   char f2;          
   volatile int f3;  
};

union U3 {
   unsigned long long f0;  
   char f1;                
};

static union U3 g_8[4] = {{0xF4990069F30CC2FDULL}, {0xF4990069F30CC2FDULL}, {0xF4990069F30CC2FDULL}, {0xF4990069F30CC2FDULL}};
static int g_12[7] = {9, 9, 0, 9, 9, 0, 9};
static const int *g_18 = 0;  
static const int **volatile g_17 = &g_18;
static int g_21 = 0x31A558FF;
static int *volatile g_20 = &g_21;
static union U1 g_22 = {-7};

static union U1 func_1(void);
static int func_2(union U3 p_3, union U3 p_4, const int p_5, unsigned long long p_6, unsigned long long p_7);  

static union U1 func_1(void)

    union U3 l_9 = {-5LL};  
    int *l_19 = &g_12[2];
    (*g_20) = ((*l_19) = func_2(g_8[0], l_9, l_9.f0, g_8[0].f0, l_9.f0));
    return g_22;
}

static int func_2(union U3 p_3, union U3 p_4, const int p_5, unsigned long long p_6, unsigned long long p_7)

    int *l_11[4][7] = {
        {&g_12[2], 0, 0, &g_12[2], 0, 0, &g_12[2]},  // Use 0 instead of NULL
        {0, &g_12[2], 0, 0, &g_12[2], 0, 0},
        {&g_12[2], &g_12[2], &g_12[2], &g_12[2], &g_12[2], &g_12[2], &g_12[2]},
        {&g_12[2], 0, 0, &g_12[2], 0, 0, &g_12[2]}
    };
    char l_13 = 0x5A;
    unsigned short l_14 = 0;
    int i, j;
    l_14--;
    (*g_17) = &p_5;
    return p_3.f0;
}

int main(void)
{
    func_1();
    return 0;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWEuP2zYQ_jX0ZWCDHL0PPtjrGC2QNqegR4OWaJmpLAkUtUn664sR9bJX227SF1BhIS3nxXl8MxItm0bnpVJbFuxZcFjJ1l4rs1VZe6squzpX2dftL1ddKMjUuc1zXeZw1qU0WjWQVrdaFyqDz9pegSGuc1h_8BgiyDIbCcgQGT4BZMqoizKqTMmMvSrIi-osC6grXVplgIU8P4mYhRykhUKXCgIBRtnWlIoM7hjuuHts4McS0qq0RjaWzJO9Rt4UVLUy0uqqhK9aFdmouA7cEz5fVfn2eMRjPJwhbhg_ML5z9x-UUSCNci5YVTdgKzC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