#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ load_file(int /*<<< orphan*/ *, unsigned char *,
                             unsigned long);
unsigned char *malloc(unsigned long);
int /*<<< orphan*/ stderr;
void read_file(char *filename, unsigned char **ptr, unsigned long *sz) {
  FILE *fh = fopen(filename, "rb");
  if (fh == NULL) {
    fprintf(stderr, "Can't open \"%s\".\n", filename);
    exit(1);
  }
  if (fseek(fh, 0, SEEK_END) < 0) {
    fprintf(stderr, "Seek error.\n");
    exit(1);
  }
  *sz = ftell(fh);
  *ptr = malloc(*sz);
  if (*ptr == NULL) {
    fprintf(stderr, "Out of memory.\n");
    exit(1);
  }
  if (fseek(fh, 0, SEEK_SET) < 0) {
    fprintf(stderr, "Seek error.\n");
    exit(1);
  }
  load_file(fh, *ptr, *sz);
  fclose(fh);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    11d8 <read_file+0x58>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
jge    1217 <read_file+0x97>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    126f <read_file+0xef>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0x0,%rax
jge    12ae <read_file+0x12e>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
call   1080 <load_file@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1227 <read_file+0xa7>
mov    %rax,%rbx
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
js     1215 <read_file+0x95>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     124b <read_file+0xcb>
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %rax,%rax
js     1215 <read_file+0x95>
mov    (%r15),%rsi
mov    (%r14),%rdx
mov    %rbx,%rdi
call   1080 <load_file@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
jmp    125b <read_file+0xdb>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1040 <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 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Seek error.\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
@SEEK_SET = global i32 0
define void @read_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %4
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32*, i32** %7
%28 = call i64 @ftell(i32* %27)
%29 = load i64*, i64** %6
store i64 %28, i64* %29
%30 = load i64*, i64** %6
%31 = load i64, i64* %30
%32 = call i8* @malloc(i64 %31)
%33 = load i8**, i8*** %5
store i8* %32, i8** %33
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %26
%42 = load i32*, i32** %7
%43 = load i32, i32* @SEEK_SET
%44 = call i64 @fseek(i32* %42, i32 0, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %41
%51 = load i32*, i32** %7
%52 = load i8**, i8*** %5
%53 = load i8*, i8** %52
%54 = load i64*, i64** %6
%55 = load i64, i64* %54
%56 = call i32 @load_file(i32* %51, i8* %53, i64 %55)
%57 = load i32*, i32** %7
%58 = call i32 @fclose(i32* %57)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"Can't open \22%s\22.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [13 x i8] c"Seek error.\0A\00"
@.str.3 = [16 x i8] c"Out of memory.\0A\00"
@SEEK_SET = global i32 0
define void @read_file(i8* %0, i8** nocapture %1, i64* nocapture %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 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 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 i64 @fseek(i32* %4, i32 0, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = tail call i64 @ftell(i32* %4)
store i64 %19, i64* %2
%20 = tail call i8* @malloc(i64 %19)
store i8* %20, i8** %1
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = load i32, i32* @SEEK_SET
%28 = tail call i64 @fseek(i32* %4, i32 0, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i8*, i8** %1
%36 = load i64, i64* %2
%37 = tail call i32 @load_file(i32* %4, i8* %35, i64 %36)
%38 = tail call i32 @fclose(i32* %4)
ret void
}