#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 *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int read_file(const char *file_name, unsigned char **data) {
  FILE *in;
  unsigned char *in_buf = NULL;
  size_t file_size;
  size_t bytes_read;
  in = fopen(file_name, "rb");
  if (in == NULL) {
    return -1;
  }
  fseek(in, 0, SEEK_END);
  file_size = ftell(in);
  fseek(in, 0, SEEK_SET);
  in_buf = malloc(file_size);
  bytes_read = fread(in_buf, 1, file_size, in);
  if (bytes_read != file_size) {
    return -2;
  }
  fclose(in);
  *data = in_buf;
  return (int)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    11a3 <read_file+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <read_file+0xd4>
mov    -0x20(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
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     1219 <read_file+0xb9>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1234 <read_file+0xd4>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
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     11eb <read_file+0x8b>
mov    %rax,%rbx
mov    0x2e62(%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    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,%rcx
mov    $0xfffffffe,%eax
cmp    %r15,%rcx
jne    11f0 <read_file+0x90>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12,(%r14)
mov    %r15d,%eax
jmp    11f0 <read_file+0x90>
mov    $0xffffffff,%eax
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 i32 @read_file(i8* %0, i8** %1) {
%3 = alloca i32
%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 i32 -1, i32* %3
br label %41
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 i32*, i32** %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64, i64* %8
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = load i32*, i32** %6
%29 = call i64 @fread(i8* %26, i32 1, i64 %27, i32* %28)
store i64 %29, i64* %9
%30 = load i64, i64* %9
%31 = load i64, i64* %8
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %15
store i32 -2, i32* %3
br label %41
34: ; preds = %15
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %7
%38 = load i8**, i8*** %5
store i8* %37, i8** %38
%39 = load i64, i64* %9
%40 = trunc i64 %39 to i32
store i32 %40, i32* %3
br label %41
41: ; preds = %34, %33, %14
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @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 %17, 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 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i32 0, i32 %9)
%11 = tail call i8* @malloc(i64 %8)
%12 = tail call i64 @fread(i8* %11, i32 1, i64 %8, i32* %3)
%13 = icmp eq i64 %12, %8
br i1 %13, label %14, label %17
14: ; preds = %5
%15 = tail call i32 @fclose(i32* %3)
store i8* %11, i8** %1
%16 = trunc i64 %8 to i32
br label %17
17: ; preds = %5, %2, %14
%18 = phi i32 [ %16, %14 ], [ -1, %2 ], [ -2, %5 ]
ret i32 %18
}