#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);
size_t 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 = "";
  if (!aSource) {
    *aoDestination = '\0';
    return;
  }
  if (!aOldSubStr) {
    strcpy(aoDestination, aSource);
    return;
  }
  if (aNewSubStr) {
    lNewSubStr = aNewSubStr;
  }
  p = aSource;
  size_t lOldSubLen = strlen(aOldSubStr);
  *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)
cmpq   $0x0,-0x8(%rbp)
jne    118a <replaceSubStr+0x3a>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1236 <replaceSubStr+0xe6>
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <replaceSubStr+0x57>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1236 <replaceSubStr+0xe6>
cmpq   $0x0,-0x18(%rbp)
je     11ba <replaceSubStr+0x6a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%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     1229 <replaceSubStr+0xd9>
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
add    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11d6 <replaceSubStr+0x86>
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
test   %rdi,%rdi
je     11fc <replaceSubStr+0xac>
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     120f <replaceSubStr+0xbf>
test   %rdx,%rdx
lea    0xe80(%rip),%r12
cmovne %rdx,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
movb   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11e3 <replaceSubStr+0x93>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%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    %rbp,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbp
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 i64
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** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %4
%16 = load i8*, i8** %8
store i8 0, i8* %16
br label %59
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = load i8*, i8** %8
%22 = load i8*, i8** %5
%23 = call i32 @strcpy(i8* %21, i8* %22)
br label %59
24: ; preds = %17
%25 = load i8*, i8** %7
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i8*, i8** %7
store i8* %28, i8** %11
br label %29
29: ; preds = %27, %24
%30 = load i8*, i8** %5
store i8* %30, i8** %10
%31 = load i8*, i8** %6
%32 = call i64 @strlen(i8* %31)
store i64 %32, i64* %12
%33 = load i8*, i8** %8
store i8 0, i8* %33
br label %34
34: ; preds = %39, %29
%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 %55
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 i64, i64* %12
%54 = getelementptr inbounds i8, i8* %52, i64 %53
store i8* %54, i8** %10
br label %34
55: ; preds = %34
%56 = load i8*, i8** %8
%57 = load i8*, i8** %10
%58 = call i32 @strcat(i8* %56, i8* %57)
br label %59
59: ; preds = %55, %20, %15
ret void
}

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