#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 */
char *replace_str(char *, char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *replace_all_str(char *str, char *orig, char *rep) {
  static char buf[4080];
  char *replace_buf, *p;
  strncpy(buf, str, 4080);
  while ((p = strstr(buf, orig))) {
    replace_buf = replace_str(buf, orig, rep);
    strncpy(buf, replace_buf, 4080);
    buf[4080 - 1] = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<replace_all_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0x2f01(%rip),%rdi
mov    $0xff0,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rsi
lea    0x2eec(%rip),%rdi
call   1050 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11b0 <replace_all_str+0x80>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0x2eca(%rip),%rdi
call   1040 <replace_str@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0x2eb6(%rip),%rdi
mov    $0xff0,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3e94(%rip)
jmp    1159 <replace_all_str+0x29>
lea    0x2e99(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_all_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rsi
lea    0x2f0b(%rip),%rbx
mov    %rbx,%rdi
mov    $0xff0,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11a5 <replace_all_str+0x75>
lea    0x2ee7(%rip),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <replace_str@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
mov    $0xff0,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3eaa(%rip)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    1170 <replace_all_str+0x40>
lea    0x2ea4(%rip),%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@replace_all_str.buf = internal global [4080 x i8] zeroinitializer
define i8* @replace_all_str(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
%9 = load i8*, i8** %4
%10 = call i32 @strncpy(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %9, i32 4080)
br label %11
11: ; preds = %15, %3
%12 = load i8*, i8** %5
%13 = call i8* @strstr(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %12)
store i8* %13, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i8* @replace_str(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %16, i8* %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = call i32 @strncpy(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %19, i32 4080)
store i8 0, i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 4079)
br label %11
21: ; preds = %11
ret i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@replace_all_str.buf = internal global [4080 x i8] zeroinitializer
define i8* @replace_all_str(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strncpy(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %0, i32 4080)
%5 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* dereferenceable(1) %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %3, %7
%8 = tail call i8* @replace_str(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %1, i8* %2)
%9 = tail call i32 @strncpy(i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* %8, i32 4080)
store i8 0, i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 4079)
%10 = tail call i8* @strstr(i8* dereferenceable(1) getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0), i8* dereferenceable(1) %1)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %7
12: ; preds = %7, %3
ret i8* getelementptr inbounds ([4080 x i8], [4080 x i8]* @replace_all_str.buf, i64 0, i64 0)
}