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


x86 assembly (clang, -O0):
<replace_Substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
lea    0x2ee7(%rip),%rdi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0x2ed7(%rip),%rdi
call   1070 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ac <replace_Substring+0x5c>
lea    0x2ebd(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1264 <replace_Substring+0x114>
mov    -0x30(%rbp),%rax
lea    0x2ea9(%rip),%rcx
sub    %rcx,%rax
mov    %eax,%edx
lea    0x3e9d(%rip),%rdi
lea    0x2e96(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rcx
lea    0x2e86(%rip),%rax
sub    %rax,%rcx
lea    0x3e7c(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
lea    0x2e6d(%rip),%rax
sub    %rax,%rcx
lea    0x3e63(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rcx
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
lea    0xdb4(%rip),%rsi
lea    0x3e0b(%rip),%rdx
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_Substring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
mov    %rsi,%r12
mov    %rdi,%r14
movslq %ecx,%r13
add    %rdi,%r13
lea    0x2eee(%rip),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11ed <replace_Substring+0x9d>
mov    %rax,%rbx
lea    0x2ec9(%rip),%rsi
mov    %rax,%rbp
sub    %rsi,%rbp
lea    0x3ebc(%rip),%r15
mov    %r15,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r15,%rax
add    %rbp,%r15
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rax,%rbp
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    %rbx,%rcx
lea    0xe2f(%rip),%rsi
mov    %r15,%rdi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1060 <sprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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