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


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1174 <replace_str+0x34>
jmp    121c <replace_str+0xdc>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2ec4(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
lea    0x2ead(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
sub    %rax,%rcx
lea    0x2e97(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xe0f(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0x2e4b(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0x2e39(%rip),%rsi
call   1030 <strncpy@plt>
add    $0x40,%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,%r15
mov    %rdi,%r12
call   1060 <strstr@plt>
test   %rax,%rax
je     11cb <replace_str+0x8b>
mov    %rax,%r13
mov    %rbx,(%rsp)
mov    %rax,%rbx
sub    %r12,%rbx
lea    0x2ede(%rip),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
lea    (%rbx,%rbp,1),%r14
movb   $0x0,(%rbx,%rbp,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r13,%rcx
lea    0xe64(%rip),%rsi
mov    %r14,%rdi
mov    (%rsp),%rdx
call   1050 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strncpy@plt>
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
@.str = [5 x i8] c"%s%s\00"
define void @replace_str(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i8* @strstr(i8* %8, i8* %9)
store i8* %10, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
br label %44
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i8*, i8** %7
%16 = load i8*, i8** %4
%17 = ptrtoint i8* %15 to i64
%18 = ptrtoint i8* %16 to i64
%19 = sub i64 %17, %18
%20 = trunc i64 %19 to i32
%21 = call i32 @strncpy(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i8* %14, i32 %20)
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = ptrtoint i8* %22 to i64
%25 = ptrtoint i8* %23 to i64
%26 = sub i64 %24, %25
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %7
%29 = load i8*, i8** %4
%30 = ptrtoint i8* %28 to i64
%31 = ptrtoint i8* %29 to i64
%32 = sub i64 %30, %31
%33 = getelementptr inbounds i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i64 %32
%34 = load i8*, i8** %6
%35 = load i8*, i8** %7
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %39)
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%43 = call i32 @strncpy(i8* %41, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %13, %12
ret void
}

LLVM IR (-O2):
@replace_str.buffer = internal global [4096 x i8] zeroinitializer
@.str = [5 x i8] c"%s%s\00"
define void @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 %19, 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 @strlen(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%18 = tail call i32 @strncpy(i8* %0, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %3, %6
ret void
}