#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(char *, int, int, int /*<<< orphan*/ *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int fs_read_file(const char *file_name, char **buffer) {
  FILE *file;
  int length;
  file = fopen(file_name, "rb");
  if (file == NULL) {
    return -1;
  }
  if (fseek(file, 0, SEEK_END) == -1) {
    fclose(file);
    return -1;
  }
  if ((length = ftell(file)) == -1) {
    fclose(file);
    return -1;
  }
  if (fseek(file, 0, SEEK_SET) == -1) {
    fclose(file);
    return -1;
  }
  *buffer = malloc(length);
  if (*buffer == NULL) {
    fclose(file);
    return -1;
  }
  length = fread(*buffer, 1, length, file);
  fclose(file);
  return length;
}


x86 assembly (clang, -O0):
<fs_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119b <fs_read_file+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <fs_read_file+0x128>
mov    -0x20(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
jne    11cd <fs_read_file+0x6d>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <fs_read_file+0x128>
mov    -0x20(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11f7 <fs_read_file+0x97>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <fs_read_file+0x128>
mov    -0x20(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
cmp    $0xffffffff,%eax
jne    1229 <fs_read_file+0xc9>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <fs_read_file+0x128>
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    125e <fs_read_file+0xfe>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <fs_read_file+0x128>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

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

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