#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 *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *replace_substring(char *source, char *buf, char *replace_string,
                        char *start_ptr, char *suffix_ptr) {
  char head[512] = {0};
  strncpy(head, source, start_ptr - source);
  char tail[512] = {0};
  if (suffix_ptr < source + strlen(source)) {
    strcpy(tail, suffix_ptr);
  }
  sprintf(buf, "%s%s%s", head, replace_string, tail);
  return buf;
}


x86 assembly (clang, -O0):
<replace_substring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x230(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x440(%rbp),%rcx
mov    %eax,%edx
mov    -0x438(%rbp),%rax
movslq %edx,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1200 <replace_substring+0xb0>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x230(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x430(%rbp),%r8
lea    0xde3(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_substring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
lea    0x200(%rsp),%rbp
mov    $0x200,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
sub    %r13d,%ebx
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
cltq
add    %r13,%rax
cmp    %r15,%rax
jbe    11c3 <replace_substring+0x73>
mov    %rsp,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
lea    0xe36(%rip),%rsi
lea    0x200(%rsp),%rdx
mov    %rsp,%r8
mov    %r12,%rdi
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %r12,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @replace_substring(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca [512 x i8]
%12 = alloca [512 x i8]
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%13 = bitcast [512 x i8]* %11 to i8*
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%15 = load i8*, i8** %6
%16 = load i8*, i8** %9
%17 = load i8*, i8** %6
%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* %14, i8* %15, i32 %21)
%23 = bitcast [512 x i8]* %12 to i8*
%24 = load i8*, i8** %10
%25 = load i8*, i8** %6
%26 = load i8*, i8** %6
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = icmp ult i8* %24, %29
br i1 %30, label %31, label %35
31: ; preds = %5
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%33 = load i8*, i8** %10
%34 = call i32 @strcpy(i8* %32, i8* %33)
br label %35
35: ; preds = %31, %5
%36 = load i8*, i8** %7
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %11, i64 0, i64 0
%38 = load i8*, i8** %8
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%40 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %37, i8* %38, i8* %39)
%41 = load i8*, i8** %7
ret i8* %41
}

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