#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*/ CopyString(char *, char *, int);
int /*<<< orphan*/ GetStringBit(char *, char *, int, int);
scalar_t__ IsEqual(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int strlen(char *);
void ReplaceString(char *str, char *fnd, char *rpl) {
  char tmp[20480], *tfn;
  int i, n, szFind, szRep, szFinal;
  szFind = strlen(fnd);
  szRep = strlen(rpl);
  tfn = malloc(szFind);
  n = 0;
  for (i = 0; i < strlen(str); i++) {
    GetStringBit(str, tfn, i, szFind);
    if (IsEqual(tfn, fnd)) {
      CopyString(rpl, tmp, n);
      i += (szFind - 1);
      n += szRep;
    } else {
      tmp[n] = str[i];
      n++;
    }
    tmp[n] = 0x00;
  }
  CopyString(tmp, str, 0);
  free(tfn);
}


x86 assembly (clang, -O0):
<ReplaceString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x5034(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x5038(%rbp)
mov    -0x5034(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x5028(%rbp)
movl   $0x0,-0x5030(%rbp)
movl   $0x0,-0x502c(%rbp)
mov    -0x502c(%rbp),%eax
mov    %eax,-0x5040(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x5040(%rbp),%eax
cmp    %ecx,%eax
jge    12a6 <ReplaceString+0x146>
mov    -0x8(%rbp),%rdi
mov    -0x5028(%rbp),%rsi
mov    -0x502c(%rbp),%edx
mov    -0x5034(%rbp),%ecx
call   1070 <GetStringBit@plt>
mov    -0x5028(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <IsEqual@plt>
cmp    $0x0,%rax
je     1258 <ReplaceString+0xf8>
mov    -0x18(%rbp),%rdi
lea    -0x5020(%rbp),%rsi
mov    -0x5030(%rbp),%edx
call   1040 <CopyString@plt>
mov    -0x5034(%rbp),%eax
sub    $0x1,%eax
add    -0x502c(%rbp),%eax
mov    %eax,-0x502c(%rbp)
mov    -0x5038(%rbp),%eax
add    -0x5030(%rbp),%eax
mov    %eax,-0x5030(%rbp)
jmp    1283 <ReplaceString+0x123>
mov    -0x8(%rbp),%rax
movslq -0x502c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
movslq -0x5030(%rbp),%rax
mov    %cl,-0x5020(%rbp,%rax,1)
mov    -0x5030(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5030(%rbp)
movslq -0x5030(%rbp),%rax
movb   $0x0,-0x5020(%rbp,%rax,1)
mov    -0x502c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x502c(%rbp)
jmp    11bb <ReplaceString+0x5b>
lea    -0x5020(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1040 <CopyString@plt>
mov    -0x5028(%rbp),%rdi
call   1030 <free@plt>
add    $0x5040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReplaceString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5018,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r14,0x8(%rsp)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    122b <ReplaceString+0xcb>
mov    %ebp,%eax
add    $0xffffffff,%eax
mov    %eax,(%rsp)
xor    %r14d,%r14d
xor    %r13d,%r13d
jmp    11ef <ReplaceString+0x8f>
nop
movslq %r14d,%rax
movzbl (%rbx,%rax,1),%eax
movslq %r13d,%rcx
mov    %al,0x10(%rsp,%rcx,1)
mov    $0x1,%eax
add    %eax,%r13d
movslq %r13d,%rax
movb   $0x0,0x10(%rsp,%rax,1)
add    $0x1,%r14d
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    %eax,%r14d
jge    122b <ReplaceString+0xcb>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1070 <GetStringBit@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <IsEqual@plt>
test   %rax,%rax
je     11c0 <ReplaceString+0x60>
mov    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %r13d,%edx
call   1040 <CopyString@plt>
add    (%rsp),%r14d
mov    0x4(%rsp),%eax
jmp    11d3 <ReplaceString+0x73>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <CopyString@plt>
mov    %r12,%rdi
call   1030 <free@plt>
add    $0x5018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @ReplaceString(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [20480 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %11
%16 = load i8*, i8** %6
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %12
%18 = load i32, i32* %11
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %8
store i32 0, i32* %10
store i32 0, i32* %9
br label %20
20: ; preds = %62, %3
%21 = load i32, i32* %9
%22 = load i8*, i8** %4
%23 = call i32 @strlen(i8* %22)
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %65
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = load i8*, i8** %8
%28 = load i32, i32* %9
%29 = load i32, i32* %11
%30 = call i32 @GetStringBit(i8* %26, i8* %27, i32 %28, i32 %29)
%31 = load i8*, i8** %8
%32 = load i8*, i8** %5
%33 = call i64 @IsEqual(i8* %31, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %25
%36 = load i8*, i8** %6
%37 = getelementptr inbounds [20480 x i8], [20480 x i8]* %7, i64 0, i64 0
%38 = load i32, i32* %10
%39 = call i32 @CopyString(i8* %36, i8* %37, i32 %38)
%40 = load i32, i32* %11
%41 = sub nsw i32 %40, 1
%42 = load i32, i32* %9
%43 = add nsw i32 %42, %41
store i32 %43, i32* %9
%44 = load i32, i32* %12
%45 = load i32, i32* %10
%46 = add nsw i32 %45, %44
store i32 %46, i32* %10
br label %58
47: ; preds = %25
%48 = load i8*, i8** %4
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [20480 x i8], [20480 x i8]* %7, i64 0, i64 %54
store i8 %52, i8* %55
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %58
58: ; preds = %47, %35
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [20480 x i8], [20480 x i8]* %7, i64 0, i64 %60
store i8 0, i8* %61
br label %62
62: ; preds = %58
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
store i32 %64, i32* %9
br label %20
65: ; preds = %20
%66 = getelementptr inbounds [20480 x i8], [20480 x i8]* %7, i64 0, i64 0
%67 = load i8*, i8** %4
%68 = call i32 @CopyString(i8* %66, i8* %67, i32 0)
%69 = load i8*, i8** %8
%70 = call i32 @free(i8* %69)
ret void
}

LLVM IR (-O2):
define void @ReplaceString(i8* %0, i8* %1, i8* %2) {
%4 = alloca [20480 x i8]
%5 = getelementptr inbounds [20480 x i8], [20480 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %1)
%7 = tail call i32 @strlen(i8* %2)
%8 = tail call i8* @malloc(i32 %6)
%9 = call i32 @strlen(i8* %0)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %3
%12 = add nsw i32 %6, -1
br label %13
13: ; preds = %11, %28
%14 = phi i32 [ 0, %11 ], [ %34, %28 ]
%15 = phi i32 [ 0, %11 ], [ %31, %28 ]
%16 = call i32 @GetStringBit(i8* %0, i8* %8, i32 %14, i32 %6)
%17 = call i64 @IsEqual(i8* %8, i8* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %13
%20 = call i32 @CopyString(i8* %2, i8* %5, i32 %15)
%21 = add nsw i32 %12, %14
br label %28
22: ; preds = %13
%23 = sext i32 %14 to i64
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = sext i32 %15 to i64
%27 = getelementptr inbounds [20480 x i8], [20480 x i8]* %4, i64 0, i64 %26
store i8 %25, i8* %27
br label %28
28: ; preds = %22, %19
%29 = phi i32 [ %7, %19 ], [ 1, %22 ]
%30 = phi i32 [ %21, %19 ], [ %14, %22 ]
%31 = add nsw i32 %29, %15
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [20480 x i8], [20480 x i8]* %4, i64 0, i64 %32
store i8 0, i8* %33
%34 = add nsw i32 %30, 1
%35 = call i32 @strlen(i8* %0)
%36 = icmp slt i32 %34, %35
br i1 %36, label %13, label %37
37: ; preds = %28, %3
%38 = call i32 @CopyString(i8* %5, i8* %0, i32 0)
%39 = call i32 @free(i8* %8)
ret void
}