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


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117f <replace_str+0x3f>
lea    0x2eda(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    120c <replace_str+0xcc>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x2eb9(%rip),%rdi
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2ea2(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e8c(%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   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xe04(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2e48(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
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,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1060 <strstr@plt>
test   %rax,%rax
je     11a3 <replace_str+0x63>
mov    %rax,%r12
mov    %rax,%rbx
sub    %r13,%rbx
lea    0x2ee2(%rip),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
lea    (%rbx,%rbp,1),%r13
movb   $0x0,(%rbx,%rbp,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r12,%rcx
lea    0xe68(%rip),%rsi
mov    %r13,%rdi
mov    %r14,%rdx
call   1050 <sprintf@plt>
lea    0x2ea6(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@replace_str.buf = internal global [4080 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 %14, label %13
13: ; preds = %3
store i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0), i8** %4
br label %42
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i8*, i8** %8
%17 = load i8*, i8** %5
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = call i32 @strncpy(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0), i8* %15, i32 %21)
%23 = load i8*, i8** %8
%24 = load i8*, i8** %5
%25 = ptrtoint i8* %23 to i64
%26 = ptrtoint i8* %24 to i64
%27 = sub i64 %25, %26
%28 = getelementptr inbounds [4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %8
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = getelementptr inbounds i8, i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0), i64 %33
%35 = load i8*, i8** %7
%36 = load i8*, i8** %8
%37 = load i8*, i8** %6
%38 = call i32 @strlen(i8* %37)
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
%41 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %35, i8* %40)
store i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0), i8** %4
br label %42
42: ; preds = %14, %13
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@replace_str.buf = internal global [4080 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 %17, 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 ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0), i8* %0, i32 %10)
%12 = getelementptr inbounds [4080 x i8], [4080 x i8]* @replace_str.buf, 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)
br label %17
17: ; preds = %3, %6
ret i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_str.buf, i64 0, i64 0)
}