#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 *, char *);
size_t fread(unsigned char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
unsigned char *malloc(size_t);
int read_from_file(char *filename, unsigned char **bytes,
                   size_t *bytes_length) {
  FILE *fptr;
  size_t size;
  fptr = fopen(filename, "rb");
  if (!fptr)
    return -1;
  fseek(fptr, 0, SEEK_END);
  *bytes_length = ftell(fptr);
  fseek(fptr, 0, SEEK_SET);
  *bytes = malloc(*bytes_length + 1);
  if (!*bytes) {
    fclose(fptr);
    return -1;
  }
  (*bytes)[*bytes_length] = '\0';
  size = fread(*bytes, *bytes_length, 1, fptr);
  if (size != 1) {
    fclose(fptr);
    return -1;
  }
  fclose(fptr);
  return 0;
}


x86 assembly (clang, -O0):
<read_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119f <read_from_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <read_from_file+0x119>
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    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1217 <read_from_file+0xb7>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <read_from_file+0x119>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
je     1269 <read_from_file+0x109>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <read_from_file+0x119>
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_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1204 <read_from_file+0xa4>
mov    %rax,%rbx
mov    0x2e59(%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    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r15),%rbp
lea    0x1(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11fc <read_from_file+0x9c>
movb   $0x0,(%rax,%rbp,1)
mov    %rax,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
xor    %r12d,%r12d
cmp    $0x1,%rax
setne  %r12b
mov    %rbx,%rdi
call   1040 <fclose@plt>
neg    %r12d
jmp    1204 <read_from_file+0xa4>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
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_from_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
store i32 -1, i32* %4
br label %56
15: ; preds = %3
%16 = load i32*, i32** %8
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %8
%20 = call i64 @ftell(i32* %19)
%21 = load i64*, i64** %7
store i64 %20, i64* %21
%22 = load i32*, i32** %8
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i64*, i64** %7
%26 = load i64, i64* %25
%27 = add i64 %26, 1
%28 = call i8* @malloc(i64 %27)
%29 = load i8**, i8*** %6
store i8* %28, i8** %29
%30 = load i8**, i8*** %6
%31 = load i8*, i8** %30
%32 = icmp ne i8* %31, null
br i1 %32, label %36, label %33
33: ; preds = %15
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
store i32 -1, i32* %4
br label %56
36: ; preds = %15
%37 = load i8**, i8*** %6
%38 = load i8*, i8** %37
%39 = load i64*, i64** %7
%40 = load i64, i64* %39
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i8**, i8*** %6
%43 = load i8*, i8** %42
%44 = load i64*, i64** %7
%45 = load i64, i64* %44
%46 = load i32*, i32** %8
%47 = call i64 @fread(i8* %43, i64 %45, i32 1, i32* %46)
store i64 %47, i64* %9
%48 = load i64, i64* %9
%49 = icmp ne i64 %48, 1
br i1 %49, label %50, label %53
50: ; preds = %36
%51 = load i32*, i32** %8
%52 = call i32 @fclose(i32* %51)
store i32 -1, i32* %4
br label %56
53: ; preds = %36
%54 = load i32*, i32** %8
%55 = call i32 @fclose(i32* %54)
store i32 0, i32* %4
br label %56
56: ; preds = %53, %50, %33, %14
%57 = load i32, i32* %4
ret i32 %57
}

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