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


x86 assembly (clang, -O0):
<strrep>:
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 <strrep+0x45>
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <strrep+0x57>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1390 <strrep+0x220>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@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     1388 <strrep+0x218>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x90(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x90(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1070 <emalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x38(%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   1040 <strlen@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x38(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <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   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <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    -0x38(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <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   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
jmp    11d4 <strrep+0x64>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strrep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r13
call   1080 <strdup@plt>
mov    %rax,%rbx
test   %r13,%r13
je     12cc <strrep+0x15c>
test   %r15,%r15
je     12cc <strrep+0x15c>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     12cc <strrep+0x15c>
mov    %rax,%r14
mov    %r15,0x8(%rsp)
mov    %r13,(%rsp)
xchg   %ax,%ax
mov    %rbx,0x10(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
sub    %ebp,%ebx
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <emalloc@plt>
mov    %rax,%r12
mov    %r14,%r13
mov    0x10(%rsp),%rsi
sub    %rsi,%r13
mov    %rax,%rdi
mov    %rsi,%rbx
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r13,%rbp
add    %r12,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%r15
add    %rbp,%r15
mov    (%rsp),%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %rbx,%r14
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   1040 <strlen@plt>
add    %r13d,%eax
mov    (%rsp),%r13
sub    %eax,%ebx
mov    %r15,%rdi
mov    0x8(%rsp),%r15
mov    %rbp,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
mov    %r13,%rdi
call   1040 <strlen@plt>
cltq
sub    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
mov    %rax,%r14
mov    %r12,%rbx
test   %rax,%rax
jne    11c0 <strrep+0x50>
jmp    12cf <strrep+0x15f>
mov    %rbx,%r12
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strrep(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* @strdup(i8* %17)
store i8* %18, i8** %4
br label %108
19: ; preds = %13
%20 = load i8*, i8** %5
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %9
br label %22
22: ; preds = %27, %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 %106
27: ; preds = %22
%28 = load i8*, i8** %9
store i8* %28, i8** %10
%29 = load i8*, i8** %10
%30 = call i32 @strlen(i8* %29)
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %30, %32
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %33, %35
%37 = add nsw i32 %36, 1
%38 = call i8* @emalloc(i32 %37)
store i8* %38, i8** %9
%39 = load i8*, i8** %9
%40 = load i8*, i8** %10
%41 = load i8*, i8** %8
%42 = load i8*, i8** %10
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
%47 = call i32 @memcpy(i8* %39, i8* %40, i32 %46)
%48 = load i8*, i8** %9
%49 = load i8*, i8** %8
%50 = load i8*, i8** %10
%51 = ptrtoint i8* %49 to i64
%52 = ptrtoint i8* %50 to i64
%53 = sub i64 %51, %52
%54 = getelementptr inbounds i8, i8* %48, i64 %53
%55 = load i8*, i8** %7
%56 = load i8*, i8** %7
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @memcpy(i8* %54, i8* %55, i32 %57)
%59 = load i8*, i8** %9
%60 = load i8*, i8** %8
%61 = load i8*, i8** %10
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = getelementptr inbounds i8, i8* %59, i64 %64
%66 = load i8*, i8** %7
%67 = call i32 @strlen(i8* %66)
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %65, i64 %68
%70 = load i8*, i8** %8
%71 = load i8*, i8** %6
%72 = call i32 @strlen(i8* %71)
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
%75 = load i8*, i8** %10
%76 = call i32 @strlen(i8* %75)
%77 = load i8*, i8** %6
%78 = call i32 @strlen(i8* %77)
%79 = sub nsw i32 %76, %78
%80 = sext i32 %79 to i64
%81 = load i8*, i8** %8
%82 = load i8*, i8** %10
%83 = ptrtoint i8* %81 to i64
%84 = ptrtoint i8* %82 to i64
%85 = sub i64 %83, %84
%86 = sub nsw i64 %80, %85
%87 = trunc i64 %86 to i32
%88 = call i32 @memcpy(i8* %69, i8* %74, i32 %87)
%89 = load i8*, i8** %9
%90 = load i8*, i8** %10
%91 = call i32 @strlen(i8* %90)
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = load i8*, i8** %6
%95 = call i32 @strlen(i8* %94)
%96 = sext i32 %95 to i64
%97 = sub i64 0, %96
%98 = getelementptr inbounds i8, i8* %93, i64 %97
%99 = load i8*, i8** %7
%100 = call i32 @strlen(i8* %99)
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %98, i64 %101
%103 = call i32 @memset(i8* %102, i32 0, i32 1)
%104 = load i8*, i8** %10
%105 = call i32 @free(i8* %104)
br label %22
106: ; preds = %22
%107 = load i8*, i8** %9
store i8* %107, i8** %4
br label %108
108: ; preds = %106, %16
%109 = load i8*, i8** %4
ret i8* %109
}

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