#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 */
scalar_t__ calloc(int, 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 *stringReplace(const char *string, const char *substr,
                    const char *replacement) {
  char *newstr = NULL;
  const char *tok = strstr(string, (const char *)substr);
  if (tok == NULL)
    return strdup(string);
  newstr = (char *)calloc(
      strlen(string) - strlen(substr) + strlen(replacement) + 1, sizeof(char));
  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):
<stringReplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ad <stringReplace+0x4d>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1349 <stringReplace+0x1e9>
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
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1203 <stringReplace+0xa3>
movq   $0x0,-0x8(%rbp)
jmp    1349 <stringReplace+0x1e9>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%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   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%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    -0x30(%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    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x28(%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    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringReplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
call   1080 <strstr@plt>
test   %rax,%rax
je     1288 <stringReplace+0x128>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %r13d,%ebp
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     129e <stringReplace+0x13e>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
mov    %r15,%r13
sub    %r14,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r13,%rbx
add    %rbp,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %r12,0x10(%rsp)
movslq %eax,%r12
add    %rbx,%r12
mov    %r15,(%rsp)
mov    0x8(%rsp),%r15
mov    %r15,%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    %r15,%rdi
call   1030 <strlen@plt>
add    %r13d,%eax
sub    %eax,%ebp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
mov    (%rsp),%rbp
call   1060 <memcpy@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
add    %rbp,%rbx
mov    %r15,%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    12a0 <stringReplace+0x140>
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* @stringReplace(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
%10 = load i8*, i8** %5
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%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 %100
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 i64 @calloc(i32 %27, i32 1)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %8
%30 = load i8*, i8** %8
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %18
store i8* null, i8** %4
br label %100
33: ; preds = %18
%34 = load i8*, i8** %8
%35 = load i8*, i8** %5
%36 = load i8*, i8** %9
%37 = load i8*, i8** %5
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = trunc i64 %40 to i32
%42 = call i32 @memcpy(i8* %34, i8* %35, i32 %41)
%43 = load i8*, i8** %8
%44 = load i8*, i8** %9
%45 = load i8*, i8** %5
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
%49 = getelementptr inbounds i8, i8* %43, i64 %48
%50 = load i8*, i8** %7
%51 = load i8*, i8** %7
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @memcpy(i8* %49, i8* %50, i32 %52)
%54 = load i8*, i8** %8
%55 = load i8*, i8** %9
%56 = load i8*, i8** %5
%57 = ptrtoint i8* %55 to i64
%58 = ptrtoint i8* %56 to i64
%59 = sub i64 %57, %58
%60 = getelementptr inbounds i8, i8* %54, i64 %59
%61 = load i8*, i8** %7
%62 = call i32 @strlen(i8* %61)
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
%65 = load i8*, i8** %9
%66 = load i8*, i8** %6
%67 = call i32 @strlen(i8* %66)
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
%70 = load i8*, i8** %5
%71 = call i32 @strlen(i8* %70)
%72 = load i8*, i8** %6
%73 = call i32 @strlen(i8* %72)
%74 = sub nsw i32 %71, %73
%75 = sext i32 %74 to i64
%76 = load i8*, i8** %9
%77 = load i8*, i8** %5
%78 = ptrtoint i8* %76 to i64
%79 = ptrtoint i8* %77 to i64
%80 = sub i64 %78, %79
%81 = sub nsw i64 %75, %80
%82 = trunc i64 %81 to i32
%83 = call i32 @memcpy(i8* %64, i8* %69, i32 %82)
%84 = load i8*, i8** %8
%85 = load i8*, i8** %5
%86 = call i32 @strlen(i8* %85)
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %84, i64 %87
%89 = load i8*, i8** %6
%90 = call i32 @strlen(i8* %89)
%91 = sext i32 %90 to i64
%92 = sub i64 0, %91
%93 = getelementptr inbounds i8, i8* %88, i64 %92
%94 = load i8*, i8** %7
%95 = call i32 @strlen(i8* %94)
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %93, i64 %96
%98 = call i32 @memset(i8* %97, i32 0, i32 1)
%99 = load i8*, i8** %8
store i8* %99, i8** %4
br label %100
100: ; preds = %33, %32, %15
%101 = load i8*, i8** %4
ret i8* %101
}

LLVM IR (-O2):
define i8* @stringReplace(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 %49
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 i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %49, label %18
18: ; preds = %8
%19 = ptrtoint i8* %4 to i64
%20 = ptrtoint i8* %0 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = tail call i32 @memcpy(i8* %16, i8* %0, i32 %22)
%24 = getelementptr inbounds i8, i8* %16, i64 %21
%25 = tail call i32 @strlen(i8* %2)
%26 = tail call i32 @memcpy(i8* %24, i8* %2, i32 %25)
%27 = tail call i32 @strlen(i8* %2)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %24, i64 %28
%30 = tail call i32 @strlen(i8* %1)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %4, i64 %31
%33 = tail call i32 @strlen(i8* %0)
%34 = tail call i32 @strlen(i8* %1)
%35 = add i32 %34, %22
%36 = sub i32 %33, %35
%37 = tail call i32 @memcpy(i8* %29, i8* %32, i32 %36)
%38 = tail call i32 @strlen(i8* %0)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %16, i64 %39
%41 = tail call i32 @strlen(i8* %1)
%42 = sext i32 %41 to i64
%43 = sub nsw i64 0, %42
%44 = getelementptr inbounds i8, i8* %40, i64 %43
%45 = tail call i32 @strlen(i8* %2)
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = tail call i32 @memset(i8* %47, i32 0, i32 1)
br label %49
49: ; preds = %8, %18, %6
%50 = phi i8* [ %7, %6 ], [ %16, %18 ], [ null, %8 ]
ret i8* %50
}