#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fopen_s(int /*<<< orphan*/ **, char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, unsigned long,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(void *, char, int);
int /*<<< orphan*/ printf(char *, ...);
void loader_read(char *infile, unsigned char **buffer,
                 unsigned long *buffer_len) {
  unsigned long len;
  unsigned char *buffer1 = NULL;
  FILE *fp;
  *buffer = NULL;
  *buffer_len = 0;
  fopen_s(&fp, infile, "rb");
  if (!fp) {
    printf(" [LOADER]Error : could't open file (%s) !\n", infile);
    return;
  }
  fseek(fp, 0, SEEK_END);
  len = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  printf(" [LOADER]Read %d bytes from disk.\n", len);
  if (len > (8 * 1024)) {
    printf(" [LOADER]Error : file length is too big (%d bytes, max = %d bytes) "
           "!\n",
           len, (8 * 1024));
    fclose(fp);
    fp = NULL;
    return;
  }
  buffer1 = (unsigned char *)malloc((1024 * 1024 - 0x4000));
  if (!buffer1) {
    fclose(fp);
    fp = NULL;
    printf(" [LOADER]Alloc error#1 (%d bytes) !\n", (1024 * 1024 - 0x4000));
    return;
  }
  memset((void *)buffer1, 'P', (1024 * 1024 - 0x4000));
  fread(buffer1, 1, len, fp);
  fclose(fp);
  fp = NULL;
  *buffer = buffer1;
  *buffer_len = len;
}


x86 assembly (clang, -O0):
<loader_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
lea    -0x30(%rbp),%rdi
lea    0xe3f(%rip),%rdx
call   1030 <fopen_s@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11e8 <loader_read+0x68>
mov    -0x8(%rbp),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12fc <loader_read+0x17c>
mov    -0x30(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x2000,-0x20(%rbp)
jbe    126a <loader_read+0xea>
mov    -0x20(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x2000,%edx
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x30(%rbp)
jmp    12fc <loader_read+0x17c>
mov    $0xfc000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ac <loader_read+0x12c>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x30(%rbp)
lea    0xdfc(%rip),%rdi
mov    $0xfc000,%esi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12fc <loader_read+0x17c>
mov    -0x28(%rbp),%rdi
mov    $0x50,%esi
mov    $0xfc000,%edx
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loader_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movq   $0x0,(%rsi)
movq   $0x0,(%rdx)
lea    0xe5a(%rip),%rdx
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1030 <fopen_s@plt>
mov    (%rsp),%rdi
test   %rdi,%rdi
je     1228 <loader_read+0xa8>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    (%rsp),%rdi
call   1080 <ftell@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0xe3d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x2001,%rbx
jb     123b <loader_read+0xbb>
lea    0xe46(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2000,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
jmp    12a8 <loader_read+0x128>
lea    0xdd4(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12a8 <loader_read+0x128>
mov    $0xfc000,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1284 <loader_read+0x104>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x50,%esi
mov    $0xfc000,%edx
call   1070 <memset@plt>
mov    (%rsp),%rcx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1040 <fread@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
mov    %r12,(%r15)
mov    %rbx,(%r14)
jmp    12a8 <loader_read+0x128>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
movq   $0x0,(%rsp)
lea    0xdfb(%rip),%rdi
mov    $0xfc000,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [43 x i8] c" [LOADER]Error : could't open file (%s) !\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [35 x i8] c" [LOADER]Read %d bytes from disk.\0A\00"
@.str.3 = [70 x i8] c" [LOADER]Error : file length is too big (%d bytes, max = %d bytes) !\0A\00"
@.str.4 = [37 x i8] c" [LOADER]Alloc error (%d bytes) !\0A\00"
define void @loader_read(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
store i8* null, i8** %8
%10 = load i8**, i8*** %5
store i8* null, i8** %10
%11 = load i64*, i64** %6
store i64 0, i64* %11
%12 = load i8*, i8** %4
%13 = call i32 @fopen_s(i32** %9, i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = load i32*, i32** %9
%15 = icmp ne i32* %14, null
br i1 %15, label %19, label %16
16: ; preds = %3
%17 = load i8*, i8** %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %59
19: ; preds = %3
%20 = load i32*, i32** %9
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i32*, i32** %9
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %7
%25 = load i32*, i32** %9
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i64, i64* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = load i64, i64* %7
%31 = icmp ugt i64 %30, 8192
br i1 %31, label %32, label %37
32: ; preds = %19
%33 = load i64, i64* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i64 %33, i32 8192)
%35 = load i32*, i32** %9
%36 = call i32 @fclose(i32* %35)
store i32* null, i32** %9
br label %59
37: ; preds = %19
%38 = call i64 @malloc(i32 1032192)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %8
%40 = load i8*, i8** %8
%41 = icmp ne i8* %40, null
br i1 %41, label %46, label %42
42: ; preds = %37
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
store i32* null, i32** %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 1032192)
br label %59
46: ; preds = %37
%47 = load i8*, i8** %8
%48 = call i32 @memset(i8* %47, i8 signext 80, i32 1032192)
%49 = load i8*, i8** %8
%50 = load i64, i64* %7
%51 = load i32*, i32** %9
%52 = call i32 @fread(i8* %49, i32 1, i64 %50, i32* %51)
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
store i32* null, i32** %9
%55 = load i8*, i8** %8
%56 = load i8**, i8*** %5
store i8* %55, i8** %56
%57 = load i64, i64* %7
%58 = load i64*, i64** %6
store i64 %57, i64* %58
br label %59
59: ; preds = %46, %42, %32, %16
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [43 x i8] c" [LOADER]Error : could't open file (%s) !\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [35 x i8] c" [LOADER]Read %d bytes from disk.\0A\00"
@.str.3 = [70 x i8] c" [LOADER]Error : file length is too big (%d bytes, max = %d bytes) !\0A\00"
@.str.4 = [37 x i8] c" [LOADER]Alloc error (%d bytes) !\0A\00"
define void @loader_read(i8* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = alloca i32*
%5 = bitcast i32** %4 to i8*
store i8* null, i8** %1
store i64 0, i64* %2
%6 = call i32 @fopen_s(i32** %4, i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
11: ; preds = %3
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32* %7, i32 0, i32 %12)
%14 = load i32*, i32** %4
%15 = call i64 @ftell(i32* %14)
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%20 = icmp ugt i64 %15, 8192
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i64 %15, i32 8192)
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
br label %39
25: ; preds = %11
%26 = call i64 @malloc(i32 1032192)
%27 = inttoptr i64 %26 to i8*
%28 = icmp eq i64 %26, 0
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
store i32* null, i32** %4
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 1032192)
br label %39
33: ; preds = %25
%34 = call i32 @memset(i8* %27, i8 signext 80, i32 1032192)
%35 = load i32*, i32** %4
%36 = call i32 @fread(i8* %27, i32 1, i64 %15, i32* %35)
%37 = load i32*, i32** %4
%38 = call i32 @fclose(i32* %37)
store i8* %27, i8** %1
store i64 %15, i64* %2
br label %39
39: ; preds = %33, %29, %21, %9
ret void
}