#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*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strstr(char *, char *);
char *str_replace(char *source, char *find, char *replace) {
  char *s = strstr(source, find);
  int rlen = strlen(replace);
  int flen = strlen(find);
  while (NULL != s) {
    strncpy(s, replace, rlen);
    strcpy(s + rlen, s + flen);
    s += rlen;
    s = strstr(s, find);
  }
  return (source);
}


x86 assembly (clang, -O0):
<str_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     11db <str_replace+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
jmp    117d <str_replace+0x3d>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_replace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,0x10(%rsp)
call   1060 <strstr@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    %r13,%rdi
call   1050 <strlen@plt>
test   %rbx,%rbx
je     11c4 <str_replace+0x84>
movslq 0xc(%rsp),%r14
movslq %eax,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    0xc(%rsp),%edx
call   1030 <strncpy@plt>
lea    (%rbx,%r14,1),%rbp
add    %r15,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1060 <strstr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <str_replace+0x50>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @str_replace(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%10 = load i8*, i8** %4
%11 = load i8*, i8** %5
%12 = call i8* @strstr(i8* %10, i8* %11)
store i8* %12, i8** %7
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %5
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %9
br label %17
17: ; preds = %20, %3
%18 = load i8*, i8** %7
%19 = icmp ne i8* null, %18
br i1 %19, label %20, label %41
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = call i32 @strncpy(i8* %21, i8* %22, i32 %23)
%25 = load i8*, i8** %7
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8*, i8** %7
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = call i32 @strcpy(i8* %28, i8* %32)
%34 = load i32, i32* %8
%35 = load i8*, i8** %7
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = load i8*, i8** %5
%40 = call i8* @strstr(i8* %38, i8* %39)
store i8* %40, i8** %7
br label %17
41: ; preds = %17
%42 = load i8*, i8** %4
ret i8* %42
}

LLVM IR (-O2):
define i8* @str_replace(i8* returned %0, i8* %1, i8* %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @strlen(i8* %1)
%7 = icmp eq i8* %4, null
br i1 %7, label %19, label %8
8: ; preds = %3
%9 = sext i32 %5 to i64
%10 = sext i32 %6 to i64
br label %11
11: ; preds = %8, %11
%12 = phi i8* [ %4, %8 ], [ %17, %11 ]
%13 = tail call i32 @strncpy(i8* %12, i8* %2, i32 %5)
%14 = getelementptr inbounds i8, i8* %12, i64 %9
%15 = getelementptr inbounds i8, i8* %12, i64 %10
%16 = tail call i32 @strcpy(i8* %14, i8* %15)
%17 = tail call i8* @strstr(i8* %14, i8* dereferenceable(1) %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %11
19: ; preds = %11, %3
ret i8* %0
}