#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(char const *, char *);
long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
unsigned char *malloc(long);
long read_file(const char *file_name, unsigned char **data) {
  FILE *in;
  unsigned char *in_buf = NULL;
  long file_size;
  long bytes_read;
  in = fopen(file_name, "rb");
  if (in == NULL) {
    return -1;
  }
  fseek(in, 0, SEEK_END);
  file_size = ftell(in);
  if (file_size > 256 * (1024 * 1024)) {
    return -2;
  }
  in_buf = malloc(file_size);
  fseek(in, 0, SEEK_SET);
  bytes_read = fread(in_buf, 1, file_size, in);
  if (bytes_read != file_size) {
    return -3;
  }
  fclose(in);
  *data = in_buf;
  return bytes_read;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a4 <read_file+0x44>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1252 <read_file+0xf2>
mov    -0x20(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x10000000,-0x30(%rbp)
10
jle    11e0 <read_file+0x80>
movq   $0xfffffffffffffffe,-0x8(%rbp)
ff
jmp    1252 <read_file+0xf2>
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <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     1236 <read_file+0xd6>
movq   $0xfffffffffffffffd,-0x8(%rbp)
ff
jmp    1252 <read_file+0xf2>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe8e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1201 <read_file+0xa1>
mov    %rax,%rbx
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    $0xfffffffffffffffe,%rax
cmp    $0x10000000,%r15
jg     1208 <read_file+0xa8>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%rcx
mov    $0xfffffffffffffffd,%rax
cmp    %r15,%rcx
jne    1208 <read_file+0xa8>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
mov    %r15,%rax
jmp    1208 <read_file+0xa8>
mov    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @read_file(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8* null, i8** %7
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i64 -1, i64* %3
br label %44
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %6
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp sgt i64 %21, 268435456
br i1 %22, label %23, label %24
23: ; preds = %15
store i64 -2, i64* %3
br label %44
24: ; preds = %15
%25 = load i64, i64* %8
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %7
%27 = load i32*, i32** %6
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i8*, i8** %7
%31 = load i64, i64* %8
%32 = load i32*, i32** %6
%33 = call i64 @fread(i8* %30, i32 1, i64 %31, i32* %32)
store i64 %33, i64* %9
%34 = load i64, i64* %9
%35 = load i64, i64* %8
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %24
store i64 -3, i64* %3
br label %44
38: ; preds = %24
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %7
%42 = load i8**, i8*** %5
store i8* %41, i8** %42
%43 = load i64, i64* %9
store i64 %43, i64* %3
br label %44
44: ; preds = %38, %37, %23, %14
%45 = load i64, i64* %3
ret i64 %45
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @read_file(i8* nocapture %0, i8** nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = icmp sgt i64 %8, 268435456
br i1 %9, label %18, label %10
10: ; preds = %5
%11 = tail call i8* @malloc(i64 %8)
%12 = load i32, i32* @SEEK_SET
%13 = tail call i32 @fseek(i32* %3, i32 0, i32 %12)
%14 = tail call i64 @fread(i8* %11, i32 1, i64 %8, i32* %3)
%15 = icmp eq i64 %14, %8
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @fclose(i32* %3)
store i8* %11, i8** %1
br label %18
18: ; preds = %10, %5, %2, %16
%19 = phi i64 [ %8, %16 ], [ -1, %2 ], [ -2, %5 ], [ -3, %10 ]
ret i64 %19
}