#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *ReadBinary(char fileName[32], int *bufferSize) {
  FILE *pFile;
  long lSize;
  char *buffer;
  size_t result;
  pFile = fopen(fileName, "rb");
  if (!pFile) {
    printf("Cannot fopen() the file.\n");
    return NULL;
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (char *)malloc(sizeof(char) * lSize);
  if (!buffer) {
    printf("Failed to allocate the memory.\n");
    return NULL;
  }
  result = fread(buffer, 1, lSize, pFile);
  if (result % 3 != 0) {
    printf("Corrupted ROM? Need 3 byte instructions, got total of %lu???",
           result % 3);
    return NULL;
  }
  if (result != lSize) {
    printf("File corrupted? Number of bytes do not match...\n");
    return NULL;
  }
  fclose(pFile);
  *bufferSize = result;
  return buffer;
}


x86 assembly (clang, -O0):
<ReadBinary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ca <ReadBinary+0x4a>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <ReadBinary+0x151>
mov    -0x20(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122d <ReadBinary+0xad>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <ReadBinary+0x151>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
cmp    $0x0,%rdx
je     128b <ReadBinary+0x10b>
mov    -0x38(%rbp),%rax
mov    $0x3,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,%rsi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <ReadBinary+0x151>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     12b4 <ReadBinary+0x134>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d1 <ReadBinary+0x151>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadBinary>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe5e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122a <ReadBinary+0x9a>
mov    %rax,%rbx
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %r12d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1233 <ReadBinary+0xa3>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%rcx
movabs $0xaaaaaaaaaaaaaaab,%rdx
aa aa aa
mul    %rdx
shr    %rdx
lea    (%rdx,%rdx,2),%rax
mov    %rcx,%rsi
sub    %rax,%rsi
je     123c <ReadBinary+0xac>
lea    0xde5(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <printf@plt>
jmp    125d <ReadBinary+0xcd>
lea    0xe0f(%rip),%rdi
jmp    1255 <ReadBinary+0xc5>
lea    0xe1f(%rip),%rdi
jmp    1255 <ReadBinary+0xc5>
cmp    %r12,%rcx
jne    124e <ReadBinary+0xbe>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12d,(%r14)
jmp    125d <ReadBinary+0xcd>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [26 x i8] c"Cannot fopen() the file.\0A\00"
@SEEK_END = global i32 0
@.str.2 = [32 x i8] c"Failed to allocate the memory.\0A\00"
@.str.3 = [61 x i8] c"Corrupted ROM? Need 3 byte instructions, got total of %lu???\00"
@.str.4 = [49 x i8] c"File corrupted? Number of bytes do not match...\0A\00"
define i8* @ReadBinary(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %58
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %7
%22 = load i32*, i32** %6
%23 = call i32 @rewind(i32* %22)
%24 = load i64, i64* %7
%25 = mul i64 1, %24
%26 = trunc i64 %25 to i32
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %16
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %58
33: ; preds = %16
%34 = load i8*, i8** %8
%35 = load i64, i64* %7
%36 = load i32*, i32** %6
%37 = call i64 @fread(i8* %34, i32 1, i64 %35, i32* %36)
store i64 %37, i64* %9
%38 = load i64, i64* %9
%39 = urem i64 %38, 3
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %33
%42 = load i64, i64* %9
%43 = urem i64 %42, 3
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i64 %43)
store i8* null, i8** %3
br label %58
45: ; preds = %33
%46 = load i64, i64* %9
%47 = load i64, i64* %7
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %3
br label %58
51: ; preds = %45
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i64, i64* %9
%55 = trunc i64 %54 to i32
%56 = load i32*, i32** %5
store i32 %55, i32* %56
%57 = load i8*, i8** %8
store i8* %57, i8** %3
br label %58
58: ; preds = %51, %49, %41, %31, %14
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.3 = [61 x i8] c"Corrupted ROM? Need 3 byte instructions, got total of %lu???\00"
@str = [25 x i8] c"Cannot fopen() the file.\00"
@str.5 = [31 x i8] c"Failed to allocate the memory.\00"
@str.6 = [48 x i8] c"File corrupted? Number of bytes do not match...\00"
define i8* @ReadBinary(i8* nocapture %0, i32* 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 %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %30
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = tail call i32 @rewind(i32* %3)
%12 = trunc i64 %10 to i32
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %18
16: ; preds = %7
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
br label %30
18: ; preds = %7
%19 = tail call i64 @fread(i8* %14, i32 1, i64 %10, i32* %3)
%20 = urem i64 %19, 3
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.3, i64 0, i64 0), i64 %20)
br label %30
24: ; preds = %18
%25 = icmp eq i64 %19, %10
br i1 %25, label %28, label %26
26: ; preds = %24
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
br label %30
28: ; preds = %24
%29 = tail call i32 @fclose(i32* %3)
store i32 %12, i32* %1
br label %30
30: ; preds = %28, %26, %22, %16, %5
%31 = phi i8* [ null, %22 ], [ null, %26 ], [ %14, %28 ], [ null, %16 ], [ null, %5 ]
ret i8* %31
}