#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*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *replace_str(char *str, char *orig, char *rep) {
  static char buffer[4096];
  char *p;
  int i = 0;
  while (str[i]) {
    if (!(p = strstr(str + i, orig)))
      return str;
    strncpy(buffer + strlen(buffer), str + i, (p - str) - i);
    buffer[p - str] = '\0';
    strcat(buffer, rep);
    printf("STR:%s\n", buffer);
    i = (p - str) + strlen(orig);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<replace_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     124d <replace_str+0xfd>
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ac <replace_str+0x5c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1258 <replace_str+0x108>
lea    0x2ead(%rip),%rdi
call   1040 <strlen@plt>
cltq
lea    0x2e9f(%rip),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x2c(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
lea    0x2e66(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rsi
lea    0x2e57(%rip),%rdi
call   1060 <strcat@plt>
lea    0xdeb(%rip),%rdi
lea    0x2e44(%rip),%rsi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    116b <replace_str+0x1b>
lea    0x2e0c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_str>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,(%rsp)
cmpb   $0x0,(%rdi)
je     11f3 <replace_str+0xa3>
mov    %rsi,%r15
mov    %rdi,%r12
xor    %r14d,%r14d
lea    0x2ee8(%rip),%r13
mov    %rdi,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11fa <replace_str+0xaa>
mov    %rax,%rbp
mov    %r13,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdi
add    %r13,%rdi
sub    %r12,%rbp
mov    %ebp,%edx
sub    %r14d,%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,0x0(%rbp,%r13,1)
mov    %r13,%rdi
mov    (%rsp),%rsi
call   1060 <strcat@plt>
lea    0xe36(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
add    %rbp,%r14
movslq %r14d,%rax
lea    (%r12,%rax,1),%rbx
cmpb   $0x0,(%r12,%rax,1)
jne    1180 <replace_str+0x30>
mov    %r13,%r12
jmp    11fa <replace_str+0xaa>
lea    0x2e66(%rip),%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@replace_str.buffer = internal global [4096 x i8] zeroinitializer
@.str = [8 x i8] c"STR:%s\0A\00"
define i8* @replace_str(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %9
br label %10
10: ; preds = %27, %3
%11 = load i8*, i8** %5
%12 = load i32, i32* %9
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %64
17: ; preds = %10
%18 = load i8*, i8** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8*, i8** %6
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %8
%24 = icmp ne i8* %23, null
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = load i8*, i8** %5
store i8* %26, i8** %4
br label %65
27: ; preds = %17
%28 = call i32 @strlen(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i64 %29
%31 = load i8*, i8** %5
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8*, i8** %8
%36 = load i8*, i8** %5
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = sub nsw i64 %39, %41
%43 = trunc i64 %42 to i32
%44 = call i32 @strncpy(i8* %30, i8* %34, i32 %43)
%45 = load i8*, i8** %8
%46 = load i8*, i8** %5
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 %49
store i8 0, i8* %50
%51 = load i8*, i8** %7
%52 = call i32 @strcat(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i8* %51)
%53 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%54 = load i8*, i8** %8
%55 = load i8*, i8** %5
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = load i8*, i8** %6
%60 = call i32 @strlen(i8* %59)
%61 = sext i32 %60 to i64
%62 = add nsw i64 %58, %61
%63 = trunc i64 %62 to i32
store i32 %63, i32* %9
br label %10
64: ; preds = %10
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i8** %4
br label %65
65: ; preds = %64, %25
%66 = load i8*, i8** %4
ret i8* %66
}

LLVM IR (-O2):
@replace_str.buffer = internal global [4096 x i8] zeroinitializer
@.str = [8 x i8] c"STR:%s\0A\00"
define i8* @replace_str(i8* %0, i8* %1, i8* %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %33, label %6
6: ; preds = %3
%7 = ptrtoint i8* %0 to i64
br label %8
8: ; preds = %6, %13
%9 = phi i8* [ %0, %6 ], [ %30, %13 ]
%10 = phi i64 [ 0, %6 ], [ %27, %13 ]
%11 = tail call i8* @strstr(i8* %9, i8* dereferenceable(1) %1)
%12 = icmp eq i8* %11, null
br i1 %12, label %33, label %13
13: ; preds = %8
%14 = tail call i32 @strlen(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 %15
%17 = ptrtoint i8* %11 to i64
%18 = sub i64 %17, %7
%19 = sub i64 %18, %10
%20 = trunc i64 %19 to i32
%21 = tail call i32 @strncpy(i8* %16, i8* %9, i32 %20)
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 %18
store i8 0, i8* %22
%23 = tail call i32 @strcat(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), i8* %2)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0))
%25 = tail call i32 @strlen(i8* %1)
%26 = zext i32 %25 to i64
%27 = add i64 %18, %26
%28 = shl i64 %27, 32
%29 = ashr exact i64 %28, 32
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 0
br i1 %32, label %33, label %8
33: ; preds = %8, %13, %3
%34 = phi i8* [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), %3 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @replace_str.buffer, i64 0, i64 0), %13 ], [ %0, %8 ]
ret i8* %34
}