#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 *string_replace(char *original, char *pattern, char *replacement) {
  static char buffer[4096];
  char *p_tmp;
  if (!(p_tmp = strstr(original, pattern)))
    return original;
  strncpy(buffer, original, p_tmp - original);
  buffer[p_tmp - original] = '\0';
  sprintf(buffer + (p_tmp - original), "%s%s", replacement,
          p_tmp + strlen(pattern));
  return buffer;
}


x86 assembly (clang, -O0):
<string_replace>:
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    117c <string_replace+0x3c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <string_replace+0xc9>
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   1040 <strlen@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xe07(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2e4b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1060 <strstr@plt>
test   %rax,%rax
je     11ab <string_replace+0x6b>
mov    %rax,%r12
mov    %rax,%r13
sub    %rbx,%r13
lea    0x2ee2(%rip),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rbx
add    %r13,%rbx
movb   $0x0,0x0(%r13,%rbp,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r12,%rcx
lea    0xe63(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1050 <sprintf@plt>
mov    %rbp,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@string_replace.buffer = internal global [4096 x i8] zeroinitializer
@.str = [5 x i8] c"%s%s\00"
define i8* @string_replace(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 %43
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]* @string_replace.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]* @string_replace.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]* @string_replace.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)
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @string_replace.buffer, i64 0, i64 0), i8** %4
br label %43
43: ; preds = %15, %13
%44 = load i8*, i8** %4
ret i8* %44
}

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