#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*/ assert(int);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup2(char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *strstr(char const *, char const *);
char *strrep(const char *haystack, const char *needle,
             const char *replacement) {
  size_t h_size = strlen(haystack);
  size_t n_size = strlen(needle);
  size_t rep_size = strlen(replacement);
  assert(h_size > 0);
  char *needle_loc = strstr(haystack, needle);
  if (!needle_loc)
    return strdup2(haystack);
  size_t prefix = needle_loc - haystack;
  char *ret = malloc(1 + h_size - n_size + rep_size);
  strncpy(ret, haystack, prefix);
  strcpy(ret + prefix, replacement);
  strcpy(ret + prefix + rep_size, needle_loc + n_size);
  return ret;
}


x86 assembly (clang, -O0):
<strrep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11eb <strrep+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <strdup2@plt>
mov    %rax,-0x8(%rbp)
jmp    1258 <strrep+0xe8>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
sub    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rdi
add    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rdi
add    -0x48(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
add    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%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,%rbp
mov    %rdi,%r13
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %rax,%r12
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     122b <strrep+0xbb>
mov    %rax,%r14
mov    %rax,%rbp
sub    %r13,%rbp
mov    %r15,0x8(%rsp)
mov    0x10(%rsp),%r15
sub    %r15d,%ebx
lea    (%rbx,%r12,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
add    %rbx,%rbp
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
call   1040 <strcpy@plt>
add    %r12,%rbp
add    %r15,%r14
mov    %rbp,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <strdup2@plt>

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 i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%14 = load i8*, i8** %5
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %8
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %9
%18 = load i8*, i8** %7
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %10
%20 = load i64, i64* %8
%21 = icmp ugt i64 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = call i8* @strstr(i8* %24, i8* %25)
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = icmp ne i8* %27, null
br i1 %28, label %32, label %29
29: ; preds = %3
%30 = load i8*, i8** %5
%31 = call i8* @strdup2(i8* %30)
store i8* %31, i8** %4
br label %65
32: ; preds = %3
%33 = load i8*, i8** %11
%34 = load i8*, i8** %5
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
store i64 %37, i64* %12
%38 = load i64, i64* %8
%39 = add i64 1, %38
%40 = load i64, i64* %9
%41 = sub i64 %39, %40
%42 = load i64, i64* %10
%43 = add i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i8* @malloc(i32 %44)
store i8* %45, i8** %13
%46 = load i8*, i8** %13
%47 = load i8*, i8** %5
%48 = load i64, i64* %12
%49 = call i32 @strncpy(i8* %46, i8* %47, i64 %48)
%50 = load i8*, i8** %13
%51 = load i64, i64* %12
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = load i8*, i8** %7
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = load i8*, i8** %13
%56 = load i64, i64* %12
%57 = getelementptr inbounds i8, i8* %55, i64 %56
%58 = load i64, i64* %10
%59 = getelementptr inbounds i8, i8* %57, i64 %58
%60 = load i8*, i8** %11
%61 = load i64, i64* %9
%62 = getelementptr inbounds i8, i8* %60, i64 %61
%63 = call i32 @strcpy(i8* %59, i8* %62)
%64 = load i8*, i8** %13
store i8* %64, i8** %4
br label %65
65: ; preds = %32, %29
%66 = load i8*, i8** %4
ret i8* %66
}

LLVM IR (-O2):
define i8* @strrep(i8* %0, i8* nocapture %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = icmp ne i64 %4, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = tail call i8* @strdup2(i8* %0)
br label %29
14: ; preds = %3
%15 = ptrtoint i8* %10 to i64
%16 = ptrtoint i8* %0 to i64
%17 = sub i64 %15, %16
%18 = add i64 %4, 1
%19 = sub i64 %18, %5
%20 = add i64 %19, %6
%21 = trunc i64 %20 to i32
%22 = tail call i8* @malloc(i32 %21)
%23 = tail call i32 @strncpy(i8* %22, i8* %0, i64 %17)
%24 = getelementptr inbounds i8, i8* %22, i64 %17
%25 = tail call i32 @strcpy(i8* %24, i8* %2)
%26 = getelementptr inbounds i8, i8* %24, i64 %6
%27 = getelementptr inbounds i8, i8* %10, i64 %5
%28 = tail call i32 @strcpy(i8* %26, i8* %27)
br label %29
29: ; preds = %14, %12
%30 = phi i8* [ %22, %14 ], [ %13, %12 ]
ret i8* %30
}