#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 */
long _GetFileSize(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int ReadEntireFile(char *filename, char *mode, char *buf, long *bufsize) {
  FILE *fp = NULL;
  long filesize;
  long lret;
  if (filename == NULL)
    return -1;
  if (strcmp(filename, "") == 0)
    return -1;
  filesize = _GetFileSize(filename);
  if (filesize < 0)
    return -2;
  fp = fopen(filename, mode);
  if (fp == NULL)
    return -3;
  if (filesize <= (*bufsize)) {
    lret = fread(buf, sizeof(char), filesize, fp);
    if (lret < filesize) {
      fclose(fp);
      return -4;
    }
    (*bufsize) = filesize;
    fclose(fp);
    return 0;
  } else {
    lret = fread(buf, sizeof(char), (*bufsize), fp);
    if (lret < (*bufsize)) {
      fclose(fp);
      return -4;
    }
    fclose(fp);
    return 1;
  }
}


x86 assembly (clang, -O0):
<ReadEntireFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1187 <ReadEntireFile+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x10(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11ad <ReadEntireFile+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x10(%rbp),%rdi
call   1060 <_GetFileSize@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11d1 <ReadEntireFile+0x81>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <ReadEntireFile+0xa9>
movl   $0xfffffffd,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jg     1267 <ReadEntireFile+0x117>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    1247 <ReadEntireFile+0xf7>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%rax
jge    12aa <ReadEntireFile+0x15a>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    12ba <ReadEntireFile+0x16a>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadEntireFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1214 <ReadEntireFile+0xc4>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe87(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1214 <ReadEntireFile+0xc4>
mov    %rbx,%rdi
call   1060 <_GetFileSize@plt>
test   %rax,%rax
js     11da <ReadEntireFile+0x8a>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e1 <ReadEntireFile+0x91>
mov    %rax,%r15
mov    (%r14),%rdx
mov    %r12,%rdi
mov    $0x1,%esi
cmp    %rdx,%rbp
jle    11e8 <ReadEntireFile+0x98>
mov    %r15,%rcx
call   1030 <fread@plt>
xor    %ebx,%ebx
cmp    (%r14),%rax
setge  %bl
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    (%rbx,%rbx,4),%ebp
add    $0xfffffffc,%ebp
jmp    1214 <ReadEntireFile+0xc4>
mov    $0xfffffffe,%ebp
jmp    1214 <ReadEntireFile+0xc4>
mov    $0xfffffffd,%ebp
jmp    1214 <ReadEntireFile+0xc4>
mov    %rbp,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %rbp,%rax
jge    1207 <ReadEntireFile+0xb7>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0xfffffffc,%ebp
jmp    1214 <ReadEntireFile+0xc4>
mov    %rbp,(%r14)
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i32 @ReadEntireFile(i8* %0, i8* %1, i8* %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64* %3, i64** %9
store i32* null, i32** %10
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %71
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -1, i32* %5
br label %71
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = call i64 @_GetFileSize(i8* %22)
store i64 %23, i64* %11
%24 = load i64, i64* %11
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %21
store i32 -2, i32* %5
br label %71
27: ; preds = %21
%28 = load i8*, i8** %6
%29 = load i8*, i8** %7
%30 = call i32* @fopen(i8* %28, i8* %29)
store i32* %30, i32** %10
%31 = load i32*, i32** %10
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 -3, i32* %5
br label %71
34: ; preds = %27
%35 = load i64, i64* %11
%36 = load i64*, i64** %9
%37 = load i64, i64* %36
%38 = icmp sle i64 %35, %37
br i1 %38, label %39, label %55
39: ; preds = %34
%40 = load i8*, i8** %8
%41 = load i64, i64* %11
%42 = load i32*, i32** %10
%43 = call i64 @fread(i8* %40, i32 1, i64 %41, i32* %42)
store i64 %43, i64* %12
%44 = load i64, i64* %12
%45 = load i64, i64* %11
%46 = icmp slt i64 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = load i32*, i32** %10
%49 = call i32 @fclose(i32* %48)
store i32 -4, i32* %5
br label %71
50: ; preds = %39
%51 = load i64, i64* %11
%52 = load i64*, i64** %9
store i64 %51, i64* %52
%53 = load i32*, i32** %10
%54 = call i32 @fclose(i32* %53)
store i32 0, i32* %5
br label %71
55: ; preds = %34
%56 = load i8*, i8** %8
%57 = load i64*, i64** %9
%58 = load i64, i64* %57
%59 = load i32*, i32** %10
%60 = call i64 @fread(i8* %56, i32 1, i64 %58, i32* %59)
store i64 %60, i64* %12
%61 = load i64, i64* %12
%62 = load i64*, i64** %9
%63 = load i64, i64* %62
%64 = icmp slt i64 %61, %63
br i1 %64, label %65, label %68
65: ; preds = %55
%66 = load i32*, i32** %10
%67 = call i32 @fclose(i32* %66)
store i32 -4, i32* %5
br label %71
68: ; preds = %55
%69 = load i32*, i32** %10
%70 = call i32 @fclose(i32* %69)
store i32 1, i32* %5
br label %71
71: ; preds = %68, %65, %50, %47, %33, %26, %20, %15
%72 = load i32, i32* %5
ret i32 %72
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i32 @ReadEntireFile(i8* %0, i8* nocapture %1, i8* nocapture %2, i64* nocapture %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %31, label %6
6: ; preds = %4
%7 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %31, label %9
9: ; preds = %6
%10 = tail call i64 @_GetFileSize(i8* %0)
%11 = icmp slt i64 %10, 0
br i1 %11, label %31, label %12
12: ; preds = %9
%13 = tail call i32* @fopen(i8* %0, i8* %1)
%14 = icmp eq i32* %13, null
br i1 %14, label %31, label %15
15: ; preds = %12
%16 = load i64, i64* %3
%17 = icmp sgt i64 %10, %16
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = tail call i64 @fread(i8* %2, i32 1, i64 %10, i32* %13)
%20 = icmp slt i64 %19, %10
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @fclose(i32* %13)
br label %31
23: ; preds = %18
store i64 %10, i64* %3
%24 = tail call i32 @fclose(i32* %13)
br label %31
25: ; preds = %15
%26 = tail call i64 @fread(i8* %2, i32 1, i64 %16, i32* %13)
%27 = load i64, i64* %3
%28 = icmp slt i64 %26, %27
%29 = tail call i32 @fclose(i32* %13)
%30 = select i1 %28, i32 -4, i32 1
br label %31
31: ; preds = %25, %12, %9, %6, %4, %23, %21
%32 = phi i32 [ -4, %21 ], [ 0, %23 ], [ -1, %4 ], [ -1, %6 ], [ -2, %9 ], [ -3, %12 ], [ %30, %25 ]
ret i32 %32
}