#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 find_string(char *, char *);
int /*<<< orphan*/ insert_string(char *, char *, int);
int /*<<< orphan*/ remove_string(char *, int, int);
int replace_string(char source[], char find[], char replace[]) {
  int found;
  int f_length;
  for (f_length = 0; find[f_length] != '\0'; ++f_length) {
  }
  found = find_string(source, find);
  if (found != -1) {
    remove_string(source, found, f_length);
    insert_string(source, replace, found);
  }
  return found;
}


x86 assembly (clang, -O0):
<replace_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1173 <replace_string+0x43>
jmp    1165 <replace_string+0x35>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    114b <replace_string+0x1b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <find_string@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11ac <replace_string+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1040 <remove_string@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <insert_string@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
nop
cmpb   $0x0,(%rsi,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1140 <replace_string+0x10>
mov    %r15,%rdi
call   1050 <find_string@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1175 <replace_string+0x45>
add    $0xffffffff,%ebx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1040 <remove_string@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <insert_string@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @replace_string(i8* %0, i8* %1, i8* %2) {
br label %4
4: ; preds = %4, %3
%5 = phi i64 [ %9, %4 ], [ 0, %3 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
%9 = add nuw i64 %5, 1
br i1 %8, label %10, label %4
10: ; preds = %4
%11 = tail call i32 @find_string(i8* %0, i8* %1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = trunc i64 %5 to i32
%15 = tail call i32 @remove_string(i8* %0, i32 %11, i32 %14)
%16 = tail call i32 @insert_string(i8* %0, i8* %2, i32 %11)
br label %17
17: ; preds = %13, %10
ret i32 %11
}