#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strdup(char const *);
int strlen(char const *);
char *strstr(char const *, char const *);
char *str_replace(const char *string, const char *substr,
                  const char *replacement) {
  char *tok = NULL;
  char *newstr = NULL;
  tok = strstr(string, substr);
  if (tok == NULL)
    return strdup(string);
  newstr = malloc(strlen(string) - strlen(substr) + strlen(replacement) + 1);
  if (newstr == NULL)
    return NULL;
  memcpy(newstr, string, tok - string);
  memcpy(newstr + (tok - string), replacement, strlen(replacement));
  memcpy(newstr + (tok - string) + strlen(replacement), tok + strlen(substr),
         strlen(string) - strlen(substr) - (tok - string));
  memset(newstr + strlen(string) - strlen(substr) + strlen(replacement), 0, 1);
  return newstr;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b5 <str_replace+0x55>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    134c <str_replace+0x1ec>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x34(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1206 <str_replace+0xa6>
movq   $0x0,-0x8(%rbp)
jmp    134c <str_replace+0x1ec>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x88(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
sub    %ecx,%eax
cltq
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
call   1080 <strstr@plt>
test   %rax,%rax
je     1283 <str_replace+0x123>
mov    %rax,%r12
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1030 <strlen@plt>
sub    %r13d,%ebp
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1299 <str_replace+0x139>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
mov    %r12,%r13
sub    %r14,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
mov    %r13,%rbx
add    %rbp,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r15,0x10(%rsp)
movslq %eax,%r15
add    %rbx,%r15
mov    %r12,(%rsp)
mov    0x8(%rsp),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
add    (%rsp),%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rbp,(%rsp)
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
add    %r13d,%eax
sub    %eax,%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    (%rsp),%rbp
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
add    %rbp,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
sub    %rax,%rbx
mov    0x10(%rsp),%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
jmp    129b <str_replace+0x13b>
mov    %r14,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <strdup@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @str_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %4
br label %99
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 @strlen(i8* %19)
%21 = load i8*, i8** %6
%22 = call i32 @strlen(i8* %21)
%23 = sub nsw i32 %20, %22
%24 = load i8*, i8** %7
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %23, %25
%27 = add nsw i32 %26, 1
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %18
store i8* null, i8** %4
br label %99
32: ; preds = %18
%33 = load i8*, i8** %9
%34 = load i8*, i8** %5
%35 = load i8*, i8** %8
%36 = load i8*, i8** %5
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @memcpy(i8* %33, i8* %34, i32 %40)
%42 = load i8*, i8** %9
%43 = load i8*, i8** %8
%44 = load i8*, i8** %5
%45 = ptrtoint i8* %43 to i64
%46 = ptrtoint i8* %44 to i64
%47 = sub i64 %45, %46
%48 = getelementptr inbounds i8, i8* %42, i64 %47
%49 = load i8*, i8** %7
%50 = load i8*, i8** %7
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @memcpy(i8* %48, i8* %49, i32 %51)
%53 = load i8*, i8** %9
%54 = load i8*, i8** %8
%55 = load i8*, i8** %5
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = getelementptr inbounds i8, i8* %53, i64 %58
%60 = load i8*, i8** %7
%61 = call i32 @strlen(i8* %60)
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
%64 = load i8*, i8** %8
%65 = load i8*, i8** %6
%66 = call i32 @strlen(i8* %65)
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %64, i64 %67
%69 = load i8*, i8** %5
%70 = call i32 @strlen(i8* %69)
%71 = load i8*, i8** %6
%72 = call i32 @strlen(i8* %71)
%73 = sub nsw i32 %70, %72
%74 = sext i32 %73 to i64
%75 = load i8*, i8** %8
%76 = load i8*, i8** %5
%77 = ptrtoint i8* %75 to i64
%78 = ptrtoint i8* %76 to i64
%79 = sub i64 %77, %78
%80 = sub nsw i64 %74, %79
%81 = trunc i64 %80 to i32
%82 = call i32 @memcpy(i8* %63, i8* %68, i32 %81)
%83 = load i8*, i8** %9
%84 = load i8*, i8** %5
%85 = call i32 @strlen(i8* %84)
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds i8, i8* %83, i64 %86
%88 = load i8*, i8** %6
%89 = call i32 @strlen(i8* %88)
%90 = sext i32 %89 to i64
%91 = sub i64 0, %90
%92 = getelementptr inbounds i8, i8* %87, i64 %91
%93 = load i8*, i8** %7
%94 = call i32 @strlen(i8* %93)
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds i8, i8* %92, i64 %95
%97 = call i32 @memset(i8* %96, i32 0, i32 1)
%98 = load i8*, i8** %9
store i8* %98, i8** %4
br label %99
99: ; preds = %32, %31, %15
%100 = load i8*, i8** %4
ret i8* %100
}

LLVM IR (-O2):
define i8* @str_replace(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i8* @strdup(i8* %0)
br label %48
8: ; preds = %3
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i32 @strlen(i8* %1)
%11 = tail call i32 @strlen(i8* %2)
%12 = add i32 %9, 1
%13 = sub i32 %12, %10
%14 = add i32 %13, %11
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %48, label %17
17: ; preds = %8
%18 = ptrtoint i8* %4 to i64
%19 = ptrtoint i8* %0 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = tail call i32 @memcpy(i8* %15, i8* %0, i32 %21)
%23 = getelementptr inbounds i8, i8* %15, i64 %20
%24 = tail call i32 @strlen(i8* %2)
%25 = tail call i32 @memcpy(i8* %23, i8* %2, i32 %24)
%26 = tail call i32 @strlen(i8* %2)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
%29 = tail call i32 @strlen(i8* %1)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %4, i64 %30
%32 = tail call i32 @strlen(i8* %0)
%33 = tail call i32 @strlen(i8* %1)
%34 = add i32 %33, %21
%35 = sub i32 %32, %34
%36 = tail call i32 @memcpy(i8* %28, i8* %31, i32 %35)
%37 = tail call i32 @strlen(i8* %0)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %15, i64 %38
%40 = tail call i32 @strlen(i8* %1)
%41 = sext i32 %40 to i64
%42 = sub nsw i64 0, %41
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = tail call i32 @strlen(i8* %2)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = tail call i32 @memset(i8* %46, i32 0, i32 1)
br label %48
48: ; preds = %8, %17, %6
%49 = phi i8* [ %7, %6 ], [ %15, %17 ], [ null, %8 ]
ret i8* %49
}