#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_END;
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 ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ *rom_fopen(int /*<<< orphan*/ *, char *);
int rom_load_linear_inverted(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;
  }
  (void)fseek(f, 0, SEEK_END);
  if (ftell(f) < sz) {
    (void)fclose(f);
    return (0);
  }
  if (ptr != NULL) {
    (void)fseek(f, off, SEEK_SET);
    (void)fread(ptr + addr + 0x10000, sz >> 1, 1, f);
    (void)fread(ptr + addr, sz >> 1, 1, f);
  }
  (void)fclose(f);
  return (1);
}


x86 assembly (clang, -O0):
<rom_load_linear_inverted>:
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    0xe8c(%rip),%rsi
call   1060 <rom_fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1194 <rom_load_linear_inverted+0x44>
movl   $0x0,-0x4(%rbp)
jmp    1270 <rom_load_linear_inverted+0x120>
cmpl   $0x40000,-0x14(%rbp)
jl     11ad <rom_load_linear_inverted+0x5d>
movl   $0x0,-0x14(%rbp)
jmp    11b8 <rom_load_linear_inverted+0x68>
mov    -0x14(%rbp),%eax
and    $0x3ffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1050 <ftell@plt>
cmp    -0x18(%rbp),%eax
jge    11f3 <rom_load_linear_inverted+0xa3>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1270 <rom_load_linear_inverted+0x120>
cmpq   $0x0,-0x28(%rbp)
je     1260 <rom_load_linear_inverted+0x110>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
add    $0x40000,%rdi
mov    -0x18(%rbp),%esi
sar    $0x1,%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%esi
sar    $0x1,%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_inverted>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%ebp
lea    0xe93(%rip),%rsi
call   1060 <rom_fopen@plt>
test   %rax,%rax
je     1211 <rom_load_linear_inverted+0xc1>
mov    %rax,%rbx
mov    %r12d,0x4(%rsp)
mov    %ebp,%r12d
and    $0x3ffff,%r12d
xor    %r13d,%r13d
cmp    $0x40000,%ebp
cmovge %r13d,%r12d
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
cmp    %r15d,%eax
jl     121c <rom_load_linear_inverted+0xcc>
test   %r14,%r14
je     1216 <rom_load_linear_inverted+0xc6>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    0x4(%rsp),%esi
call   1070 <fseek@plt>
lea    (%r14,%r12,4),%rbp
lea    (%r14,%r12,4),%rdi
add    $0x40000,%rdi
sar    %r15d
mov    $0x1,%r13d
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
jmp    121c <rom_load_linear_inverted+0xcc>
xor    %r13d,%r13d
jmp    1224 <rom_load_linear_inverted+0xd4>
mov    $0x1,%r13d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13d,%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_END = global i32 0
@SEEK_SET = global i32 0
define i32 @rom_load_linear_inverted(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 %64
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** %12
%27 = load i32, i32* @SEEK_END
%28 = call i32 @fseek(i32* %26, i32 0, i32 %27)
%29 = load i32*, i32** %12
%30 = call i32 @ftell(i32* %29)
%31 = load i32, i32* %9
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = load i32*, i32** %12
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %6
br label %64
36: ; preds = %25
%37 = load i32*, i32** %11
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %61
39: ; preds = %36
%40 = load i32*, i32** %12
%41 = load i32, i32* %10
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @fseek(i32* %40, i32 %41, i32 %42)
%44 = load i32*, i32** %11
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = getelementptr inbounds i32, i32* %47, i64 65536
%49 = load i32, i32* %9
%50 = ashr i32 %49, 1
%51 = load i32*, i32** %12
%52 = call i32 @fread(i32* %48, i32 %50, i32 1, i32* %51)
%53 = load i32*, i32** %11
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %53, i64 %55
%57 = load i32, i32* %9
%58 = ashr i32 %57, 1
%59 = load i32*, i32** %12
%60 = call i32 @fread(i32* %56, i32 %58, i32 1, i32* %59)
br label %61
61: ; preds = %39, %36
%62 = load i32*, i32** %12
%63 = call i32 @fclose(i32* %62)
store i32 1, i32* %6
br label %64
64: ; preds = %61, %33, %17
%65 = load i32, i32* %6
ret i32 %65
}

LLVM IR (-O2):
@.str = [3 x i32] [i32 114, i32 98, i32 0]
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @rom_load_linear_inverted(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 %30, label %8
8: ; preds = %5
%9 = icmp sgt i32 %1, 262143
%10 = and i32 %1, 262143
%11 = select i1 %9, i32 0, i32 %10
%12 = zext i32 %11 to i64
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %6, i32 0, i32 %13)
%15 = tail call i32 @ftell(i32* %6)
%16 = icmp slt i32 %15, %2
br i1 %16, label %27, label %17
17: ; preds = %8
%18 = icmp eq i32* %4, null
br i1 %18, label %27, label %19
19: ; preds = %17
%20 = load i32, i32* @SEEK_SET
%21 = tail call i32 @fseek(i32* %6, i32 %3, i32 %20)
%22 = getelementptr inbounds i32, i32* %4, i64 %12
%23 = getelementptr inbounds i32, i32* %22, i64 65536
%24 = ashr i32 %2, 1
%25 = tail call i32 @fread(i32* %23, i32 %24, i32 1, i32* %6)
%26 = tail call i32 @fread(i32* %22, i32 %24, i32 1, i32* %6)
br label %27
27: ; preds = %17, %19, %8
%28 = phi i32 [ 0, %8 ], [ 1, %19 ], [ 1, %17 ]
%29 = tail call i32 @fclose(i32* %6)
br label %30
30: ; preds = %27, %5
%31 = phi i32 [ 0, %5 ], [ %28, %27 ]
ret i32 %31
}