#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 *strchr(char *, char);
void replace_char(char *string, char from, char to, int maxlen) {
  char *lastchar = string + maxlen;
  while (string) {
    string = strchr(string, from);
    if (string) {
      *string = to;
      if (string >= lastchar)
        return;
    }
  }
}


x86 assembly (clang, -O0):
<replace_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %rdi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1186 <replace_char+0x76>
mov    -0x8(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1181 <replace_char+0x71>
mov    -0xa(%rbp),%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     117c <replace_char+0x6c>
jmp    1186 <replace_char+0x76>
jmp    1181 <replace_char+0x71>
jmp    1139 <replace_char+0x29>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_char>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1147 <replace_char+0x37>
mov    %edx,%r14d
mov    %rdi,%rax
movslq %ecx,%rbx
add    %rdi,%rbx
movsbl %sil,%ebp
nopl   0x0(%rax)
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1147 <replace_char+0x37>
mov    %r14b,(%rax)
cmp    %rbx,%rax
jb     1130 <replace_char+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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