#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 const *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char const *);
char *strstr(char *, char const *);
void strchg(char *buf, const char *target, const char *repl) {
  char tmp[1024 + 1];
  char *p;
  while ((p = strstr(buf, target)) != NULL) {
    *p = '\0';
    p += strlen(target);
    strcpy(tmp, p);
    strcat(buf, repl);
    strcat(buf, tmp);
  }
}


x86 assembly (clang, -O0):
<strchg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
je     11d3 <strchg+0x93>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x428(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1050 <strcat@plt>
jmp    1157 <strchg+0x17>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strchg>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1060 <strstr@plt>
test   %rax,%rax
je     11b2 <strchg+0x72>
mov    %rax,%rbx
mov    %rsp,%r12
nopl   0x0(%rax)
movb   $0x0,(%rbx)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
add    %rbx,%rsi
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <strcat@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <strcat@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1170 <strchg+0x30>
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @strchg(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1025 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%6 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %18, label %8
8: ; preds = %3, %8
%9 = phi i8* [ %16, %8 ], [ %6, %3 ]
store i8 0, i8* %9
%10 = call i32 @strlen(i8* %1)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = call i32 @strcpy(i8* %5, i8* %12)
%14 = call i32 @strcat(i8* %0, i8* %2)
%15 = call i32 @strcat(i8* %0, i8* %5)
%16 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %8
18: ; preds = %8, %3
ret void
}