#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
char *load_rom(char *path, int *size) {
  unsigned char *data;
  int i, n;
  FILE *file;
  file = fopen(path, "rb");
  if (file == NULL)
    return NULL;
  fseek(file, 0, SEEK_END);
  n = ftell(file);
  *size = n;
  data = malloc(n * sizeof(char));
  fseek(file, 0, SEEK_SET);
  for (i = 0; i < n; i++) {
    data[i] = fgetc(file);
  }
  fclose(file);
  return data;
}


x86 assembly (clang, -O0):
<load_rom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119c <load_rom+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1234 <load_rom+0xd4>
mov    -0x30(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movslq -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1223 <load_rom+0xc3>
mov    -0x30(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11f3 <load_rom+0x93>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_rom>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
lea    0xe8f(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ef <load_rom+0x8f>
mov    %rax,%r14
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1050 <ftell@plt>
mov    %eax,%ebp
mov    %eax,(%rbx)
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
test   %ebp,%ebp
jle    11e5 <load_rom+0x85>
mov    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1040 <fgetc@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11d0 <load_rom+0x70>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11f2 <load_rom+0x92>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @load_rom(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
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** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %49
15: ; preds = %2
%16 = load i32*, i32** %9
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %9
%20 = call i32 @ftell(i32* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = load i32*, i32** %5
store i32 %21, i32* %22
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = mul i64 %24, 1
%26 = trunc i64 %25 to i32
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = load i32*, i32** %9
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
store i32 0, i32* %7
br label %31
31: ; preds = %42, %15
%32 = load i32, i32* %7
%33 = load i32, i32* %8
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %45
35: ; preds = %31
%36 = load i32*, i32** %9
%37 = call zeroext i8 @fgetc(i32* %36)
%38 = load i8*, i8** %6
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
br label %42
42: ; preds = %35
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %31
45: ; preds = %31
%46 = load i32*, i32** %9
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %6
store i8* %48, i8** %3
br label %49
49: ; preds = %45, %14
%50 = load i8*, i8** %3
ret i8* %50
}

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