#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 */
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
int sfpr_str_replace(char *src, char *str_src, char *str_des) {
  char buff[4096];
  char buff2[4096];
  char *ptr = NULL;
  int i = 0;
  if (src != NULL) {
    strcpy(buff2, src);
  } else {
    return -1;
  }
  memset(buff, 0x00, sizeof(buff));
  while ((ptr = strstr(buff2, str_src)) != 0) {
    if (ptr - buff2 != 0)
      memcpy(&buff[i], buff2, ptr - buff2);
    memcpy(&buff[i + ptr - buff2], str_des, strlen(str_des));
    i += ptr - buff2 + strlen(str_des);
    strcpy(buff2, ptr + strlen(str_src));
  }
  strcat(buff, buff2);
  strcpy(src, buff);
  return 0;
}


x86 assembly (clang, -O0):
<sfpr_str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2060,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x2028(%rbp)
movl   $0x0,-0x202c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ac <sfpr_str_replace+0x4c>
lea    -0x2020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11b8 <sfpr_str_replace+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <sfpr_str_replace+0x1d6>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
je     130c <sfpr_str_replace+0x1ac>
mov    -0x2028(%rbp),%rax
lea    -0x2020(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x0,%rax
je     1237 <sfpr_str_replace+0xd7>
movslq -0x202c(%rbp),%rax
lea    -0x1020(%rbp),%rdi
add    %rax,%rdi
lea    -0x2020(%rbp),%rsi
mov    -0x2028(%rbp),%rax
lea    -0x2020(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x202c(%rbp),%eax
mov    -0x2028(%rbp),%rcx
cltq
add    %rax,%rcx
lea    -0x2020(%rbp),%rax
sub    %rax,%rcx
lea    -0x1020(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x2058(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x2050(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2058(%rbp),%rdi
mov    -0x2050(%rbp),%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x2028(%rbp),%rax
lea    -0x2020(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x2048(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2048(%rbp),%rcx
cltq
add    %rax,%rcx
movslq -0x202c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x202c(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2038(%rbp)
mov    -0x2028(%rbp),%rax
mov    %rax,-0x2040(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2040(%rbp),%rsi
mov    -0x2038(%rbp),%rdi
cltq
add    %rax,%rsi
call   1030 <strcpy@plt>
jmp    11cb <sfpr_str_replace+0x6b>
lea    -0x1020(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sfpr_str_replace>:
test   %rdi,%rdi
je     1286 <sfpr_str_replace+0x126>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%rax
lea    0x1010(%rsp),%r12
mov    %r12,%rdi
mov    %rax,(%rsp)
mov    %rax,%rsi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     1251 <sfpr_str_replace+0xf1>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
jmp    1242 <sfpr_str_replace+0xe2>
nopl   0x0(%rax)
lea    (%rsp,%r15,1),%rdi
add    $0x10,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%r15,1),%rax
sub    %r12,%rax
lea    (%rsp,%rax,1),%r14
add    $0x10,%r14
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %ebp,%r15d
add    %eax,%r15d
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
movslq %eax,%rsi
add    %rbx,%rsi
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1251 <sfpr_str_replace+0xf1>
mov    %rbx,%rbp
movslq %r15d,%r15
sub    %r12,%rbp
jne    11d0 <sfpr_str_replace+0x70>
xor    %ebp,%ebp
jmp    11e2 <sfpr_str_replace+0x82>
lea    0x10(%rsp),%rbx
lea    0x1010(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <strcat@plt>
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
define i32 @sfpr_str_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [4096 x i8]
%9 = alloca [4096 x i8]
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* null, i8** %10
store i32 0, i32* %11
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %5
%17 = call i32 @strcpy(i8* %15, i8* %16)
br label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %87
19: ; preds = %14
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 4096)
br label %22
22: ; preds = %46, %19
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%24 = load i8*, i8** %6
%25 = call i8* @strstr(i8* %23, i8* %24)
store i8* %25, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %80
27: ; preds = %22
%28 = load i8*, i8** %10
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %27
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %36
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%39 = load i8*, i8** %10
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @memcpy(i8* %37, i8* %38, i32 %44)
br label %46
46: ; preds = %34, %27
%47 = load i32, i32* %11
%48 = load i8*, i8** %10
%49 = sext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %48, i64 %49
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%52 = ptrtoint i8* %50 to i64
%53 = ptrtoint i8* %51 to i64
%54 = sub i64 %52, %53
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 %54
%56 = load i8*, i8** %7
%57 = load i8*, i8** %7
%58 = call i32 @strlen(i8* %57)
%59 = call i32 @memcpy(i8* %55, i8* %56, i32 %58)
%60 = load i8*, i8** %10
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%62 = ptrtoint i8* %60 to i64
%63 = ptrtoint i8* %61 to i64
%64 = sub i64 %62, %63
%65 = load i8*, i8** %7
%66 = call i32 @strlen(i8* %65)
%67 = sext i32 %66 to i64
%68 = add nsw i64 %64, %67
%69 = load i32, i32* %11
%70 = sext i32 %69 to i64
%71 = add nsw i64 %70, %68
%72 = trunc i64 %71 to i32
store i32 %72, i32* %11
%73 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%74 = load i8*, i8** %10
%75 = load i8*, i8** %6
%76 = call i32 @strlen(i8* %75)
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %74, i64 %77
%79 = call i32 @strcpy(i8* %73, i8* %78)
br label %22
80: ; preds = %22
%81 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%82 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%83 = call i32 @strcat(i8* %81, i8* %82)
%84 = load i8*, i8** %5
%85 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%86 = call i32 @strcpy(i8* %84, i8* %85)
store i32 0, i32* %4
br label %87
87: ; preds = %80, %18
%88 = load i32, i32* %4
ret i32 %88
}

LLVM IR (-O2):
define i32 @sfpr_str_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca [4096 x i8]
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%8 = icmp eq i8* %0, null
br i1 %8, label %47, label %9
9: ; preds = %3
%10 = call i32 @strcpy(i8* %7, i8* %0)
%11 = call i32 @memset(i8* %6, i32 0, i32 4096)
%12 = call i8* @strstr(i8* %7, i8* dereferenceable(1) %1)
%13 = icmp eq i8* %12, null
br i1 %13, label %44, label %14
14: ; preds = %9
%15 = ptrtoint [4096 x i8]* %5 to i64
br label %16
16: ; preds = %14, %27
%17 = phi i8* [ %12, %14 ], [ %42, %27 ]
%18 = phi i32 [ 0, %14 ], [ %37, %27 ]
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %19, %15
%21 = icmp eq i64 %20, 0
%22 = sext i32 %18 to i64
br i1 %21, label %27, label %23
23: ; preds = %16
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %22
%25 = trunc i64 %20 to i32
%26 = call i32 @memcpy(i8* %24, i8* %7, i32 %25)
br label %27
27: ; preds = %16, %23
%28 = phi i32 [ %25, %23 ], [ 0, %16 ]
%29 = getelementptr inbounds i8, i8* %17, i64 %22
%30 = ptrtoint i8* %29 to i64
%31 = sub i64 %30, %15
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %31
%33 = call i32 @strlen(i8* %2)
%34 = call i32 @memcpy(i8* %32, i8* %2, i32 %33)
%35 = call i32 @strlen(i8* %2)
%36 = add i32 %18, %28
%37 = add i32 %36, %35
%38 = call i32 @strlen(i8* %1)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %17, i64 %39
%41 = call i32 @strcpy(i8* %7, i8* %40)
%42 = call i8* @strstr(i8* %7, i8* dereferenceable(1) %1)
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %16
44: ; preds = %27, %9
%45 = call i32 @strcat(i8* %6, i8* %7)
%46 = call i32 @strcpy(i8* %0, i8* %6)
br label %47
47: ; preds = %3, %44
%48 = phi i32 [ 0, %44 ], [ -1, %3 ]
ret i32 %48
}