#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 */
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void replace_char(char *s, char c, char *replacement) {
  char tmp[1024];
  char *p = s;
  while ((p = strchr(p, c))) {
    strncpy(tmp, s, p - s);
    tmp[p - s] = '\0';
    strcat(tmp, replacement);
    ++p;
    strcat(tmp, p);
    strcpy(s, tmp);
  }
}


x86 assembly (clang, -O0):
<replace_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1050 <strchr@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     1205 <replace_char+0xc5>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x428(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x428(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x428(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1164 <replace_char+0x24>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_char>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
call   1050 <strchr@plt>
test   %rax,%rax
je     11bf <replace_char+0x7f>
mov    %rax,%rbx
mov    %rsp,%r12
movsbl %bpl,%r13d
nopl   (%rax)
mov    %rbx,%rbp
sub    %r15,%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,(%rsp,%rbp,1)
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
add    $0x1,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1060 <strcat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <replace_char+0x30>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @replace_char(i8* %0, i8 signext %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i8 %1, i8* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
store i8* %9, i8** %8
br label %10
10: ; preds = %15, %3
%11 = load i8*, i8** %8
%12 = load i8, i8* %5
%13 = call i8* @strchr(i8* %11, i8 signext %12)
store i8* %13, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %42
15: ; preds = %10
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%17 = load i8*, i8** %4
%18 = load i8*, i8** %8
%19 = load i8*, i8** %4
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @strncpy(i8* %16, i8* %17, i32 %23)
%25 = load i8*, i8** %8
%26 = load i8*, i8** %4
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %6
%33 = call i32 @strcat(i8* %31, i8* %32)
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %8
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%37 = load i8*, i8** %8
%38 = call i32 @strcat(i8* %36, i8* %37)
%39 = load i8*, i8** %4
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 @strcpy(i8* %39, i8* %40)
br label %10
42: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @replace_char(i8* %0, i8 signext %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i8* @strchr(i8* dereferenceable(1) %0, i8 signext %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %23, label %8
8: ; preds = %3
%9 = ptrtoint i8* %0 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i8* [ %6, %8 ], [ %21, %10 ]
%12 = ptrtoint i8* %11 to i64
%13 = sub i64 %12, %9
%14 = trunc i64 %13 to i32
%15 = call i32 @strncpy(i8* %5, i8* %0, i32 %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %13
store i8 0, i8* %16
%17 = call i32 @strcat(i8* %5, i8* %2)
%18 = getelementptr inbounds i8, i8* %11, i64 1
%19 = call i32 @strcat(i8* %5, i8* %18)
%20 = call i32 @strcpy(i8* %0, i8* %5)
%21 = call i8* @strchr(i8* dereferenceable(1) %18, i8 signext %1)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %10
23: ; preds = %10, %3
ret void
}