#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*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ strncasecmp(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strncat(char *, char *, int);
char *str_replace(const char *seed, const char *apple, char *tree) {
  static char buffer[8192 * 2];
  char *nail;
  char *bpt;
  buffer[0] = '\0';
  bpt = &buffer[0];
  nail = &tree[0];
  while (*nail != '\0') {
    if (strncasecmp(nail, seed, strlen(seed))) {
      strncat(bpt, nail, 1);
      bpt++;
      nail++;
    } else {
      strcat(buffer, apple);
      bpt += strlen(apple);
      nail += strlen(seed);
    }
  }
  *bpt++ = '\0';
  strcpy(tree, buffer);
  return buffer;
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x0,0x2ef5(%rip)
lea    0x2eee(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1236 <str_replace+0xe6>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <strncasecmp@plt>
cmp    $0x0,%rax
je     11ef <str_replace+0x9f>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1231 <str_replace+0xe1>
mov    -0x10(%rbp),%rsi
lea    0x2e66(%rip),%rdi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    117e <str_replace+0x2e>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    0x2e0d(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x2e01(%rip),%rax
add    $0x40,%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    %rdx,%r14
movb   $0x0,0x2efb(%rip)
cmpb   $0x0,(%rdx)
je     11e3 <str_replace+0x93>
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x2ee9(%rip),%r13
mov    %r13,%rbx
mov    %r14,%rbp
jmp    11ae <str_replace+0x5e>
nop
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <strcat@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
add    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x0,0x0(%rbp,%rax,1)
lea    0x0(%rbp,%rax,1),%rbp
je     11ea <str_replace+0x9a>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1060 <strncasecmp@plt>
test   %rax,%rax
je     1180 <str_replace+0x30>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
add    $0x1,%rbx
mov    $0x1,%eax
jmp    11a2 <str_replace+0x52>
lea    0x2e76(%rip),%rbx
movb   $0x0,(%rbx)
lea    0x2e6c(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@str_replace.buffer = internal global [16384 x i8] zeroinitializer
define i8* @str_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i8 0, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0)
store i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0), i8** %8
%9 = load i8*, i8** %6
%10 = getelementptr inbounds i8, i8* %9, i64 0
store i8* %10, i8** %7
br label %11
11: ; preds = %44, %3
%12 = load i8*, i8** %7
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %11
%17 = load i8*, i8** %7
%18 = load i8*, i8** %4
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = call i64 @strncasecmp(i8* %17, i8* %18, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %16
%24 = load i8*, i8** %8
%25 = load i8*, i8** %7
%26 = call i32 @strncat(i8* %24, i8* %25, i32 1)
%27 = load i8*, i8** %8
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %8
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %7
br label %44
31: ; preds = %16
%32 = load i8*, i8** %5
%33 = call i32 @strcat(i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0), i8* %32)
%34 = load i8*, i8** %5
%35 = call i32 @strlen(i8* %34)
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
%39 = load i8*, i8** %4
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %7
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8, i8* %41, i64 %42
store i8* %43, i8** %7
br label %44
44: ; preds = %31, %23
br label %11
45: ; preds = %11
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %8
store i8 0, i8* %46
%48 = load i8*, i8** %6
%49 = call i32 @strcpy(i8* %48, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0))
ret i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0)
}

LLVM IR (-O2):
@str_replace.buffer = internal global [16384 x i8] zeroinitializer
define i8* @str_replace(i8* %0, i8* %1, i8* %2) {
store i8 0, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0)
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %3, %22
%7 = phi i8* [ %24, %22 ], [ getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0), %3 ]
%8 = phi i8* [ %25, %22 ], [ %2, %3 ]
%9 = tail call i32 @strlen(i8* %0)
%10 = tail call i64 @strncasecmp(i8* %8, i8* %0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = tail call i32 @strncat(i8* %7, i8* %8, i32 1)
%14 = getelementptr inbounds i8, i8* %7, i64 1
br label %22
15: ; preds = %6
%16 = tail call i32 @strcat(i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0), i8* %1)
%17 = tail call i32 @strlen(i8* %1)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = tail call i32 @strlen(i8* %0)
%21 = sext i32 %20 to i64
br label %22
22: ; preds = %15, %12
%23 = phi i64 [ %21, %15 ], [ 1, %12 ]
%24 = phi i8* [ %19, %15 ], [ %14, %12 ]
%25 = getelementptr inbounds i8, i8* %8, i64 %23
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 0
br i1 %27, label %28, label %6
28: ; preds = %22, %3
%29 = phi i8* [ getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0), %3 ], [ %24, %22 ]
store i8 0, i8* %29
%30 = tail call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0))
ret i8* getelementptr inbounds ([16384 x i8], [16384 x i8]* @str_replace.buffer, i64 0, i64 0)
}