#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*/ free_string(char *);
char *str_dup(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
char *string_replace(char *orig, char *old, char *new) {
  char buf[8192];
  int i;
  buf[0] = '\0';
  strcpy(buf, orig);
  if (strstr(orig, old) != NULL) {
    i = strlen(orig) - strlen(strstr(orig, old));
    buf[i] = '\0';
    strcat(buf, new);
    strcat(buf, &orig[i + strlen(old)]);
    if (orig)
      free_string(orig);
  }
  return str_dup(buf);
}


x86 assembly (clang, -O0):
<string_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x0,-0x2020(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1261 <string_replace+0x101>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x203c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x203c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2024(%rbp)
movslq -0x2024(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2030(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x2038(%rbp)
mov    -0x2024(%rbp),%eax
mov    %eax,-0x2028(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2038(%rbp),%rsi
mov    -0x2030(%rbp),%rdi
mov    %eax,%ecx
mov    -0x2028(%rbp),%eax
add    %ecx,%eax
cltq
add    %rax,%rsi
call   1060 <strcat@plt>
cmpq   $0x0,-0x8(%rbp)
je     125c <string_replace+0xfc>
mov    -0x8(%rbp),%rdi
call   1050 <free_string@plt>
jmp    1261 <string_replace+0x101>
lea    -0x2020(%rbp),%rdi
call   1070 <str_dup@plt>
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_replace>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,(%rsp)
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f0 <string_replace+0x90>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
mov    %rax,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
movslq %ebp,%rbp
movb   $0x0,(%rsp,%rbp,1)
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strcat@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
movslq %ebp,%rsi
add    %rbx,%rsi
mov    %r12,%rdi
call   1060 <strcat@plt>
test   %rbx,%rbx
je     11f0 <string_replace+0x90>
mov    %rbx,%rdi
call   1050 <free_string@plt>
mov    %rsp,%rdi
call   1070 <str_dup@plt>
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @string_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [8192 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
store i8 0, i8* %9
%10 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i8* @strstr(i8* %13, i8* %14)
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %46
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %4
%21 = load i8*, i8** %5
%22 = call i8* @strstr(i8* %20, i8* %21)
%23 = call i32 @strlen(i8* %22)
%24 = sub nsw i32 %19, %23
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%29 = load i8*, i8** %6
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = load i32, i32* %8
%34 = load i8*, i8** %5
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %33, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %32, i64 %37
%39 = call i32 @strcat(i8* %31, i8* %38)
%40 = load i8*, i8** %4
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %17
%43 = load i8*, i8** %4
%44 = call i32 @free_string(i8* %43)
br label %45
45: ; preds = %42, %17
br label %46
46: ; preds = %45, %3
%47 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%48 = call i8* @str_dup(i8* %47)
ret i8* %48
}

LLVM IR (-O2):
define i8* @string_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
store i8 0, i8* %5
%6 = call i32 @strcpy(i8* %5, i8* %0)
%7 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %25, label %9
9: ; preds = %3
%10 = call i32 @strlen(i8* %0)
%11 = call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%12 = call i32 @strlen(i8* %11)
%13 = sub nsw i32 %10, %12
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @strcat(i8* %5, i8* %2)
%17 = call i32 @strlen(i8* %1)
%18 = add nsw i32 %17, %13
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %0, i64 %19
%21 = call i32 @strcat(i8* %5, i8* %20)
%22 = icmp eq i8* %0, null
br i1 %22, label %25, label %23
23: ; preds = %9
%24 = call i32 @free_string(i8* %0)
br label %25
25: ; preds = %9, %23, %3
%26 = call i8* @str_dup(i8* %5)
ret i8* %26
}