#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*/ sprintf(char *, char *, char *, ...);
int strlen(char *);
char *strstr(char *, char *);
void find_replace(char *str, char *orig, char *rep, char *output) {
  char buffer[4096] = {0};
  char *p;
  sprintf(buffer, "%s", str);
  if (!(p = strstr(buffer, orig))) {
    sprintf(output, "%s", str);
    return;
  }
  *p = '\0';
  sprintf(output, "%s%s%s", buffer, rep, p + strlen(orig));
}


x86 assembly (clang, -O0):
<find_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe84(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
jne    11c3 <find_replace+0x83>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    1237 <find_replace+0xf7>
mov    -0x1028(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1040(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1028(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1048(%rbp),%r8
mov    -0x1040(%rbp),%rdi
mov    -0x1038(%rbp),%rdx
mov    -0x1030(%rbp),%rcx
cltq
add    %rax,%r8
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_replace>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r12
mov    %rsp,%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11c6 <find_replace+0x76>
mov    %rax,%rbx
movb   $0x0,(%rax)
mov    %r13,%rdi
call   1040 <strlen@plt>
movslq %eax,%r8
add    %rbx,%r8
lea    0xe4c(%rip),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <sprintf@plt>
jmp    11d1 <find_replace+0x81>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define void @find_replace(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%11 = bitcast [4096 x i8]* %9 to i8*
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = call i8* @strstr(i8* %15, i8* %16)
store i8* %17, i8** %10
%18 = icmp ne i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %4
%20 = load i8*, i8** %8
%21 = load i8*, i8** %5
%22 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %34
23: ; preds = %4
%24 = load i8*, i8** %10
store i8 0, i8* %24
%25 = load i8*, i8** %8
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%27 = load i8*, i8** %7
%28 = load i8*, i8** %10
%29 = load i8*, i8** %6
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %27, i8* %32)
br label %34
34: ; preds = %23, %19
ret void
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%s%s%s\00"
define void @find_replace(i8* nocapture %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = call i8* @strcpy(i8* %6, i8* dereferenceable(1) %0)
%8 = call i8* @strstr(i8* %6, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = call i8* @strcpy(i8* dereferenceable(1) %3, i8* dereferenceable(1) %0)
br label %17
12: ; preds = %4
store i8 0, i8* %8
%13 = call i32 @strlen(i8* %1)
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %8, i64 %14
%16 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %3, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %2, i8* %15)
br label %17
17: ; preds = %12, %10
ret void
}