#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 scalar_t__ uint8_t;
typedef int uint32_t;
typedef scalar_t__ int8_t;

/* Variables and functions */
scalar_t__ flash_read(int, scalar_t__ *, int);
scalar_t__ get_reg_val(int);
uint8_t load_byte(uint32_t vaddr, int8_t *ram, int8_t *flash) {
  int8_t byte = 0;
  if (vaddr >= 0xfffe0000 && vaddr <= 0xffffffff)
    return (int8_t)get_reg_val(vaddr);
  vaddr = vaddr & ~0x20000000;
  if (vaddr >= 0x9fc00000 && vaddr < 0x9fe00000)
    byte = (int8_t)flash_read(vaddr, flash, 1);
  else if (vaddr >= 0x9a000000 && vaddr < 0x9f200000)
    byte = (int8_t)flash_read(vaddr, flash, 1);
  else if (vaddr >= 0x80000000 && vaddr < 0x82000000)
    byte = *(int8_t *)(ram + vaddr - 0x80000000);
  return byte;
}


x86 assembly (clang, -O0):
<load_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0xfffe0000,-0xc(%rbp)
jb     1163 <load_byte+0x43>
cmpl   $0xffffffff,-0xc(%rbp)
ja     1163 <load_byte+0x43>
mov    -0xc(%rbp),%edi
call   1040 <get_reg_val@plt>
mov    %rax,-0x8(%rbp)
jmp    1225 <load_byte+0x105>
mov    -0xc(%rbp),%eax
and    $0xdfffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x9fc00000,-0xc(%rbp)
jb     11a2 <load_byte+0x82>
cmpl   $0x9fe00000,-0xc(%rbp)
jae    11a2 <load_byte+0x82>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <flash_read@plt>
mov    %rax,-0x28(%rbp)
jmp    121d <load_byte+0xfd>
cmpl   $0x9a000000,-0xc(%rbp)
jb     11d6 <load_byte+0xb6>
cmpl   $0x9f200000,-0xc(%rbp)
jae    11d6 <load_byte+0xb6>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <flash_read@plt>
mov    %rax,-0x28(%rbp)
jmp    1218 <load_byte+0xf8>
cmpl   $0x80000000,-0xc(%rbp)
jb     1213 <load_byte+0xf3>
cmpl   $0x82000000,-0xc(%rbp)
jae    1213 <load_byte+0xf3>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
movabs $0xfffffffc00000000,%rcx
ff ff ff
add    %rcx,%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1218 <load_byte+0xf8>
jmp    121d <load_byte+0xfd>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_byte>:
mov    %edi,%eax
cmp    $0xfffe0000,%edi
jb     1131 <load_byte+0x11>
mov    %eax,%edi
jmp    1040 <get_reg_val@plt>
mov    %eax,%edi
and    $0xdfffffff,%edi
and    $0xdfe00000,%eax
cmp    $0x9fc00000,%eax
je     1152 <load_byte+0x32>
lea    0x66000000(%rdi),%eax
cmp    $0x51fffff,%eax
ja     115f <load_byte+0x3f>
mov    %rdx,%rsi
mov    $0x1,%edx
jmp    1030 <flash_read@plt>
cmp    $0x81ffffff,%edi
jg     117d <load_byte+0x5d>
movslq %edi,%rax
lea    (%rsi,%rax,8),%rax
movabs $0xfffffffc00000000,%rcx
ff ff ff
mov    (%rcx,%rax,1),%rax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i64 @load_byte(i32 %0, i64* %1, i64* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64
store i32 %0, i32* %5
store i64* %1, i64** %6
store i64* %2, i64** %7
store i64 0, i64* %8
%9 = load i32, i32* %5
%10 = icmp uge i32 %9, -131072
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* %5
%13 = icmp ule i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %5
%16 = call i64 @get_reg_val(i32 %15)
store i64 %16, i64* %4
br label %56
17: ; preds = %11, %3
%18 = load i32, i32* %5
%19 = and i32 %18, -536870913
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp uge i32 %20, -1614807040
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* %5
%24 = icmp ult i32 %23, -1612709888
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* %5
%27 = load i64*, i64** %7
%28 = call i64 @flash_read(i32 %26, i64* %27, i32 1)
store i64 %28, i64* %8
br label %54
29: ; preds = %22, %17
%30 = load i32, i32* %5
%31 = icmp uge i32 %30, -1711276032
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i32, i32* %5
%34 = icmp ult i32 %33, -1625292800
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i32, i32* %5
%37 = load i64*, i64** %7
%38 = call i64 @flash_read(i32 %36, i64* %37, i32 1)
store i64 %38, i64* %8
br label %53
39: ; preds = %32, %29
%40 = load i32, i32* %5
%41 = icmp uge i32 %40, -2147483648
br i1 %41, label %42, label %52
42: ; preds = %39
%43 = load i32, i32* %5
%44 = icmp ult i32 %43, -2113929216
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i64*, i64** %6
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i64, i64* %46, i64 %48
%50 = getelementptr inbounds i64, i64* %49, i64 -2147483648
%51 = load i64, i64* %50
store i64 %51, i64* %8
br label %52
52: ; preds = %45, %42, %39
br label %53
53: ; preds = %52, %35
br label %54
54: ; preds = %53, %25
%55 = load i64, i64* %8
store i64 %55, i64* %4
br label %56
56: ; preds = %54, %14
%57 = load i64, i64* %4
ret i64 %57
}

LLVM IR (-O2):
define i64 @load_byte(i32 %0, i64* nocapture %1, i64* %2) {
%4 = icmp ugt i32 %0, -131073
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i64 @get_reg_val(i32 %0)
br label %25
7: ; preds = %3
%8 = and i32 %0, -536870913
%9 = and i32 %0, -538968064
%10 = icmp eq i32 %9, -1614807040
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i64 @flash_read(i32 %8, i64* %2, i32 1)
br label %25
13: ; preds = %7
%14 = add i32 %8, 1711276032
%15 = icmp ult i32 %14, 85983232
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i64 @flash_read(i32 %8, i64* %2, i32 1)
br label %25
18: ; preds = %13
%19 = icmp slt i32 %8, -2113929216
br i1 %19, label %20, label %25
20: ; preds = %18
%21 = sext i32 %8 to i64
%22 = getelementptr inbounds i64, i64* %1, i64 %21
%23 = getelementptr inbounds i64, i64* %22, i64 -2147483648
%24 = load i64, i64* %23
br label %25
25: ; preds = %11, %18, %20, %16, %5
%26 = phi i64 [ %6, %5 ], [ %12, %11 ], [ %17, %16 ], [ %24, %20 ], [ 0, %18 ]
ret i64 %26
}