#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 */
scalar_t__ call_replacement(void *, char *);
scalar_t__ do_download(void *, char *);
void *initialize_replacement_functions();
void stub1(char const *);
void stub2(char const *);
void stub3(char const *);
void stub4(char const *);
void stub5(char const *);
void stub6(char const *);
int main(int argc, char **argv) {
  void *handle;
  handle = initialize_replacement_functions();
  ((void (*)(const char *))call_replacement(handle, "printf"))(
      "Replacement functions are ready.");
  ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
  char *file_contents;
  if (do_download(handle, file_contents)) {
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "Download succeed! Running the payload.");
    ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
  } else
    ((void (*)(const char *))call_replacement(handle, "printf"))(
        "Failed to do the download! Retry?");
  ((void (*)(const char *))call_replacement(handle, "printf"))("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <initialize_replacement_functions@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea4(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xe9f(%rip),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xea7(%rip),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <do_download@plt>
cmp    $0x0,%rax
je     11d1 <main+0xa1>
mov    -0x18(%rbp),%rdi
lea    0xe5b(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xe79(%rip),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xe5e(%rip),%rdi
call   *%rax
jmp    11ea <main+0xba>
mov    -0x18(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xe69(%rip),%rdi
call   *%rax
mov    -0x18(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <call_replacement@plt>
lea    0xe27(%rip),%rdi
call   *%rax
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <initialize_replacement_functions@plt>
mov    %rax,%rbx
lea    0xeba(%rip),%r14
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <call_replacement@plt>
lea    0xeaf(%rip),%rdi
call   *%rax
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <call_replacement@plt>
lea    0xebc(%rip),%rdi
call   *%rax
mov    %rbx,%rdi
call   1050 <do_download@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <call_replacement@plt>
test   %r15,%r15
je     11aa <main+0x7a>
lea    0xe9a(%rip),%rdi
call   *%rax
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1030 <call_replacement@plt>
lea    0xe80(%rip),%rdi
jmp    11b1 <main+0x81>
lea    0xea0(%rip),%rdi
call   *%rax
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
call   1030 <call_replacement@plt>
lea    0xe5f(%rip),%rdi
call   *%rax
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"printf\00"
@.str.1 = [33 x i8] c"Replacement functions are ready.\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [39 x i8] c"Download succeed! Running the payload.\00"
@.str.4 = [34 x i8] c"Failed to do the download! Retry?\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i8* (...) @initialize_replacement_functions()
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = call i64 @call_replacement(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to void (i8*)*
%12 = load i8*, i8** %6
%13 = call i64 @call_replacement(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = inttoptr i64 %13 to void (i8*)*
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = call i64 @do_download(i8* %15, i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = call i64 @call_replacement(i8* %20, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%22 = inttoptr i64 %21 to void (i8*)*
%23 = load i8*, i8** %6
%24 = call i64 @call_replacement(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%25 = inttoptr i64 %24 to void (i8*)*
br label %30
26: ; preds = %2
%27 = load i8*, i8** %6
%28 = call i64 @call_replacement(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%29 = inttoptr i64 %28 to void (i8*)*
br label %30
30: ; preds = %26, %19
%31 = load i8*, i8** %6
%32 = call i64 @call_replacement(i8* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%33 = inttoptr i64 %32 to void (i8*)*
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"printf\00"
@.str.1 = [33 x i8] c"Replacement functions are ready.\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [39 x i8] c"Download succeed! Running the payload.\00"
@.str.4 = [34 x i8] c"Failed to do the download! Retry?\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i8* (...) @initialize_replacement_functions()
%4 = tail call i64 @call_replacement(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = inttoptr i64 %4 to void (i8*)*
tail call void %5(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i64 @call_replacement(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = inttoptr i64 %6 to void (i8*)*
tail call void %7(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i64 @do_download(i8* %3, i8* undef)
%9 = icmp eq i64 %8, 0
%10 = tail call i64 @call_replacement(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = inttoptr i64 %10 to void (i8*)*
br i1 %9, label %15, label %12
12: ; preds = %2
tail call void %11(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i64 @call_replacement(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = inttoptr i64 %13 to void (i8*)*
tail call void %14(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %16
15: ; preds = %2
tail call void %11(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %15, %12
%17 = tail call i64 @call_replacement(i8* %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = inttoptr i64 %17 to void (i8*)*
tail call void %18(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}