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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *rom_fopen(int /*<<< orphan*/ *, char *);
int rom_load_linear(wchar_t *fn, uint32_t addr, int sz, int off, uint8_t *ptr) {
  FILE *f = rom_fopen(fn, L"rb");
  if (f == NULL) {
    ;
    return (0);
  }
  if (addr >= 0x40000) {
    addr = 0;
  } else {
    addr &= 0x03ffff;
  }
  if (ptr != NULL) {
    (void)fseek(f, off, SEEK_SET);
    (void)fread(ptr + addr, sz, 1, f);
  }
  (void)fclose(f);
  return (1);
}


x86 assembly (clang, -O0):
<rom_load_linear>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1050 <rom_fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1184 <rom_load_linear+0x44>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <rom_load_linear+0xb8>
cmpl   $0x40000,-0x14(%rbp)
jl     119d <rom_load_linear+0x5d>
movl   $0x0,-0x14(%rbp)
jmp    11a8 <rom_load_linear+0x68>
mov    -0x14(%rbp),%eax
and    $0x3ffff,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e8 <rom_load_linear+0xa8>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rom_load_linear>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%r13d
lea    0xea2(%rip),%rsi
call   1050 <rom_fopen@plt>
test   %rax,%rax
je     11bb <rom_load_linear+0x7b>
mov    %rax,%rbp
test   %r12,%r12
je     11ac <rom_load_linear+0x6c>
mov    %r13d,%eax
and    $0x3ffff,%eax
xor    %ebx,%ebx
cmp    $0x40000,%r13d
cmovl  %eax,%ebx
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1060 <fseek@plt>
lea    (%r12,%rbx,4),%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    11bd <rom_load_linear+0x7d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i32] [i32 114, i32 98, i32 0]
@SEEK_SET = global i32 0
define i32 @rom_load_linear(i32* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32* %4, i32** %11
%13 = load i32*, i32** %7
%14 = call i32* @rom_fopen(i32* %13, i8* bitcast ([3 x i32]* @.str to i8*))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %18
17: ; preds = %5
store i32 0, i32* %6
br label %43
18: ; preds = %5
%19 = load i32, i32* %8
%20 = icmp sge i32 %19, 262144
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %8
br label %25
22: ; preds = %18
%23 = load i32, i32* %8
%24 = and i32 %23, 262143
store i32 %24, i32* %8
br label %25
25: ; preds = %22, %21
%26 = load i32*, i32** %11
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i32*, i32** %12
%30 = load i32, i32* %10
%31 = load i32, i32* @SEEK_SET
%32 = call i32 @fseek(i32* %29, i32 %30, i32 %31)
%33 = load i32*, i32** %11
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %9
%38 = load i32*, i32** %12
%39 = call i32 @fread(i32* %36, i32 %37, i32 1, i32* %38)
br label %40
40: ; preds = %28, %25
%41 = load i32*, i32** %12
%42 = call i32 @fclose(i32* %41)
store i32 1, i32* %6
br label %43
43: ; preds = %40, %17
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
@.str = [3 x i32] [i32 114, i32 98, i32 0]
@SEEK_SET = global i32 0
define i32 @rom_load_linear(i32* %0, i32 %1, i32 %2, i32 %3, i32* %4) {
%6 = tail call i32* @rom_fopen(i32* %0, i8* bitcast ([3 x i32]* @.str to i8*))
%7 = icmp eq i32* %6, null
br i1 %7, label %21, label %8
8: ; preds = %5
%9 = icmp eq i32* %4, null
br i1 %9, label %19, label %10
10: ; preds = %8
%11 = icmp sgt i32 %1, 262143
%12 = and i32 %1, 262143
%13 = select i1 %11, i32 0, i32 %12
%14 = zext i32 %13 to i64
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %6, i32 %3, i32 %15)
%17 = getelementptr inbounds i32, i32* %4, i64 %14
%18 = tail call i32 @fread(i32* %17, i32 %2, i32 1, i32* %6)
br label %19
19: ; preds = %10, %8
%20 = tail call i32 @fclose(i32* %6)
br label %21
21: ; preds = %5, %19
%22 = phi i32 [ 1, %19 ], [ 0, %5 ]
ret i32 %22
}