#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 */
struct stat {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ make_loader_data(char *, char *, size_t *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int main(int argc, char **argv) {
  char *oldloaderfilename = argv[1];
  char *newloaderfilename = argv[2];
  ;
  size_t oldloaderfilesize, newloaderfilesize;
  FILE *fp;
  struct stat buf;
  char oldloaderbuf[1024 * 1024] = "";
  char newloaderbuf[1024 * 1024] = "";
  stat(oldloaderfilename, &buf);
  oldloaderfilesize = buf.st_size;
  printf("oldloaderfilename=%s,oldloaderfilesize=%d,newloaderfilename=%s\n",
         oldloaderfilename, oldloaderfilesize, newloaderfilename);
  fp = fopen(oldloaderfilename, "rb");
  if (!fp) {
    printf("Can not open file %s !\n", oldloaderfilename);
    return -1;
  }
  if (fread(oldloaderbuf, 1, oldloaderfilesize, fp) == oldloaderfilesize) {
    make_loader_data(oldloaderbuf, newloaderbuf, &newloaderfilesize);
  } else {
    printf("read old loader file fail!\n");
  }
  fclose(fp);
  fp = fopen(newloaderfilename, "w+b");
  if (fwrite(newloaderbuf, 1, newloaderfilesize, fp) == newloaderfilesize) {
    printf("write new loader file success,size=%d \n", newloaderfilesize);
  } else {
    printf("write new loader file fail!\n");
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    -0x100040(%rbp),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1070 <memset@plt>
lea    -0x200040(%rbp),%rdi
xor    %esi,%esi
mov    $0x100000,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1243 <main+0xc3>
mov    -0x18(%rbp),%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <main+0x185>
lea    -0x100040(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x28(%rbp),%rax
jne    1282 <main+0x102>
lea    -0x100040(%rbp),%rdi
lea    -0x200040(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <make_loader_data@plt>
jmp    1290 <main+0x110>
lea    0xdd2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
lea    -0x200040(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x30(%rbp),%rax
jne    12e7 <main+0x167>
mov    -0x30(%rbp),%rsi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f5 <main+0x175>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x200040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200018,%rsp
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
lea    0x100010(%rsp),%rdi
mov    $0x100000,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0x10(%rsp),%rdi
mov    $0x100000,%edx
xor    %esi,%esi
call   1080 <memset@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
mov    0x8(%rsp),%r12
lea    0xe1c(%rip),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe45(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     123f <main+0xaf>
mov    %rax,%r15
lea    0x100010(%rsp),%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rax,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1257 <main+0xc7>
lea    0x100010(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
call   1070 <make_loader_data@plt>
jmp    1263 <main+0xd3>
lea    0xdfd(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12c2 <main+0x132>
lea    0xe29(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
lea    0xde9(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    (%rsp),%rdx
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   10b0 <fwrite@plt>
cmp    (%rsp),%rax
jne    12ac <main+0x11c>
lea    0xdbf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12b8 <main+0x128>
lea    0xdef(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x200018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [64 x i8] c"oldloaderfilename=%s,oldloaderfilesize=%d,newloaderfilename=%s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Can not open file %s !\0A\00"
@.str.3 = [28 x i8] c"read old loader file fail!\0A\00"
@.str.4 = [4 x i8] c"w+b\00"
@.str.5 = [40 x i8] c"write new loader file success,size=%d \0A\00"
@.str.6 = [29 x i8] c"write new loader file fail!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32*
%11 = alloca %struct.stat
%12 = alloca [1048576 x i8]
%13 = alloca [1048576 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
store i8* %19, i8** %7
%20 = bitcast [1048576 x i8]* %12 to i8*
%21 = bitcast [1048576 x i8]* %13 to i8*
%22 = load i8*, i8** %6
%23 = call i32 @stat(i8* %22, %struct.stat* %11)
%24 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%25 = load i64, i64* %24
store i64 %25, i64* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %8
%28 = load i8*, i8** %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %26, i64 %27, i8* %28)
%30 = load i8*, i8** %6
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %10
%32 = load i32*, i32** %10
%33 = icmp ne i32* %32, null
br i1 %33, label %37, label %34
34: ; preds = %2
%35 = load i8*, i8** %6
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %35)
store i32 -1, i32* %3
br label %69
37: ; preds = %2
%38 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %12, i64 0, i64 0
%39 = load i64, i64* %8
%40 = load i32*, i32** %10
%41 = call i64 @fread(i8* %38, i32 1, i64 %39, i32* %40)
%42 = load i64, i64* %8
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %12, i64 0, i64 0
%46 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %13, i64 0, i64 0
%47 = call i32 @make_loader_data(i8* %45, i8* %46, i64* %9)
br label %50
48: ; preds = %37
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i32*, i32** %10
%52 = call i32 @fclose(i32* %51)
%53 = load i8*, i8** %7
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %10
%55 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %13, i64 0, i64 0
%56 = load i64, i64* %9
%57 = load i32*, i32** %10
%58 = call i64 @fwrite(i8* %55, i32 1, i64 %56, i32* %57)
%59 = load i64, i64* %9
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %50
%62 = load i64, i64* %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i64 %62)
br label %66
64: ; preds = %50
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
%67 = load i32*, i32** %10
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %3
br label %69
69: ; preds = %66, %34
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [64 x i8] c"oldloaderfilename=%s,oldloaderfilesize=%d,newloaderfilename=%s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [24 x i8] c"Can not open file %s !\0A\00"
@.str.4 = [4 x i8] c"w+b\00"
@.str.5 = [40 x i8] c"write new loader file success,size=%d \0A\00"
@str = [27 x i8] c"read old loader file fail!\00"
@str.7 = [28 x i8] c"write new loader file fail!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i64
%4 = alloca %struct.stat
%5 = alloca [1048576 x i8]
%6 = alloca [1048576 x i8]
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = bitcast i64* %3 to i8*
%12 = bitcast %struct.stat* %4 to i8*
%13 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %5, i64 0, i64 0
%14 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %6, i64 0, i64 0
%15 = call i32 @stat(i8* %8, %struct.stat* %4)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i64, i64* %16
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %8, i64 %17, i8* %10)
%19 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %43
23: ; preds = %2
%24 = call i64 @fread(i8* %13, i32 1, i64 %17, i32* %19)
%25 = icmp eq i64 %24, %17
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @make_loader_data(i8* %13, i8* %14, i64* %3)
br label %30
28: ; preds = %23
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
%31 = call i32 @fclose(i32* %19)
%32 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%33 = load i64, i64* %3
%34 = call i64 @fwrite(i8* %14, i32 1, i64 %33, i32* %32)
%35 = load i64, i64* %3
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i64 %34)
br label %41
39: ; preds = %30
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
%42 = call i32 @fclose(i32* %32)
br label %43
43: ; preds = %41, %21
%44 = phi i32 [ 0, %41 ], [ -1, %21 ]
ret i32 %44
}