#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *memchr(char *, char, int);
void find_and_replace(char *haystack, size_t len, char needle, char replace) {
  char *end = haystack + len;
  while ((haystack = memchr(haystack, needle, end - haystack)) != NULL) {
    *haystack = replace;
    haystack++;
  }
}


x86 assembly (clang, -O0):
<find_and_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %cl,-0x11(%rbp)
mov    %al,-0x12(%rbp)
mov    -0x8(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x11(%rbp),%al
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
mov    %ecx,%edx
movsbl %al,%esi
call   1030 <memchr@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
je     117a <find_and_replace+0x6a>
mov    -0x12(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1136 <find_and_replace+0x26>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_and_replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %edx,%esi
mov    %ebp,%edx
call   1030 <memchr@plt>
test   %rax,%rax
je     115a <find_and_replace+0x4a>
add    %rbp,%rbx
movsbl %r15b,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14b,(%rax)
add    $0x1,%rax
mov    %ebx,%edx
sub    %eax,%edx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <memchr@plt>
test   %rax,%rax
jne    1140 <find_and_replace+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @find_and_replace(i8* %0, i64 %1, i8 signext %2, i8 signext %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8 %2, i8* %7
store i8 %3, i8* %8
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = getelementptr inbounds i8, i8* %10, i64 %11
store i8* %12, i8** %9
br label %13
13: ; preds = %24, %4
%14 = load i8*, i8** %5
%15 = load i8, i8* %7
%16 = load i8*, i8** %9
%17 = load i8*, i8** %5
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = call i8* @memchr(i8* %14, i8 signext %15, i32 %21)
store i8* %22, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %13
%25 = load i8, i8* %8
%26 = load i8*, i8** %5
store i8 %25, i8* %26
%27 = load i8*, i8** %5
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %5
br label %13
29: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @find_and_replace(i8* %0, i64 %1, i8 signext %2, i8 signext %3) {
%5 = getelementptr inbounds i8, i8* %0, i64 %1
%6 = ptrtoint i8* %5 to i64
%7 = trunc i64 %1 to i32
%8 = tail call i8* @memchr(i8* %0, i8 signext %2, i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %4, %10
%11 = phi i8* [ %16, %10 ], [ %8, %4 ]
store i8 %3, i8* %11
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = ptrtoint i8* %12 to i64
%14 = sub i64 %6, %13
%15 = trunc i64 %14 to i32
%16 = tail call i8* @memchr(i8* %12, i8 signext %2, i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %10
18: ; preds = %10, %4
ret void
}