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

    <tr>
        <th>Summary</th>
        <td>
            gcc faster than clang by 1.5x in a branchy loop
        </td>
    </tr>

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

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

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

<pre>
    Sorry for a vague title because i haven't investigated it deep enough. Here is the repro

```cpp
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>

uint32_t median(const uint32_t *nums, uint32_t n)
{
    if (n == 0)
 return 0;

    const uint32_t target1 = (n + 1) / 2;
    const uint32_t target2 = (n + 2) / 2;

    uint32_t last_min = 0;
 uint32_t so_far = 0;

    bool target1_found = false;
    uint32_t target1_value = 0;
    uint32_t target2_value = 0;

    while (so_far < n)
    {
        uint32_t cur_min = UINT32_MAX;
 uint32_t cur_so_far = 0;

        for (uint32_t i = 0; i < n; ++i)
 {
            const uint32_t num = nums[i];
            if ((num > last_min || so_far == 0) && num < cur_min) {
                cur_min = num;
                cur_so_far = 1;
            } else if (num == cur_min) {
                cur_so_far += 1;
            }
 }

        last_min = cur_min;
        so_far += cur_so_far;

 if (!target1_found && so_far >= target1)
        {
 target1_value = cur_min;
            target1_found = true;
 }
        if (so_far >= target2)
        {
            target2_value = cur_min;
            break;
        }
    }

    return (target1_value + target2_value) / 2;
}

int main()
{
 /*{
        const uint32_t v[] = {1, 2, 3, 4, 5};
 printf("median(): %u\n", median(v, 5));
    }

    {
 const uint32_t v[] = {12, 88, 34, 100, 198, 1888181, 0, 0, 0, 111, 13};
 printf("median(): %u\n", median(v, 11));
    }

    {
 const uint32_t v[] = {10, 10, 15, 33, 10, 12};
 printf("median(): %u\n", median(v, 6));
    }

    {
 const uint32_t v[] = {10};
        printf("median(): %u\n", median(v, 1));
    }

    {
        const uint32_t v[] = {0};
 printf("median(): %u\n", median(v, 1));
    }

 printf("median(): %u\n", median(NULL, 0));*/


    uint32_t *vec = (uint32_t*)malloc(100000 * sizeof *vec);
    for (uint32_t i = 0; i < 100000; ++i)
        vec[i] = i;

    struct timespec start;
 clock_gettime(CLOCK_MONOTONIC, &start);

    const uint32_t m = median(vec, 100000);

    struct timespec end;
 clock_gettime(CLOCK_MONOTONIC, &end);

    printf("m = %d, t = %llu\n", m,
            (unsigned long long)(end.tv_sec - start.tv_sec) * 1000000000llu +
                end.tv_nsec - start.tv_nsec);

 return 0;
}
```

$ clang test.c -O2 -g -o a.out && ./a.out
m = 49999, t = 7783469737 // 7.7 seconds

$ gcc test.c -O2 -g -o a.out && a.out
m = 49999, t = 5852896599 // 5.85 seconds
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV9uOozgTfhrnptQICghwkYscJvpHf0_3xc5Iexc54BDvOCbCJru9T7-yORP6MKO2Iiexq-r76uATVYrnkrEVCTck3C1opc9FuTrzkmZcv9DFscheVn8UZfkCp6IECjeaVww014LBkaW0Ugw4nOmNSYKRBi5vTGmeU80y4Boyxq7AZFHlZwf-x0oGXIE-MyjZtSyIuyPuuumXbv1Jr9dmBH0uU1FlDIi_VToT_Oicif_llWlevDkr9VvTx6IQr85rfmHDSdtXXGofDxouLONUEozTQioN3TjBtawuiuC2H5MEk8ZItKl_AADwExCMJRB_R_wduJ0UlExXpQSX-JshuFGawGla5kx7xkZjDDfgEUyA4B6wM_CqKo5V8V61N9CpCqr04cIt9QHLXkAVhxMtJ9O9IRP3lvrhVFQys6InKhQbUZ76ebhRUbEp7L0gzgr24n-fuWDG647odpAlIzHK1AghrcrO9x9fn777ePi2_nMmCEbwnUCYZtYYwbhT4520_WmI-RuTHIIb3nO8IziTYlldrDFbkeGGk3A3ilrb6ko0RWAVvgwSHG1JtB3ks61UILgkuGwgtm1U7MwcM8tuEDlZXWaptHKDwHmzgiTaARNm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