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

    <tr>
        <th>Summary</th>
        <td>
            Inconsistent debug information of static global pointer, compiled with "-g -O0"
        </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>
    We use more brief source code from > https://github.com/llvm/llvm-project/issues/95512, with different optimization level `-g -O0` to expose more.

Problems: The dereference of `global_pointer` is `3`  on line 16 and it continue to point to `3`  on line 24, but the data that the `golbal_pointer` points to is changed to `0` while the value of the `global_pointer` is still the same  when we step in continuously. This issue leads to a wrong return value `0` (the correct should be `3`).
```
* thread #1, name = 'case_O0.out', stop reason = step in
    frame #0: 0x00005555555551cc case_O0.out`func_1 at case.c:16:13
   13       ptr_global = ptr_local_var;
   14    *ptr_to_global_pointer = &local_var;
   15   
-> 16            return *global_pointer;
   17   }
   18   
   19   
(lldb) fr v
(int *) ptr_global = 0x00007fffffffd864
(int) local_var = 3
(int *) ptr_local_var = 0x00007fffffffd864
(int **) ptr_to_global_pointer = 0x0000555555558040
(lldb) ta v
Global variables for /home/ad/Downloads/lldb/reproduce_bug/case.c in /home/ad/Downloads/lldb/reproduce_bug/case_O0.out:
(int *) global_pointer = 0x00007fffffffd864
(int) global_var = 7
(lldb) p *global_pointer
(int) 3
(lldb) s
Process 37174 stopped
* thread #1, name = 'case_O0.out', stop reason = step in
    frame #0: 0x000055555555515b case_O0.out`main at case.c:23:21
   20   int main(void)
   21   {
   22       int a = 0;
-> 23         printf("%d %d", func_1(), *global_pointer);
   24       a += *global_pointer;
   25       return a;
   26   }
(lldb) ta v
Global variables for /home/ad/Downloads/lldb/reproduce_bug/case.c in /home/ad/Downloads/lldb/reproduce_bug/case_O0.out:
(int *) global_pointer = 0x00007fffffffd864
(int) global_var = 7
(lldb) p *global_pointer
(int) 3
(lldb) s
Process 37174 stopped
* thread #1, name = 'case_O0.out', stop reason = step in
    frame #0: 0x0000555555555174 case_O0.out`main at case.c:24:11
   21   {
   22       int a = 0;
   23       printf("%d %d", func_1(), *global_pointer);
-> 24            a += *global_pointer;
   25    return a;
   26   }
(lldb) ta v
Global variables for /home/ad/Downloads/lldb/reproduce_bug/case.c in /home/ad/Downloads/lldb/reproduce_bug/case_O0.out:
(int *) global_pointer = 0x00007fffffffd864
(int) global_var = 7
(lldb) p *global_pointer
(int) 0
(lldb) s
Process 38123 stopped
* thread #1, name = 'case_O0.out', stop reason = step in
    frame #0: 0x0000555555555183 case_O0.out`main at case.c:25:12
   22       int a = 0;
   23       printf("%d %d", func_1(), *global_pointer);
   24    a += *global_pointer;
-> 25            return a;
   26   }
(lldb) fr v
(int) a = 0
```



Source code: `cat case.c`
```
#include "stdio.h"

static int global_var = 7;
static int *global_pointer = (void*)0;

static int func_1(void)
{
    int *ptr_global = &global_var;
    int local_var = 3;
    int *ptr_local_var = &local_var;
    int **ptr_to_global_pointer = &global_pointer;

    ptr_global = ptr_local_var;
    *ptr_to_global_pointer = &local_var;

    return *global_pointer;
}


int main(void)
{
    int a = 0;
 printf("%d %d", func_1(), *global_pointer);
    a += *global_pointer;
    return a;
}
```
Environment: The problems can be reproduced in LLVM 18.1.2, LLVM17.0.6, and LLVM16.0.3.  In these environments, the variable `a` and the dereference of the static global pointer `global_pointer` compiled with `-g -O0` and `-g -O3` are inconsistent (0 and 21845). However, their debug information one line 25 emitted in LLVM14.0.6 with  `-g -O0` and `-g -O3`  are consistent ( both are 0).

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWFmP27AR_jX0y2AFiTosP_jBycZtgBQp0KB9XFDiyGJBkwZJ2Ul_fUFKsuRjD2wTBChiLLQixRnO8X08hlkrdgpxTfIPJH9csM612qyRd3ut3aLS_Mf6XwidRdhrg1AZgQ1Y3Zka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