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

    <tr>
        <th>Summary</th>
        <td>
            Missed folding memset(len) to memset(len + 1) of zeros
        </td>
    </tr>

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

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

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

<pre>
    [Godbolt](https://godbolt.org/z/sv4vPq1sf)
[GCC bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120408)

Compiling the C code attachment shows f0_slow() is not optimize like f0() with flags -O3. Could be similar to https://github.com/llvm/llvm-project/issues/139810#issuecomment-2878255401 

```
void f0(unsigned char * restrict arg0, unsigned long len){
 __builtin_memset(arg0, 0, len + 1);
}

void f0_slow(unsigned char * restrict arg0, unsigned long len){
        __builtin_memset(arg0, 0, len);
 arg0[len] = '\0';
}
```
```
define dso_local void @f0(ptr noalias noundef writeonly captures(none) %arg0, i64 noundef %len) local_unnamed_addr {
entry:
  %add = add i64 %len, 1
  tail call void @llvm.memset.p0.i64(ptr align 1 %arg0, i8 0, i64 %add, i1 false)
  ret void
}

declare void @llvm.memset.p0.i64(ptr writeonly captures(none), i8, i64, i1 immarg) #1

define dso_local void @f0_slow(ptr noalias noundef writeonly captures(none) %arg0, i64 noundef %len) local_unnamed_addr {
entry:
  tail call void @llvm.memset.p0.i64(ptr align 1 %arg0, i8 0, i64 %len, i1 false)
 %arrayidx = getelementptr inbounds nuw i8, ptr %arg0, i64 %len
  store i8 0, ptr %arrayidx, align 1
  ret void
}
```
clang -v
```
clang version 21.0.0git (https://github.com/llvm/llvm-project.git 12c62ebcb2008086ad597fffa0f4daf6fe79fe5e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-trunk-20250522/bin
Found candidate GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0
Selected GCC installation: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
 (in-process)
 "/opt/compiler-explorer/clang-trunk-20250522/bin/clang-21" -cc1 -triple x86_64-unknown-linux-gnu -S -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name example.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -fno-verbose-asm -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -v -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21 -internal-isystem /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -mllvm --x86-asm-syntax=intel -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /app/output.s -x c <source>
clang -cc1 version 21.0.0git based upon LLVM 21.0.0git default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/compiler-explorer/clang-trunk-20250522/lib/clang/21/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
Compiler returned: 0
```
</pre>
<img width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