#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 const *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char const *);
char *str_replace(char *st, const char *orig, const char *repl,
                  int max_line_length) {
  char buffer[max_line_length];
  char *ch;
  if (!(ch = strstr(st, orig)))
    return st;
  strncpy(buffer, st, ch - st);
  buffer[ch - st] = 0;
  sprintf(buffer + (ch - st), "%s%s", repl, ch + strlen(orig));
  return str_replace(buffer, (const char *)orig, (const char *)repl,
                     max_line_length);
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11bc <str_replace+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1250 <str_replace+0x100>
mov    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x68(%rbp),%rcx
mov    -0x60(%rbp),%rdi
mov    -0x58(%rbp),%rdx
cltq
add    %rax,%rcx
lea    0xdd4(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <str_replace@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,-0x38(%rbp)
mov    %rsi,%r12
mov    %rdi,%r13
mov    %ecx,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1070 <strstr@plt>
test   %rax,%rax
je     11e7 <str_replace+0x97>
mov    %rax,%r15
mov    %r14d,-0x2c(%rbp)
mov    %rax,%r14
sub    %r13,%r14
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
lea    (%rbx,%r14,1),%r13
movb   $0x0,(%rbx,%r14,1)
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
add    %r15,%rcx
lea    0xe3c(%rip),%rsi
mov    %r13,%rdi
mov    -0x38(%rbp),%r14
mov    %r14,%rdx
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    -0x2c(%rbp),%ecx
call   1050 <str_replace@plt>
mov    %rax,%r13
mov    %r13,%rax
lea    -0x28(%rbp),%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 i8* @str_replace(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*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* %9
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %10
%17 = alloca i8, i64 %15
store i64 %15, i64* %11
%18 = load i8*, i8** %6
%19 = load i8*, i8** %7
%20 = call i8* @strstr(i8* %18, i8* %19)
store i8* %20, i8** %12
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %4
%23 = load i8*, i8** %6
store i8* %23, i8** %5
store i32 1, i32* %13
br label %56
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = load i8*, i8** %12
%27 = load i8*, i8** %6
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = trunc i64 %30 to i32
%32 = call i32 @strncpy(i8* %17, i8* %25, i32 %31)
%33 = load i8*, i8** %12
%34 = load i8*, i8** %6
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = getelementptr inbounds i8, i8* %17, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %12
%40 = load i8*, i8** %6
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = getelementptr inbounds i8, i8* %17, i64 %43
%45 = load i8*, i8** %8
%46 = load i8*, i8** %12
%47 = load i8*, i8** %7
%48 = call i32 @strlen(i8* %47)
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %46, i64 %49
%51 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %45, i8* %50)
%52 = load i8*, i8** %7
%53 = load i8*, i8** %8
%54 = load i32, i32* %9
%55 = call i8* @str_replace(i8* %17, i8* %52, i8* %53, i32 %54)
store i8* %55, i8** %5
store i32 1, i32* %13
br label %56
56: ; preds = %24, %22
%57 = load i8*, i8** %10
%58 = load i8*, i8** %5
ret i8* %58
}

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