<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/137699>137699</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Misinterpreting debug information when stepping through a loop in a binary compiled with -O1
</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>
This issue arises when debugging into a while loop using step-by-step execution. The debugger incorrectly indicates that line 24 and 26 of the main function is part of the loop body. This behavior can be consistently reproduced with LLVM versions 17.0.6, 18.1.3, and 18.1.8.
Further details are provided below:
```
clang -g -O1 bug_5.c -o 5.out
lldb
(lldb) file 5.out
(lldb) b 14
(lldb) r
(lldb) s
(lldb) s
[...]
* thread #1, name = '5.out', stop reason = step in
frame #0: 0x00005555555551d0 5.out`main at bug_5.c:24
21 return sum > 500 ? 1 : 0;
22 }
23
-> 24 int main(){
25 int arr[] = {12, 9, 8, 15, 7, 18, 20, -1};
26 return f(arr);
27 }
(lldb) s
* thread #1, name = '5.out', stop reason = step in
frame #0: 0x00005555555551d3 5.out`main at bug_5.c:26:12
23
24 int main(){
25 int arr[] = {12, 9, 8, 15, 7, 18, 20, -1};
-> 26 return f(arr);
27 }
(lldb) s
* thread #1, name = '5.out', stop reason = step in
frame #0: 0x00005555555551d3 5.out`main [inlined] f(a=<unavailable>) at bug_5.c:13:28
10 int sum = 0;
11
12 while (i < filter.size && a[i] != filter.terminal_value && sum < filter.threshold) {
-> 13 if ((a[i] % 2 == 0 && a[i] > 10) || (a[i] % 4 == 0 && a[i] > 0 && a[i] < 50)) {
14 sum += a[i] * 2;
15 } else if (a[i] % 3 == 0) {
16 sum += a[i] / 3;
```
`cat bug_5.c`
```
struct Filter {
int size;
int threshold;
int terminal_value;
};
int f(int *a) {
struct Filter filter = {100, 1000, -1};
int i = 0;
int sum = 0;
while (i < filter.size && a[i] != filter.terminal_value && sum < filter.threshold) {
if ((a[i] % 2 == 0 && a[i] > 10) || (a[i] % 4 == 0 && a[i] > 0 && a[i] < 50)) {
sum += a[i] * 2;
} else if (a[i] % 3 == 0) {
sum += a[i] / 3;
}
i++;
}
return sum > 500 ? 1 : 0;
}
int main(){
int arr[] = {12, 9, 8, 15, 7, 18, 20, -1};
return f(arr);
}
```
GDB produces a similar result:
```
gdb
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
...
(gdb) file 5.out
(gdb) b 14
(gdb) r
[...]
(gdb) s
12 while (i < filter.size && a[i] != filter.terminal_value && sum < filter.threshold) {
(gdb) s
26 return f(arr);
(gdb) s
f (a=<optimised out>) at bug_5.c:13
13 if ((a[i] % 2 == 0 && a[i] > 10) || (a[i] % 4 == 0 && a[i] > 0 && a[i] < 50)) {
(gdb)
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMV0uT2ygQ_jX40mUVD7188MEzjnNJNpdkrykkYYktGVyAZjL59VuN5Ic8k2R2K5uNyyUJ6MfXD-hGeq9bo9SaZHck2y7kEDrr1qoZDtaGRWWbp_XHTnvQ3g8KpNNeeXjslIFGVUPbatOCNsGChMdO9wp6a48weJz3QR2X1dMS36C-qHoI2poEPnZq4lYOtKmtc6oO_RNo0-haBuUhdDJAr40CnoI0DfAc7B5Cp-AgtYH9YGoUBtrDUbpwWozaETVq0R4q1ckHbR3U0kCloLbGax-UQXVOHZ1thlo18KhDB-_e_fkeHpTz2hoPrEhokhN-D6xMWCLwC5HEUZkAoRtCN7vBhU45aFSQuvcgnYKjsw-6UQ1UqrePRCAdyen0p5u6l6aFZQvLDwyqof2cJTUsLWSJHcIotu-bCr94Gb_4Cvbo3DPFZb4Cls5n3HzoXxhmd0mSkGwblzYQOqdkA4QLhlYaeVBAxBYIL0aNvMB5H-wRnJLemrgc46oNoRsAgL2LbFxQIjZAv1BKaXb6sYZO4HMaAyjDyXIiNjwdZXAGQOgKpTkVBmfADwcg4g1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