#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 *);
int fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
unsigned char *malloc(long);
int file_read(const char *file, unsigned char **buf, unsigned int *length) {
  FILE *fd = NULL;
  fd = fopen(file, "rb");
  if (fd == NULL) {
    return -1;
  }
  fseek(fd, 0, SEEK_END);
  long size = ftell(fd);
  fseek(fd, 0, SEEK_SET);
  unsigned char *data = malloc(size);
  int bytes = fread(data, 1, size, fd);
  if (bytes != size) {
    fclose(fd);
    return -1;
  }
  fclose(fd);
  *buf = data;
  *length = bytes;
  return bytes;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a7 <file_read+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <file_read+0xe8>
mov    -0x28(%rbp),%rdi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1225 <file_read+0xc5>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <file_read+0xe8>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     120f <file_read+0xaf>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %rax,%r14
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
movslq %eax,%r15
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    %r15,%r14
jne    120f <file_read+0xaf>
mov    0x8(%rsp),%rax
mov    %r13,(%rax)
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
mov    %ebp,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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