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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] -O3 don't generate branch instruction
        </td>
    </tr>

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

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

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

<pre>
    
Using -O3 optimization removes branch instruction and program gets segfault

```
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/14.2.1
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
```

Problem occurs in ffmpeg project

Expand sections to see details about compilation
<details>
  <summary>FFmpeg version/configure</summary>
  
  

  git clone https://git.ffmpeg.org/ffmpeg.git --branch n4.4-dev --single-branch --depth=1
  
```
./ffmpeg/configure 
--prefix=. 
--enable-shared 
--disable-static 
--enable-pic 
--disable-asm 
--cc=clang 
--cxx=clang++ 
--ld=clang++ 
--disable-stripping 
--disable-inline-asm 
--enable-version3 
--enable-swscale 
--enable-v4l2-m2m 
--disable-outdev=fbdev 
--disable-indev=fbdev 
--disable-openssl 
--disable-xmm-clobber-test 
--disable-lto 
--enable-debug=3 
--disable-programs 
--disable-postproc
--disable-doc 
--disable-htmlpages 
--disable-manpages 
--disable-podpages 
--disable-txtpages 
--disable-sndio
--disable-schannel 
--disable-securetransport 
--disable-xlib 
--disable-cuda 
--disable-cuvid 
--disable-nvenc 
--disable-vaapi 
--disable-vdpau 
--disable-videotoolbox 
--disable-audiotoolbox 
--disable-appkit 
--disable-alsa 
--disable-cuda 
--disable-cuvid 
--disable-nvenc 
--disable-vaapi 
--disable-vdpau 
--disable-sdl2
```
</details>

<details>
 <summary>Compile options stored in ffbuild/config.mak - O3 optimization</summary>
 
```
CPPFLAGS= -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -DPIC -DZLIB_CONST

CFLAGS=   -std=c11 -fomit-frame-pointer -fPIC -pthread      -g3 -Wdeclaration-after-statement -Wall -Wdisabled-optimization -Wpointer-arith -Wredundant-decls -Wwrite-strings -Wtype-limits -Wundef -Wmissing-prototypes -Wno-pointer-to-int-cast -Wstrict-prototypes -Wempty-body -Wno-parentheses -Wno-switch -Wno-format-zero-length -Wno-pointer-sign -Wno-unused-const-variable -Wno-bool-operation -Wno-char-subscripts -O3 -fno-math-errno -fno-signed-zeros -mstack-alignment=16 -Qunused-arguments -Werror=implicit-function-declaration -Werror=missing-prototypes -Werror=return-type

CXXFLAGS=  -D__STDC_CONSTANT_MACROS -std=c++11
```
</details>

Code fragment (ff_seek_frame_binary libavformat/utils.c:2155):
```
        index = av_index_search_timestamp(st, target_ts,
 flags & ~AVSEEK_FLAG_BACKWARD);
        printf("index = %d\n", index);
        printf("st->nb_index_entries = %d\n", st->nb_index_entries);
        //expanded av_assert0(index < st->nb_index_entries);
        do {
            if (!(index < st->nb_index_entries)) {
                av_log(NULL, AV_LOG_PANIC, "Assertion %s failed at %s:%d\n",
 AV_STRINGIFY(index < st->nb_index_entries), __FILE__, __LINE__);
 abort();
            }
        } while (0);

        if (index >= 0)
            printf("branch inside\n");
            e = &st->index_entries[index]; // SEGFAULT
            av_assert1(e->timestamp >= target_ts);
            pos_max = e->pos;
            ts_max = e->timestamp;
            pos_limit = pos_max - e->min_distance;
 av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
                                    " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
 }
```

Index is local variable int type

Code output:
```
index = -1
st->nb_index_entries = 1
branch inside
```

Expected two branch instruction - for `index < st->nb_index_entries` and `index >= 0` but got only one:

![image](https://github.com/user-attachments/assets/cefedc9e-a0f5-427d-99a4-df2442a4889c)

`jge` to `ff_seek_frame_binary` is assert code. This part in disassembler:

![image](https://github.com/user-attachments/assets/58f96296-6426-46f5-9408-1caba3f4ba77)

Behavior with -O0 (manually changed in config.mak, another options same):

Code otput:
```
index = -1
st->nb_index_entries = 1
```
Asm:

![image](https://github.com/user-attachments/assets/d95bcdeb-10b5-4e73-bae0-e29a180f73f7)

![image](https://github.com/user-attachments/assets/c78d8e73-79ab-4747-affe-38ed8baec16f)


Compiler with -O3 optimized out needed branch =)






</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WFtz6ygS_jXkhcIlI1_khzw4vpxKTTbJJjmTs_uiQqIlMweBCpDjzMP-9i2QfInlnJmpnbOulGO6m77x0TQwa0WpAK7R-AaNl1escRttrt-0LgoBV5nm79coWqJo_tUKVWLyEGNdO1GJ35kTWmEDld6CxZlhKt9goawzTR5YTHFcG10aVuESnMUWyoI10rUKu-9J1P2FYS6ZKvEWjPUahslgOEhazgszJTgUz_EumaSTEalzIoVqdqRUTSeyMcA4rjQH6QVrbcWuZd0q65iUwJfCeBai68YaRNeZUK3EWjeK45wpLjhzgL8sFiEaJmWItDeLrgcDRNdSZIiuyzxHdH3JM7oejgZ0MPyfrUxGf8HOM0jIHfCfZmBxCKFqpBM-C_EcD1B8g0ZRNRn9SCqmnVhMz7z9ka4zpLTfj0ZnEiqs87wxFguFi6KqofTA-w3yD1Bb7WoPSQsBnhY7jS0A5uCYkBazTDcO57qqRZeqdm686CRQvGpJGKN4YZuqYuYdxav1OljsQIvoOteqEGVjAMULRNdHycP0D__3o1I4nEutAG-cqy2K54iu_ZIIN2ijGmhTIrruBl6ekG7jqdFgRDhsMSF-o0rYMwjhULsNipfDM7Mf8zk4KD6NoJMlpDZQiB2Kl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