#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 *);
void replace(char string[], char from[], char to[]) {
  char buffer[4096];
  char *s;
  if (!(s = strstr(string, from))) {
    return;
  }
  strncpy(buffer, string, s - string);
  buffer[s - string] = '\0';
  sprintf(buffer + (s - string), "%s%s", to, s + strlen(from));
  string[0] = 0;
  strcpy(string, buffer);
  return replace(string, from, to);
}


x86 assembly (clang, -O0):
<replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%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,-0x1028(%rbp)
cmp    $0x0,%rax
jne    119a <replace+0x3a>
jmp    125f <replace+0xff>
lea    -0x1020(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1028(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x1028(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rax
mov    -0x1028(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x1038(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    -0x1028(%rbp),%rax
mov    %rax,-0x1040(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1040(%rbp),%rcx
mov    -0x1038(%rbp),%rdi
mov    -0x1030(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdce(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <replace@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1080 <strstr@plt>
test   %rax,%rax
je     11e0 <replace+0x80>
mov    %rax,%r12
mov    %rax,%rbx
sub    %r13,%rbx
mov    %rsp,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
lea    (%rsp,%rbx,1),%rbp
movb   $0x0,(%rsp,%rbx,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    %r12,%rcx
lea    0xe49(%rip),%rsi
mov    %rbp,%rdi
mov    %r14,%rdx
call   1070 <sprintf@plt>
movb   $0x0,0x0(%r13)
mov    %r13,%rdi
mov    %rsp,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <replace@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define void @replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [4096 x i8]
%8 = alloca 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** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %3
br label %52
14: ; preds = %3
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = load i8*, i8** %8
%18 = load i8*, i8** %4
%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* %15, i8* %16, i32 %22)
%24 = load i8*, i8** %8
%25 = load i8*, i8** %4
%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]* %7, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = load i8*, i8** %8
%32 = load i8*, i8** %4
%33 = ptrtoint i8* %31 to i64
%34 = ptrtoint i8* %32 to i64
%35 = sub i64 %33, %34
%36 = getelementptr inbounds i8, i8* %30, i64 %35
%37 = load i8*, i8** %6
%38 = load i8*, i8** %8
%39 = load i8*, i8** %5
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %37, i8* %42)
%44 = load i8*, i8** %4
%45 = getelementptr inbounds i8, i8* %44, i64 0
store i8 0, i8* %45
%46 = load i8*, i8** %4
%47 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%48 = call i32 @strcpy(i8* %46, i8* %47)
%49 = load i8*, i8** %4
%50 = load i8*, i8** %5
%51 = load i8*, i8** %6
br label %52
52: ; preds = %14, %13
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define void @replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%7 = icmp eq i8* %6, null
br i1 %7, label %20, label %8
8: ; preds = %3
%9 = ptrtoint i8* %6 to i64
%10 = ptrtoint i8* %0 to i64
%11 = sub i64 %9, %10
%12 = trunc i64 %11 to i32
%13 = call i32 @strncpy(i8* %5, i8* %0, i32 %12)
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %11
store i8 0, i8* %14
%15 = call i32 @strlen(i8* %1)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = call i32 @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %17)
store i8 0, i8* %0
%19 = call i32 @strcpy(i8* %0, i8* %5)
br label %20
20: ; preds = %3, %8
ret void
}