#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 */
struct tbVM {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ FCC;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, long, int, int /*<<< orphan*/ *);
char *malloc(long);
scalar_t__ memcmp(char const *, char *, int);
long tbGetFileSize(int /*<<< orphan*/ *);
int tbLoadBuffer(struct tbVM *, char *, long);
int tbLoadFile(struct tbVM *pVM, const char *FilePath) {
  long FileSize;
  char *RawFileBuffer;
  FILE *fFile;
  const char FCC[] = "BCCF";
  fFile = fopen(FilePath, "r");
  if (fFile == NULL) {
    return 0;
  }
  FileSize = tbGetFileSize(fFile);
  RawFileBuffer = malloc(FileSize);
  if (RawFileBuffer == NULL) {
    fclose(fFile);
    return 0;
  }
  if (fread(RawFileBuffer, FileSize, 1, fFile) == 0) {
    fclose(fFile);
    return 0;
  }
  if (memcmp(FCC, RawFileBuffer, sizeof(FCC) - 1) != 0) {
    fclose(fFile);
    return 0;
  }
  fclose(fFile);
  return tbLoadBuffer(pVM, &RawFileBuffer[sizeof(FCC) - 1],
                      FileSize - (sizeof(FCC) - 1));
}


x86 assembly (clang, -O0):
<tbLoadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0x35(%rbp)
mov    0xe75(%rip),%al
mov    %al,-0x31(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bd <tbLoadFile+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    1282 <tbLoadFile+0x112>
mov    -0x30(%rbp),%rdi
call   1070 <tbGetFileSize@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f7 <tbLoadFile+0x87>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <tbLoadFile+0x112>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
jne    122c <tbLoadFile+0xbc>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <tbLoadFile+0x112>
lea    -0x35(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     125d <tbLoadFile+0xed>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1282 <tbLoadFile+0x112>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rdx
sub    $0x4,%rdx
call   1050 <tbLoadBuffer@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tbLoadFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rax
mov    %rdi,%r14
movb   $0x0,0x4(%rsp)
movl   $0x46434342,(%rsp)
lea    0xe71(%rip),%rsi
mov    %rax,%rdi
call   1090 <fopen@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     121d <tbLoadFile+0xad>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <tbGetFileSize@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1215 <tbLoadFile+0xa5>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1215 <tbLoadFile+0xa5>
mov    %rsp,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
mov    %rax,%r13
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %r13,%r13
jne    121d <tbLoadFile+0xad>
add    $0x4,%rbx
add    $0xfffffffffffffffc,%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <tbLoadBuffer@plt>
mov    %eax,%r15d
jmp    121d <tbLoadFile+0xad>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tbVM = type { i32 }
@__const.tbLoadFile.FCC = [5 x i8] c"BCCF\00"
@.str = [2 x i8] c"r\00"
define i32 @tbLoadFile(%struct.tbVM* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.tbVM*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [5 x i8]
store %struct.tbVM* %0, %struct.tbVM** %4
store i8* %1, i8** %5
%10 = bitcast [5 x i8]* %9 to i8*
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %52
16: ; preds = %2
%17 = load i32*, i32** %8
%18 = call i64 @tbGetFileSize(i32* %17)
store i64 %18, i64* %6
%19 = load i64, i64* %6
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32*, i32** %8
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %3
br label %52
26: ; preds = %16
%27 = load i8*, i8** %7
%28 = load i64, i64* %6
%29 = load i32*, i32** %8
%30 = call i64 @fread(i8* %27, i64 %28, i32 1, i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %3
br label %52
35: ; preds = %26
%36 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%37 = load i8*, i8** %7
%38 = call i64 @memcmp(i8* %36, i8* %37, i32 4)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32*, i32** %8
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %3
br label %52
43: ; preds = %35
%44 = load i32*, i32** %8
%45 = call i32 @fclose(i32* %44)
%46 = load %struct.tbVM*, %struct.tbVM** %4
%47 = load i8*, i8** %7
%48 = getelementptr inbounds i8, i8* %47, i64 4
%49 = load i64, i64* %6
%50 = sub i64 %49, 4
%51 = call i32 @tbLoadBuffer(%struct.tbVM* %46, i8* %48, i64 %50)
store i32 %51, i32* %3
br label %52
52: ; preds = %43, %40, %32, %23, %15
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.tbVM = type { i32 }
@__const.tbLoadFile.FCC = [5 x i8] c"BCCF\00"
@.str = [2 x i8] c"r\00"
define i32 @tbLoadFile(%struct.tbVM* %0, i8* nocapture %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %26, label %7
7: ; preds = %2
%8 = tail call i64 @tbGetFileSize(i32* %5)
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @fclose(i32* %5)
br label %26
13: ; preds = %7
%14 = tail call i64 @fread(i8* %9, i64 %8, i32 1, i32* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @fclose(i32* %5)
br label %26
18: ; preds = %13
%19 = call i64 @memcmp(i8* %4, i8* %9, i32 4)
%20 = icmp eq i64 %19, 0
%21 = call i32 @fclose(i32* %5)
br i1 %20, label %22, label %26
22: ; preds = %18
%23 = getelementptr inbounds i8, i8* %9, i64 4
%24 = add i64 %8, -4
%25 = call i32 @tbLoadBuffer(%struct.tbVM* %0, i8* %23, i64 %24)
br label %26
26: ; preds = %18, %2, %22, %16, %11
%27 = phi i32 [ 0, %11 ], [ 0, %16 ], [ %25, %22 ], [ 0, %2 ], [ 0, %18 ]
ret i32 %27
}