#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 *, size_t);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *strstr(char const *, char const *);
void string_replace(char *target, const char *needle, const char *replacement) {
  char buffer[2048] = {0};
  char *insert_point = &buffer[0];
  const char *tmp = target;
  size_t needle_len = strlen(needle);
  size_t repl_len = strlen(replacement);
  while (1) {
    const char *p = strstr(tmp, needle);
    if (p == NULL) {
      strcpy(insert_point, tmp);
      break;
    }
    memcpy(insert_point, tmp, p - tmp);
    insert_point += p - tmp;
    memcpy(insert_point, replacement, repl_len);
    insert_point += repl_len;
    tmp = p + needle_len;
  }
  strcpy(target, buffer);
}


x86 assembly (clang, -O0):
<string_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1050 <memset@plt>
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x838(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x840(%rbp)
mov    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
jne    11f0 <string_replace+0xa0>
mov    -0x828(%rbp),%rdi
mov    -0x830(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1279 <string_replace+0x129>
mov    -0x828(%rbp),%rdi
mov    -0x830(%rbp),%rsi
mov    -0x848(%rbp),%rdx
mov    -0x830(%rbp),%rax
sub    %rax,%rdx
call   1060 <memcpy@plt>
mov    -0x848(%rbp),%rax
mov    -0x830(%rbp),%rcx
sub    %rcx,%rax
add    -0x828(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x828(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x840(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x840(%rbp),%rax
add    -0x828(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x848(%rbp),%rax
add    -0x838(%rbp),%rax
mov    %rax,-0x830(%rbp)
jmp    11b3 <string_replace+0x63>
mov    -0x8(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %rdx,%rbp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x20(%rsp),%rbx
mov    $0x800,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,0x10(%rsp)
mov    %rbp,0x18(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %r15,%r13
mov    %r15,%rsi
call   1070 <strstr@plt>
mov    %r14,0x8(%rsp)
test   %rax,%rax
je     1208 <string_replace+0xb8>
mov    %rax,%r15
lea    0x20(%rsp),%r12
mov    0x8(%rsp),%r14
mov    %r15,%rbx
sub    %r14,%rbx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
add    %r12,%rbx
mov    %rbx,%rdi
mov    0x18(%rsp),%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
add    %rbp,%rbx
mov    %r15,%r14
add    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %r13,%rsi
call   1070 <strstr@plt>
mov    %rax,%r15
mov    %rbx,%r12
test   %rax,%rax
jne    11c0 <string_replace+0x70>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0x20(%rsp),%rsi
mov    0x8(%rsp),%rdi
call   1030 <strcpy@plt>
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @string_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [2048 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%13 = bitcast [2048 x i8]* %7 to i8*
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
store i8* %14, i8** %8
%15 = load i8*, i8** %4
store i8* %15, i8** %9
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %10
%18 = load i8*, i8** %6
%19 = call i64 @strlen(i8* %18)
store i64 %19, i64* %11
br label %20
20: ; preds = %3, %30
%21 = load i8*, i8** %9
%22 = load i8*, i8** %5
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %12
%24 = load i8*, i8** %12
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i8*, i8** %8
%28 = load i8*, i8** %9
%29 = call i32 @strcpy(i8* %27, i8* %28)
br label %56
30: ; preds = %20
%31 = load i8*, i8** %8
%32 = load i8*, i8** %9
%33 = load i8*, i8** %12
%34 = load i8*, i8** %9
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = call i32 @memcpy(i8* %31, i8* %32, i64 %37)
%39 = load i8*, i8** %12
%40 = load i8*, i8** %9
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 %43
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = load i8*, i8** %6
%48 = load i64, i64* %11
%49 = call i32 @memcpy(i8* %46, i8* %47, i64 %48)
%50 = load i64, i64* %11
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i64 %50
store i8* %52, i8** %8
%53 = load i8*, i8** %12
%54 = load i64, i64* %10
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8* %55, i8** %9
br label %20
56: ; preds = %26
%57 = load i8*, i8** %4
%58 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%59 = call i32 @strcpy(i8* %57, i8* %58)
ret void
}

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