#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*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char const *, unsigned int const);
int /*<<< orphan*/ memmove(char *, char *, int);
int strlen(char const *);
char *ustrprintf(char *, char *, char const *, char *);
char *str_replace(char *line, char *start, char *end, const char *replace) {
  const unsigned int len_find = end - start;
  const unsigned int len_replace = strlen(replace);
  if (len_replace < len_find) {
    memcpy(start, replace, len_replace);
    memmove(start + len_replace, end, strlen(end) + 1);
    return line;
  } else {
    char *del = line;
    *start = '\0';
    line = ustrprintf("%s%s%s", line, replace, end);
    free(del);
    return line;
  }
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    11dc <str_replace+0x8c>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1060 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1218 <str_replace+0xc8>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe02(%rip),%rdi
call   1070 <ustrprintf@plt>
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%r14
mov    %r15d,%ebx
sub    %r13d,%ebx
mov    %rcx,%rdi
call   1040 <strlen@plt>
cmp    %ebx,%eax
jae    11a8 <str_replace+0x58>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %ebp,%ebp
add    %r13,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <memmove@plt>
mov    %r14,%rax
jmp    11d0 <str_replace+0x80>
movb   $0x0,0x0(%r13)
lea    0xe4c(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1070 <ustrprintf@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%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* @str_replace(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %8
%14 = load i8*, i8** %7
%15 = ptrtoint i8* %13 to i64
%16 = ptrtoint i8* %14 to i64
%17 = sub i64 %15, %16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %10
%19 = load i8*, i8** %9
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = load i32, i32* %10
%23 = icmp ult i32 %21, %22
br i1 %23, label %24, label %39
24: ; preds = %4
%25 = load i8*, i8** %7
%26 = load i8*, i8** %9
%27 = load i32, i32* %11
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i8*, i8** %7
%30 = load i32, i32* %11
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i8*, i8** %8
%34 = load i8*, i8** %8
%35 = call i32 @strlen(i8* %34)
%36 = add nsw i32 %35, 1
%37 = call i32 @memmove(i8* %32, i8* %33, i32 %36)
%38 = load i8*, i8** %6
store i8* %38, i8** %5
br label %49
39: ; preds = %4
%40 = load i8*, i8** %6
store i8* %40, i8** %12
%41 = load i8*, i8** %7
store i8 0, i8* %41
%42 = load i8*, i8** %6
%43 = load i8*, i8** %9
%44 = load i8*, i8** %8
%45 = call i8* @ustrprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %42, i8* %43, i8* %44)
store i8* %45, i8** %6
%46 = load i8*, i8** %12
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %6
store i8* %48, i8** %5
br label %49
49: ; preds = %39, %24
%50 = load i8*, i8** %5
ret i8* %50
}

LLVM IR (-O2):
@.str = [7 x i8] c"%s%s%s\00"
define i8* @str_replace(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %1 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = tail call i32 @strlen(i8* %3)
%10 = icmp ult i32 %9, %8
br i1 %10, label %11, label %18
11: ; preds = %4
%12 = tail call i32 @memcpy(i8* %1, i8* %3, i32 %9)
%13 = zext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = tail call i32 @strlen(i8* %2)
%16 = add nsw i32 %15, 1
%17 = tail call i32 @memmove(i8* %14, i8* %2, i32 %16)
br label %21
18: ; preds = %4
store i8 0, i8* %1
%19 = tail call i8* @ustrprintf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %3, i8* %2)
%20 = tail call i32 @free(i8* %0)
br label %21
21: ; preds = %18, %11
%22 = phi i8* [ %0, %11 ], [ %19, %18 ]
ret i8* %22
}