#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 *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
int replace_str(char *source_target, char *lookup, char *rep) {
  char *buffer, *p;
  buffer = malloc(16384);
  if (!buffer)
    return -3;
  if (strlen(source_target) + strlen(lookup) > 16384)
    return -2;
  if (!(p = strstr(source_target, lookup)))
    return -1;
  memset(buffer, 0, 16384);
  strncpy(buffer, source_target, p - source_target);
  sprintf(buffer + (p - source_target), "%s%s", rep, p + strlen(lookup));
  memset(source_target, 0, strlen(buffer) + 1);
  memcpy(source_target, buffer, strlen(buffer));
  return 0;
}


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x4000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a9 <replace_str+0x39>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12c9 <replace_str+0x159>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
cmp    $0x4000,%eax
jle    11dc <replace_str+0x6c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12c9 <replace_str+0x159>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1203 <replace_str+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <replace_str+0x159>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1050 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xd8c(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x4000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     124f <replace_str+0xdf>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
add    %ebp,%eax
mov    $0xfffffffe,%r12d
cmp    $0x4000,%eax
jg     125d <replace_str+0xed>
mov    %r13,(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1257 <replace_str+0xe7>
mov    %rax,%r13
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4000,%edx
call   1050 <memset@plt>
mov    %r13,%rbp
sub    %r14,%rbp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
add    %rbx,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xde9(%rip),%rsi
mov    %rbp,%rdi
mov    (%rsp),%rdx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
jmp    125d <replace_str+0xed>
mov    $0xfffffffd,%r12d
jmp    125d <replace_str+0xed>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define i32 @replace_str(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%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
%10 = call i8* @malloc(i32 16384)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 -3, i32* %4
br label %64
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
%17 = load i8*, i8** %6
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %16, %18
%20 = icmp sgt i32 %19, 16384
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -2, i32* %4
br label %64
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = call i8* @strstr(i8* %23, i8* %24)
store i8* %25, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %22
store i32 -1, i32* %4
br label %64
28: ; preds = %22
%29 = load i8*, i8** %8
%30 = call i32 @memset(i8* %29, i32 0, i32 16384)
%31 = load i8*, i8** %8
%32 = load i8*, i8** %5
%33 = load i8*, i8** %9
%34 = load i8*, i8** %5
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
%39 = call i32 @strncpy(i8* %31, i8* %32, i32 %38)
%40 = load i8*, i8** %8
%41 = load i8*, i8** %9
%42 = load i8*, i8** %5
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = getelementptr inbounds i8, i8* %40, i64 %45
%47 = load i8*, i8** %7
%48 = load i8*, i8** %9
%49 = load i8*, i8** %6
%50 = call i32 @strlen(i8* %49)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %47, i8* %52)
%54 = load i8*, i8** %5
%55 = load i8*, i8** %8
%56 = call i32 @strlen(i8* %55)
%57 = add nsw i32 %56, 1
%58 = call i32 @memset(i8* %54, i32 0, i32 %57)
%59 = load i8*, i8** %5
%60 = load i8*, i8** %8
%61 = load i8*, i8** %8
%62 = call i32 @strlen(i8* %61)
%63 = call i32 @memcpy(i8* %59, i8* %60, i32 %62)
store i32 0, i32* %4
br label %64
64: ; preds = %28, %27, %21, %13
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i32 @replace_str(i8* %0, i8* %1, i8* %2) {
%4 = tail call dereferenceable_or_null(16384) i8* @malloc(i32 16384)
%5 = icmp eq i8* %4, null
br i1 %5, label %31, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i32 @strlen(i8* %1)
%9 = add nsw i32 %8, %7
%10 = icmp sgt i32 %9, 16384
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %31, label %14
14: ; preds = %11
%15 = tail call i32 @memset(i8* %4, i32 0, i32 16384)
%16 = ptrtoint i8* %12 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = tail call i32 @strncpy(i8* %4, i8* %0, i32 %19)
%21 = getelementptr inbounds i8, i8* %4, i64 %18
%22 = tail call i32 @strlen(i8* %1)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %12, i64 %23
%25 = tail call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %24)
%26 = tail call i32 @strlen(i8* %4)
%27 = add nsw i32 %26, 1
%28 = tail call i32 @memset(i8* %0, i32 0, i32 %27)
%29 = tail call i32 @strlen(i8* %4)
%30 = tail call i32 @memcpy(i8* %0, i8* %4, i32 %29)
br label %31
31: ; preds = %11, %6, %3, %14
%32 = phi i32 [ 0, %14 ], [ -3, %3 ], [ -2, %6 ], [ -1, %11 ]
ret i32 %32
}