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

/* Variables and functions */
int /*<<< orphan*/ flash_read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ get_reg_val(int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int32_t load_word(uint32_t vaddr, int8_t *ram, int8_t *flash) {
  int32_t word = 0;
  if (vaddr >= 0xfffe0000 && vaddr <= 0xffffffff)
    return (int32_t)get_reg_val(vaddr);
  vaddr = vaddr & ~0x20000000;
  if (vaddr >= 0x9fc00000 && vaddr < 0x9fe00000)
    word = flash_read(vaddr, flash, 4);
  else if (vaddr >= 0x9a000000 && vaddr < 0x9f200000)
    word = flash_read(vaddr, flash, 4);
  else if (vaddr >= 0x80000000 && vaddr < 0x82000000)
    word = *(int32_t *)(ram + vaddr - 0x80000000);
  word = ntohl(word);
  return word;
}


x86 assembly (clang, -O0):
<load_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xfffe0000,-0x8(%rbp)
jb     1171 <load_word+0x41>
cmpl   $0xffffffff,-0x8(%rbp)
ja     1171 <load_word+0x41>
mov    -0x8(%rbp),%edi
call   1040 <get_reg_val@plt>
mov    %eax,-0x4(%rbp)
jmp    1238 <load_word+0x108>
mov    -0x8(%rbp),%eax
and    $0xdfffffff,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x9fc00000,-0x8(%rbp)
jb     11af <load_word+0x7f>
cmpl   $0x9fe00000,-0x8(%rbp)
jae    11af <load_word+0x7f>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <flash_read@plt>
mov    %eax,-0x1c(%rbp)
jmp    1227 <load_word+0xf7>
cmpl   $0x9a000000,-0x8(%rbp)
jb     11e2 <load_word+0xb2>
cmpl   $0x9f200000,-0x8(%rbp)
jae    11e2 <load_word+0xb2>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <flash_read@plt>
mov    %eax,-0x1c(%rbp)
jmp    1222 <load_word+0xf2>
cmpl   $0x80000000,-0x8(%rbp)
jb     121d <load_word+0xed>
cmpl   $0x82000000,-0x8(%rbp)
jae    121d <load_word+0xed>
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
movabs $0xfffffffe00000000,%rcx
ff ff ff
add    %rcx,%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1222 <load_word+0xf2>
jmp    1227 <load_word+0xf7>
mov    -0x1c(%rbp),%edi
call   1050 <ntohl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_word>:
mov    %edi,%eax
cmp    $0xfffe0000,%edi
jb     1141 <load_word+0x11>
mov    %eax,%edi
jmp    1040 <get_reg_val@plt>
push   %rax
mov    %eax,%edi
and    $0xdfffffff,%edi
and    $0xdfe00000,%eax
cmp    $0x9fc00000,%eax
je     1163 <load_word+0x33>
lea    0x66000000(%rdi),%eax
cmp    $0x51fffff,%eax
ja     1178 <load_word+0x48>
mov    %rdx,%rsi
mov    $0x4,%edx
call   1030 <flash_read@plt>
mov    %eax,%edi
pop    %rax
jmp    1050 <ntohl@plt>
xor    %eax,%eax
cmp    $0x81ffffff,%edi
jg     1170 <load_word+0x40>
movslq %edi,%rax
lea    (%rsi,%rax,4),%rax
movabs $0xfffffffe00000000,%rcx
ff ff ff
mov    (%rcx,%rax,1),%eax
jmp    1170 <load_word+0x40>

LLVM IR (-O0):
define i32 @load_word(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 0, i32* %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 i32 @get_reg_val(i32 %15)
store i32 %16, i32* %4
br label %58
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 i32*, i32** %7
%28 = call i32 @flash_read(i32 %26, i32* %27, i32 4)
store i32 %28, i32* %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 i32*, i32** %7
%38 = call i32 @flash_read(i32 %36, i32* %37, i32 4)
store i32 %38, i32* %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 i32*, i32** %6
%47 = load i32, i32* %5
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = getelementptr inbounds i32, i32* %49, i64 -2147483648
%51 = load i32, i32* %50
store i32 %51, i32* %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 i32, i32* %8
%56 = call i32 @ntohl(i32 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %14
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
define i32 @load_word(i32 %0, i32* nocapture %1, i32* %2) {
%4 = icmp ugt i32 %0, -131073
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @get_reg_val(i32 %0)
br label %28
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 i32 @flash_read(i32 %8, i32* %2, i32 4)
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 i32 @flash_read(i32 %8, i32* %2, i32 4)
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 i32, i32* %1, i64 %21
%23 = getelementptr inbounds i32, i32* %22, i64 -2147483648
%24 = load i32, i32* %23
br label %25
25: ; preds = %16, %20, %18, %11
%26 = phi i32 [ %12, %11 ], [ %17, %16 ], [ %24, %20 ], [ 0, %18 ]
%27 = tail call i32 @ntohl(i32 %26)
br label %28
28: ; preds = %25, %5
%29 = phi i32 [ %6, %5 ], [ %27, %25 ]
ret i32 %29
}