#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*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *strstr(char *, char const *);
int /*<<< orphan*/ xfree(char *);
char *xmalloc(int);
char *xstrdup(char const *);
int xstrlen(char const *);
char *xstrreplace(const char *string, const char *substr,
                  const char *replacement) {
  char *tok = NULL;
  char *newstr = NULL;
  char *oldstr = NULL;
  if (substr == NULL || replacement == NULL) {
    return xstrdup(string);
  }
  newstr = xstrdup(string);
  while ((tok = strstr(newstr, substr))) {
    oldstr = newstr;
    newstr =
        xmalloc(xstrlen(oldstr) - xstrlen(substr) + xstrlen(replacement) + 1);
    if (newstr == NULL) {
      xfree(oldstr);
      return NULL;
    }
    memcpy(newstr, oldstr, tok - oldstr);
    memcpy(newstr + (tok - oldstr), replacement, xstrlen(replacement));
    memcpy(newstr + (tok - oldstr) + xstrlen(replacement),
           tok + xstrlen(substr),
           xstrlen(oldstr) - xstrlen(substr) - (tok - oldstr));
    memset(newstr + xstrlen(oldstr) - xstrlen(substr) + xstrlen(replacement), 0,
           1);
    xfree(oldstr);
  }
  return newstr;
}


x86 assembly (clang, -O0):
<xstrreplace>:
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)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b5 <xstrreplace+0x45>
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <xstrreplace+0x57>
mov    -0x10(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    13ab <xstrreplace+0x23b>
mov    -0x10(%rbp),%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     13a3 <xstrreplace+0x233>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    -0x3c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1251 <xstrreplace+0xe1>
mov    -0x38(%rbp),%rdi
call   1030 <xfree@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ab <xstrreplace+0x23b>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x90(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    -0x90(%rbp),%rdi
mov    -0x88(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x80(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x70(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x78(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x68(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
sub    %ecx,%eax
cltq
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <xstrlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
mov    -0x38(%rbp),%rdi
call   1030 <xfree@plt>
jmp    11d4 <xstrreplace+0x64>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrreplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,(%rsp)
mov    %rsi,%rbx
call   1060 <xstrdup@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     12d3 <xstrreplace+0x163>
cmpq   $0x0,(%rsp)
je     12d3 <xstrreplace+0x163>
mov    %rbx,%r15
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     12d3 <xstrreplace+0x163>
mov    %r15,0x8(%rsp)
nopl   (%rax)
mov    %rax,%r13
mov    %r14,%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ebp
mov    (%rsp),%rdi
call   1080 <xstrlen@plt>
sub    %ebp,%ebx
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1050 <xmalloc@plt>
test   %rax,%rax
je     12d8 <xstrreplace+0x168>
mov    %rax,%r12
mov    %r13,0x10(%rsp)
sub    %r14,%r13
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1070 <memcpy@plt>
mov    %r13,%rbp
add    %r12,%rbp
mov    (%rsp),%rbx
mov    %rbx,%rdi
call   1080 <xstrlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1080 <xstrlen@plt>
movslq %eax,%r15
add    %rbp,%r15
mov    0x8(%rsp),%rdi
call   1080 <xstrlen@plt>
movslq %eax,%rbp
add    0x10(%rsp),%rbp
mov    %r14,%rdi
call   1080 <xstrlen@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1080 <xstrlen@plt>
add    %r13d,%eax
mov    0x8(%rsp),%r13
sub    %eax,%ebx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
mov    %r14,%rdi
call   1080 <xstrlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
mov    %r13,%rdi
call   1080 <xstrlen@plt>
cltq
sub    %rax,%rbx
mov    (%rsp),%rdi
call   1080 <xstrlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
call   1030 <xfree@plt>
mov    %r12,%rdi
mov    %r13,%r15
mov    %r13,%rsi
call   1090 <strstr@plt>
mov    %r12,%r14
test   %rax,%rax
jne    11c0 <xstrreplace+0x50>
jmp    12e3 <xstrreplace+0x173>
mov    %r14,%r12
jmp    12e3 <xstrreplace+0x173>
mov    %r14,%rdi
call   1030 <xfree@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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