#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 = "";
  if (!aSource) {
    *aoDestination = '\0';
    return;
  }
  if (!aOldSubStr) {
    strcpy(aoDestination, aSource);
    return;
  }
  if (aNewSubStr) {
    lNewSubStr = aNewSubStr;
  }
  p = aSource;
  int 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    1238 <replaceSubStr+0xe8>
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <replaceSubStr+0x57>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1238 <replaceSubStr+0xe8>
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    %eax,-0x3c(%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
test   %rdi,%rdi
je     11fd <replaceSubStr+0xad>
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rsi,%rsi
je     1210 <replaceSubStr+0xc0>
test   %rdx,%rdx
lea    0xe80(%rip),%r12
cmovne %rdx,%r12
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
movb   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11e4 <replaceSubStr+0x94>
mov    %rax,%r13
movslq %ebp,%rbp
nopl   0x0(%rax,%rax,1)
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** %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 %60
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 %60
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 i32 @strlen(i8* %31)
store i32 %32, i32* %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 %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, %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 %34
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 %34
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 i32 @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 %31, label %17
17: ; preds = %11
%18 = sext i32 %14 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* %13)
%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, %11
%32 = phi i8* [ %0, %11 ], [ %28, %19 ]
%33 = tail call i32 @strcat(i8* %3, i8* %32)
br label %34
34: ; preds = %31, %9, %6
ret void
}