#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*/ memset(char *, char, scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
char *strstr(char *, char *);
void strreplace(char *src, char *str, char *rep) {
  char *p = strstr(src, str);
  do {
    if (p) {
      char buf[1024];
      memset(buf, '\0', strlen(buf));
      if (src == p) {
        strcpy(buf, rep);
        strcat(buf, p + strlen(str));
      } else {
        strncpy(buf, src, strlen(src) - strlen(p));
        strcat(buf, rep);
        strcat(buf, p + strlen(str));
      }
      memset(src, '\0', strlen(src));
      strcpy(src, buf);
    }
  } while (p && (p = strstr(src, str)));
}


x86 assembly (clang, -O0):
<strreplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12e5 <strreplace+0x185>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x428(%rbp),%rdi
mov    %rax,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1219 <strreplace+0xb9>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x438(%rbp),%rsi
mov    -0x430(%rbp),%rdi
add    %rax,%rsi
call   1070 <strcat@plt>
jmp    12b0 <strreplace+0x150>
lea    -0x420(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x460(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x460(%rbp),%rdx
mov    -0x458(%rbp),%rdi
mov    -0x450(%rbp),%rsi
sub    %rax,%rdx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcat@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x448(%rbp),%rsi
mov    -0x440(%rbp),%rdi
add    %rax,%rsi
call   1070 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x468(%rbp),%rdi
mov    %rax,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    12ea <strreplace+0x18a>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x469(%rbp)
je     131b <strreplace+0x1bb>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x469(%rbp)
mov    -0x469(%rbp),%al
test   $0x1,%al
jne    1188 <strreplace+0x28>
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strreplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1080 <strstr@plt>
mov    %rax,%rbx
mov    %rsp,%r12
jmp    11e7 <strreplace+0x87>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rbx
mov    %r15,%rdi
call   1050 <strlen@plt>
add    %rax,%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <strcat@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1060 <memset@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     123a <strreplace+0xda>
test   %rbx,%rbx
je     123a <strreplace+0xda>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1060 <memset@plt>
cmp    %r13,%rbx
je     1190 <strreplace+0x30>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %rax,%rbp
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
jmp    119e <strreplace+0x3e>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @strreplace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [1024 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call i8* @strstr(i8* %9, i8* %10)
store i8* %11, i8** %7
br label %12
12: ; preds = %68, %3
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %59
15: ; preds = %12
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = call i64 @strlen(i8* %17)
%19 = call i32 @memset(i8* %16, i8 signext 0, i64 %18)
%20 = load i8*, i8** %4
%21 = load i8*, i8** %7
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %33
23: ; preds = %15
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = load i8*, i8** %6
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** %7
%29 = load i8*, i8** %5
%30 = call i64 @strlen(i8* %29)
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = call i32 @strcat(i8* %27, i8* %31)
br label %51
33: ; preds = %15
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%35 = load i8*, i8** %4
%36 = load i8*, i8** %4
%37 = call i64 @strlen(i8* %36)
%38 = load i8*, i8** %7
%39 = call i64 @strlen(i8* %38)
%40 = sub nsw i64 %37, %39
%41 = call i32 @strncpy(i8* %34, i8* %35, i64 %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = load i8*, i8** %6
%44 = call i32 @strcat(i8* %42, i8* %43)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = load i8*, i8** %7
%47 = load i8*, i8** %5
%48 = call i64 @strlen(i8* %47)
%49 = getelementptr inbounds i8, i8* %46, i64 %48
%50 = call i32 @strcat(i8* %45, i8* %49)
br label %51
51: ; preds = %33, %23
%52 = load i8*, i8** %4
%53 = load i8*, i8** %4
%54 = call i64 @strlen(i8* %53)
%55 = call i32 @memset(i8* %52, i8 signext 0, i64 %54)
%56 = load i8*, i8** %4
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%58 = call i32 @strcpy(i8* %56, i8* %57)
br label %59
59: ; preds = %51, %12
br label %60
60: ; preds = %59
%61 = load i8*, i8** %7
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %68
63: ; preds = %60
%64 = load i8*, i8** %4
%65 = load i8*, i8** %5
%66 = call i8* @strstr(i8* %64, i8* %65)
store i8* %66, i8** %7
%67 = icmp ne i8* %66, null
br label %68
68: ; preds = %63, %60
%69 = phi i1 [ false, %60 ], [ %67, %63 ]
br i1 %69, label %12, label %70
70: ; preds = %68
ret void
}

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