#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*/ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
FILE *loadrom(const char *ROMFILE, void *addr, uint32_t total_mem) {
  uint32_t rom_file_size;
  FILE *p = fopen(ROMFILE, "r+b");
  if (p == NULL) {
    return NULL;
  }
  fseek(p, 0L, SEEK_END);
  rom_file_size = ftell(p);
  fseek(p, 0L, SEEK_SET);
  printf("\nSize of ROM File : [%d] and Virtual Machine Memory : [%d]",
         rom_file_size, total_mem);
  printf("\nROM Address to be loaded at : [%p]", (void *)addr);
  printf("\nReading ROM Image......");
  fread(addr, sizeof(uint8_t), rom_file_size, p);
  printf("\nROM Reading Done.");
  return p;
}


x86 assembly (clang, -O0):
<loadrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118f <loadrom+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    1226 <loadrom+0xd6>
mov    -0x28(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
call   1030 <fread@plt>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadrom>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f7 <loadrom+0xa7>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
lea    0xe58(%rip),%rdi
mov    %ebp,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe80(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe93(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [59 x i8] c"\0ASize of ROM File : [%d] and Virtual Machine Memory : [%d]\00"
@.str.2 = [36 x i8] c"\0AROM Address to be loaded at : [%p]\00"
@.str.3 = [25 x i8] c"\0AReading ROM Image......\00"
@.str.4 = [19 x i8] c"\0AROM Reading Done.\00"
define i32* @loadrom(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32* null, i32** %4
br label %36
15: ; preds = %3
%16 = load i32*, i32** %9
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %9
%20 = call i32 @ftell(i32* %19)
store i32 %20, i32* %8
%21 = load i32*, i32** %9
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i64 0, i32 %22)
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i8*, i8** %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = load i32*, i32** %9
%33 = call i32 @fread(i8* %30, i32 4, i32 %31, i32* %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32*, i32** %9
store i32* %35, i32** %4
br label %36
36: ; preds = %15, %14
%37 = load i32*, i32** %4
ret i32* %37
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [59 x i8] c"\0ASize of ROM File : [%d] and Virtual Machine Memory : [%d]\00"
@.str.2 = [36 x i8] c"\0AROM Address to be loaded at : [%p]\00"
@.str.3 = [25 x i8] c"\0AReading ROM Image......\00"
@.str.4 = [19 x i8] c"\0AROM Reading Done.\00"
define noalias i32* @loadrom(i8* nocapture %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %4, i64 0, i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %2)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @fread(i8* %1, i32 4, i32 %9, i32* %4)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %17
17: ; preds = %3, %6
ret i32* %4
}