#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*/ strcat(char *const, char const *);
int /*<<< orphan*/ strcpy(char *const, char const *const);
int strlen(char const *const);
int /*<<< orphan*/ strncat(char *const, char const *, int);
char *strstr(char const *, char const *const);
void replaceSubStr(char const *const aSource, char const *const aOldSubStr,
                   char const *const aNewSubStr, char *const aoDestination) {
  char const *pOccurence;
  char const *p;
  char const *lNewSubStr = "";
  int lOldSubLen = strlen(aOldSubStr);
  if (!aSource) {
    *aoDestination = '\0';
    return;
  }
  if (!aOldSubStr) {
    strcpy(aoDestination, aSource);
    return;
  }
  if (aNewSubStr) {
    lNewSubStr = aNewSubStr;
  }
  p = aSource;
  *aoDestination = '\0';
  while ((pOccurence = strstr(p, aOldSubStr)) != NULL) {
    strncat(aoDestination, p, pOccurence - p);
    strcat(aoDestination, lNewSubStr);
    p = pOccurence + lOldSubLen;
  }
  strcat(aoDestination, p);
}


x86 assembly (clang, -O0):
<replaceSubStr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1196 <replaceSubStr+0x46>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1238 <replaceSubStr+0xe8>
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <replaceSubStr+0x63>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1238 <replaceSubStr+0xe8>
cmpq   $0x0,-0x18(%rbp)
je     11c6 <replaceSubStr+0x76>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     122b <replaceSubStr+0xdb>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11d5 <replaceSubStr+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <strcat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceSubStr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rbx,%rbx
je     11fd <replaceSubStr+0xad>
test   %r14,%r14
je     1210 <replaceSubStr+0xc0>
mov    %eax,0x4(%rsp)
test   %rbp,%rbp
lea    0xe71(%rip),%r12
cmovne %rbp,%r12
movb   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11e4 <replaceSubStr+0x94>
mov    %rax,%r13
movslq 0x4(%rsp),%rbp
nop
mov    %r13d,%edx
sub    %ebx,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <strncat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <strcat@plt>
mov    %r13,%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    11b0 <replaceSubStr+0x60>
mov    %r15,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <strcat@plt>
movb   $0x0,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <strcpy@plt>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @replaceSubStr(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %8
7: ; preds = %4
store i8 0, i8* %3
br label %34
8: ; preds = %4
%9 = icmp eq i8* %1, null
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = tail call i32 @strcpy(i8* %3, i8* %0)
br label %34
12: ; preds = %8
%13 = icmp eq i8* %2, null
%14 = select i1 %13, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %2
store i8 0, i8* %3
%15 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* %1)
%16 = icmp eq i8* %15, null
br i1 %16, label %31, label %17
17: ; preds = %12
%18 = sext i32 %5 to i64
br label %19
19: ; preds = %17, %19
%20 = phi i8* [ %15, %17 ], [ %29, %19 ]
%21 = phi i8* [ %0, %17 ], [ %28, %19 ]
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = tail call i32 @strncat(i8* %3, i8* %21, i32 %25)
%27 = tail call i32 @strcat(i8* %3, i8* %14)
%28 = getelementptr inbounds i8, i8* %20, i64 %18
%29 = tail call i8* @strstr(i8* dereferenceable(1) %28, i8* %1)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %19
31: ; preds = %19, %12
%32 = phi i8* [ %0, %12 ], [ %28, %19 ]
%33 = tail call i32 @strcat(i8* %3, i8* %32)
br label %34
34: ; preds = %31, %10, %7
ret void
}