#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 scalar_t__ uint32_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 *);
scalar_t__ fread(unsigned char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(scalar_t__);
unsigned char *qLoad(const char *romName, uint32_t *length) {
  FILE *inRom = NULL;
  unsigned char *romData;
  *length = 0;
  inRom = fopen(romName, "rb");
  if (!inRom) {
    return 0;
  }
  fseek(inRom, 0, SEEK_END);
  *length = ftell(inRom);
  fseek(inRom, 0, SEEK_SET);
  romData = (unsigned char *)malloc(*length);
  if (romData) {
    if (*length != fread(romData, 1, *length, inRom)) {
      free(romData);
      *length = 0;
      romData = 0;
    }
  }
  fclose(inRom);
  return romData;
}


x86 assembly (clang, -O0):
<qLoad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bf <qLoad+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    127b <qLoad+0x10b>
mov    -0x20(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     126a <qLoad+0xfa>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
je     1265 <qLoad+0xf5>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x28(%rbp)
jmp    126a <qLoad+0xfa>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qLoad>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
movq   $0x0,(%rsi)
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120d <qLoad+0x9d>
mov    %rax,%r15
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r14)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%r14),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1201 <qLoad+0x91>
mov    %rax,%rbx
mov    (%r14),%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %rax,%r12
je     1203 <qLoad+0x93>
mov    %rbx,%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
xor    %ebx,%ebx
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    120f <qLoad+0x9f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @qLoad(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32* null, i32** %6
%8 = load i64*, i64** %5
store i64 0, i64* %8
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i8* null, i8** %3
br label %48
14: ; preds = %2
%15 = load i32*, i32** %6
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %6
%19 = call i64 @ftell(i32* %18)
%20 = load i64*, i64** %5
store i64 %19, i64* %20
%21 = load i32*, i32** %6
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i32 0, i32 %22)
%24 = load i64*, i64** %5
%25 = load i64, i64* %24
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %44
30: ; preds = %14
%31 = load i64*, i64** %5
%32 = load i64, i64* %31
%33 = load i8*, i8** %7
%34 = load i64*, i64** %5
%35 = load i64, i64* %34
%36 = load i32*, i32** %6
%37 = call i64 @fread(i8* %33, i32 1, i64 %35, i32* %36)
%38 = icmp ne i64 %32, %37
br i1 %38, label %39, label %43
39: ; preds = %30
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
%42 = load i64*, i64** %5
store i64 0, i64* %42
store i8* null, i8** %7
br label %43
43: ; preds = %39, %30
br label %44
44: ; preds = %43, %14
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %7
store i8* %47, i8** %3
br label %48
48: ; preds = %44, %13
%49 = load i8*, i8** %3
ret i8* %49
}

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