#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(size_t);
int read_file(uint8_t **buf, size_t *size, const char *path) {
  FILE *fp;
  fp = fopen(path, "rb");
  if (fp == NULL)
    return -1;
  fseek(fp, 0, SEEK_END);
  *size = ftell(fp);
  *buf = malloc(*size);
  if (*buf == NULL) {
    fclose(fp);
    return -1;
  }
  fseek(fp, 0, SEEK_SET);
  if (fread(*buf, *size, 1, fp) != 1) {
    fclose(fp);
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119f <read_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <read_file+0xfd>
mov    -0x28(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x28(%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    11ff <read_file+0x9f>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <read_file+0xfd>
mov    -0x28(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     124d <read_file+0xed>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <read_file+0xfd>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8c(%rip),%rsi
mov    %rdx,%rdi
call   1080 <fopen@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11fa <read_file+0x9a>
mov    %rax,%rbx
mov    0x2e58(%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    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11f2 <read_file+0x92>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r15),%rdi
mov    (%r14),%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %ebp,%ebp
cmp    $0x1,%eax
setne  %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
neg    %ebp
jmp    11fa <read_file+0x9a>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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 @read_file(i32** %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i32*
store i32** %0, i32*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %48
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %8
%19 = call i64 @ftell(i32* %18)
%20 = load i64*, i64** %6
store i64 %19, i64* %20
%21 = load i64*, i64** %6
%22 = load i64, i64* %21
%23 = call i32* @malloc(i64 %22)
%24 = load i32**, i32*** %5
store i32* %23, i32** %24
%25 = load i32**, i32*** %5
%26 = load i32*, i32** %25
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %14
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
store i32 -1, i32* %4
br label %48
31: ; preds = %14
%32 = load i32*, i32** %8
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i32**, i32*** %5
%36 = load i32*, i32** %35
%37 = load i64*, i64** %6
%38 = load i64, i64* %37
%39 = load i32*, i32** %8
%40 = call i32 @fread(i32* %36, i64 %38, i32 1, i32* %39)
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %45
42: ; preds = %31
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
store i32 -1, i32* %4
br label %48
45: ; preds = %31
%46 = load i32*, i32** %8
%47 = call i32 @fclose(i32* %46)
store i32 0, i32* %4
br label %48
48: ; preds = %45, %42, %28, %13
%49 = load i32, i32* %4
ret i32 %49
}

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