#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 strlen(char const *);
bool replaceString(char source[], const char s1[], const char s2[]) {
  int findString(const char source[], const char destination[]);
  void removeString(char source[], int start, int counts);
  void insertString(char source[], const char insertChars[], int start);
  int start;
  start = findString(source, s1);
  if (start == -1)
    return false;
  else {
    removeString(source, start, strlen(s1));
    insertString(source, s2, start);
    return true;
  }
}


x86 assembly (clang, -O0):
<replaceString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <findString@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    117a <replaceString+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <replaceString+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    %eax,%edx
call   1060 <removeString@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <insertString@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
call   1040 <findString@plt>
cmp    $0xffffffff,%eax
je     1184 <replaceString+0x44>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <removeString@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <insertString@plt>
mov    $0x1,%eax
jmp    1186 <replaceString+0x46>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @replaceString(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = call i32 @findString(i8* %9, i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %23
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* %8
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %5
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
store i32 1, i32* %4
br label %23
23: ; preds = %15, %14
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
define i32 @replaceString(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @findString(i8* %0, i8* %1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %1)
tail call void @removeString(i8* %0, i32 %4, i32 %7)
tail call void @insertString(i8* %0, i8* %2, i32 %4)
br label %8
8: ; preds = %3, %6
%9 = phi i32 [ 1, %6 ], [ 0, %3 ]
ret i32 %9
}