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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(int /*<<< orphan*/ *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int /*<<< orphan*/ stderr;
void ReadFile(const char *filename, uint8_t **result, size_t *result_size) {
  FILE *fp;
  uint8_t *data;
  size_t file_size;
  size_t got;
  fp = fopen(filename, "rb");
  if (fp == NULL) {
    fprintf(stderr, "Failed to open input file: %s\n", filename);
    exit(1);
  }
  fseek(fp, 0, SEEK_END);
  file_size = ftell(fp);
  data = malloc(file_size);
  if (data == NULL) {
    fprintf(stderr, "Unable to create memory image of input file: %s\n",
            filename);
    exit(1);
  }
  fseek(fp, 0, SEEK_SET);
  got = fread(data, 1, file_size, fp);
  if (got != file_size) {
    fprintf(stderr, "Unable to read data from input file: %s\n", filename);
    exit(1);
  }
  fclose(fp);
  *result = data;
  *result_size = file_size;
}


x86 assembly (clang, -O0):
<ReadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <ReadFile+0x56>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1232 <ReadFile+0xb2>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdff(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1291 <ReadFile+0x111>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd1(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     121f <ReadFile+0x9f>
mov    %rax,%rbx
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1231 <ReadFile+0xb1>
mov    %rax,%rbp
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r13,%rax
jne    1243 <ReadFile+0xc3>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rbp,(%r12)
mov    %r13,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
jmp    1253 <ReadFile+0xd3>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
jmp    1253 <ReadFile+0xd3>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Failed to open input file: %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [49 x i8] c"Unable to create memory image of input file: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [41 x i8] c"Unable to read data from input file: %s\0A\00"
define void @ReadFile(i8* %0, i32** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i32**
%6 = alloca i64*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
store i8* %0, i8** %4
store i32** %1, i32*** %5
store i64* %2, i64** %6
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %7
%13 = load i32*, i32** %7
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i32*, i32** %7
%22 = load i32, i32* @SEEK_END
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i32*, i32** %7
%25 = call i64 @ftell(i32* %24)
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = call i32* @malloc(i64 %26)
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %4
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %20
%36 = load i32*, i32** %7
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @fseek(i32* %36, i32 0, i32 %37)
%39 = load i32*, i32** %8
%40 = load i64, i64* %9
%41 = load i32*, i32** %7
%42 = call i64 @fread(i32* %39, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %10
%43 = load i64, i64* %10
%44 = load i64, i64* %9
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %35
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %4
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %35
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %8
%55 = load i32**, i32*** %5
store i32* %54, i32** %55
%56 = load i64, i64* %9
%57 = load i64*, i64** %6
store i64 %56, i64* %57
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Failed to open input file: %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [49 x i8] c"Unable to create memory image of input file: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [41 x i8] c"Unable to read data from input file: %s\0A\00"
define void @ReadFile(i8* %0, i32** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %4, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %4)
%14 = tail call i32* @malloc(i64 %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @fseek(i32* %4, i32 0, i32 %21)
%23 = tail call i64 @fread(i32* %14, i32 1, i64 %13, i32* %4)
%24 = icmp eq i64 %23, %13
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = tail call i32 @fclose(i32* %4)
store i32* %14, i32** %1
store i64 %13, i64* %2
ret void
}