#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 *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
char *strstr(char const *, char const *);
void tfd_replaceSubStr(char const *aSource, char const *aOldSubStr,
                       char const *aNewSubStr, char *aoDestination) {
  char const *pOccurence;
  char const *p;
  char const *lNewSubStr = "";
  size_t 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):
<tfd_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    %rax,-0x40(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1197 <tfd_replaceSubStr+0x47>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    1236 <tfd_replaceSubStr+0xe6>
cmpq   $0x0,-0x10(%rbp)
jne    11b4 <tfd_replaceSubStr+0x64>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1236 <tfd_replaceSubStr+0xe6>
cmpq   $0x0,-0x18(%rbp)
je     11c7 <tfd_replaceSubStr+0x77>
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     1229 <tfd_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 <tfd_replaceSubStr+0x86>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <strcat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tfd_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     11fc <tfd_replaceSubStr+0xac>
test   %r14,%r14
je     120f <tfd_replaceSubStr+0xbf>
mov    %rax,%r12
test   %rbp,%rbp
lea    0xe72(%rip),%r13
cmovne %rbp,%r13
movb   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11e3 <tfd_replaceSubStr+0x93>
mov    %rax,%rbp
nopl   0x0(%rax)
mov    %ebp,%edx
sub    %ebx,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <strncat@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1060 <strcat@plt>
mov    %rbp,%rbx
add    %r12,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11b0 <tfd_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 @tfd_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** %6
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %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 %59
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 %59
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 %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, %22, %17
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @tfd_replaceSubStr(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %8
7: ; preds = %4
store i8 0, i8* %3
br label %32
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 %32
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 %29, label %17
17: ; preds = %12, %17
%18 = phi i8* [ %27, %17 ], [ %15, %12 ]
%19 = phi i8* [ %26, %17 ], [ %0, %12 ]
%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* %14)
%26 = getelementptr inbounds i8, i8* %18, i64 %5
%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, %12
%30 = phi i8* [ %0, %12 ], [ %26, %17 ]
%31 = tail call i32 @strcat(i8* %3, i8* %30)
br label %32
32: ; preds = %29, %10, %7
ret void
}