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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect fold unsigned icmp in glibc/wcsmbs/mbrtowc.c
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Since commit
```
commit ae73bc8e94b3174e2b495eee544cf843b6e51899 
Author: Nikita Popov <npopov@redhat.com> 
Date:   Tue Dec 3 14:28:56 2024 +0100 
 
    Reapply [InstCombine] Support gep nuw in icmp folds (#118472) 
 
    The profile runtime test failure this caused has been addressed in:       
    https://github.com/llvm/llvm-project/pull/118782 
 
    ----- 
 
    Unsigned icmp of gep nuw folds to unsigned icmp of offsets. Unsigned       
    icmp of gep nusw nuw folds to unsigned samesign icmp of offsets.
 
    Proofs: https://alive2.llvm.org/ce/z/VEwQY8 
 
 .../Transforms/InstCombine/InstCombineCompares.cpp | 36 ++++++++--- 
 llvm/test/Transforms/InstCombine/icmp-gep.ll       | 75 ++++++++++++++++++++++ 
 2 files changed, 102 insertions(+), 9 deletions(-)
```

I meet a regression problem when build glibc with clang
In `glibc/wcsmbs/mbrtowc.c`, the follow code
```
  inbuf = (const unsigned char *) s;
  endbuf = inbuf + n;
  if (__glibc_unlikely (endbuf < inbuf))
    {
      endbuf = (const unsigned char *) ~(uintptr_t) 0;
 if (endbuf == inbuf)
        goto ilseq;
    }
```

is folded.


Howto reproduce:
1. git clone glibc and checkout branch `azanella/clang`
2. Build glibc with
```
 CC=/build/llvm-project/build/bin/clang CXX=/build/llvm-project/build/bin/clang++ CFLAGS="-g -O2 -Wno-unused-command-line-argument" CXXFLAGS="-g -O2 -Wno-unused-command-line-argument" ../configure --host=riscv64-linux-gnu --prefix=/usr
make
```
</pre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