#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*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *replace_str(char *str, char orig[], char rep[]) {
  static char buffer[4096];
  static char work[4096];
  char *p;
  if (!(p = strstr(str, orig)))
    return str;
  strncpy(buffer, str, p - str);
  buffer[p - str] = '\0';
  sprintf(buffer + (p - str), "%s%s", rep, p + strlen(orig));
  strcpy(work, buffer);
  while ((p = strstr(work, orig))) {
    strncpy(buffer, work, p - work);
    buffer[p - work] = '\0';
    sprintf(buffer + (p - work), "%s%s", rep, p + strlen(orig));
    strcpy(work, buffer);
  }
  return work;
}


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    118c <replace_str+0x3c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f0 <replace_str+0x1a0>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2ebc(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2ea5(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e8f(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdf7(%rip),%rsi
call   1060 <sprintf@plt>
lea    0x3e4b(%rip),%rdi
lea    0x2e44(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0x3e34(%rip),%rdi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12e5 <replace_str+0x195>
mov    -0x28(%rbp),%rax
lea    0x3e16(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2e0a(%rip),%rdi
lea    0x3e03(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
lea    0x3df3(%rip),%rax
sub    %rax,%rcx
lea    0x2de9(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
lea    0x3dda(%rip),%rax
sub    %rax,%rcx
lea    0x2dd0(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rcx
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xd38(%rip),%rsi
call   1060 <sprintf@plt>
lea    0x3d8c(%rip),%rdi
lea    0x2d85(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1221 <replace_str+0xd1>
lea    0x3d74(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%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,%rbx
mov    %rsi,%rbp
mov    %rdi,%r15
call   1070 <strstr@plt>
test   %rax,%rax
je     124f <replace_str+0xff>
mov    %rax,%r13
mov    %rbp,(%rsp)
mov    %rax,%rbp
sub    %r15,%rbp
lea    0x2eda(%rip),%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
lea    (%r12,%rbp,1),%r14
movb   $0x0,0x0(%rbp,%r12,1)
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xe4a(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1060 <sprintf@plt>
lea    0x3e98(%rip),%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     124f <replace_str+0xff>
mov    %rax,%r13
lea    0x3e73(%rip),%r15
nopl   (%rax)
mov    %r13,%rbp
sub    %r15,%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
lea    (%r12,%rbp,1),%r14
movb   $0x0,0x0(%rbp,%r12,1)
mov    (%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
mov    %r14,%rdi
lea    0xdd7(%rip),%rsi
mov    %rbx,%rdx
call   1060 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1070 <strstr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11f0 <replace_str+0xa0>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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