#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 scalar_t__ uint16_t;
typedef int /*<<< orphan*/ int8_t;
typedef scalar_t__ int16_t;

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


x86 assembly (clang, -O0):
<load_short>:
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     1173 <load_short+0x43>
cmpl   $0xffffffff,-0xc(%rbp)
ja     1173 <load_short+0x43>
mov    -0xc(%rbp),%edi
call   1050 <get_reg_val@plt>
mov    %rax,-0x8(%rbp)
jmp    1242 <load_short+0x112>
mov    -0xc(%rbp),%eax
and    $0xdfffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x9fc00000,-0xc(%rbp)
jb     11b2 <load_short+0x82>
cmpl   $0x9fe00000,-0xc(%rbp)
jae    11b2 <load_short+0x82>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1030 <flash_read@plt>
mov    %rax,-0x28(%rbp)
jmp    122d <load_short+0xfd>
cmpl   $0x9a000000,-0xc(%rbp)
jb     11e6 <load_short+0xb6>
cmpl   $0x9f200000,-0xc(%rbp)
jae    11e6 <load_short+0xb6>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1030 <flash_read@plt>
mov    %rax,-0x28(%rbp)
jmp    1228 <load_short+0xf8>
cmpl   $0x80000000,-0xc(%rbp)
jb     1223 <load_short+0xf3>
cmpl   $0x82000000,-0xc(%rbp)
jae    1223 <load_short+0xf3>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
movabs $0xfffffffe00000000,%rcx
ff ff ff
add    %rcx,%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1228 <load_short+0xf8>
jmp    122d <load_short+0xfd>
mov    -0x28(%rbp),%rdi
call   1040 <ntohs@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

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

LLVM IR (-O0):
define i64 @load_short(i32 %0, i32* %1, i32* %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %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 %59
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 i64 @flash_read(i32 %26, i32* %27, i32 2)
store i64 %28, i64* %8
br label %55
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 i64 @flash_read(i32 %36, i32* %37, i32 2)
store i64 %38, i64* %8
br label %54
39: ; preds = %32, %29
%40 = load i32, i32* %5
%41 = icmp uge i32 %40, -2147483648
br i1 %41, label %42, label %53
42: ; preds = %39
%43 = load i32, i32* %5
%44 = icmp ult i32 %43, -2113929216
br i1 %44, label %45, label %53
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 = bitcast i32* %50 to i64*
%52 = load i64, i64* %51
store i64 %52, i64* %8
br label %53
53: ; preds = %45, %42, %39
br label %54
54: ; preds = %53, %35
br label %55
55: ; preds = %54, %25
%56 = load i64, i64* %8
%57 = call i64 @ntohs(i64 %56)
store i64 %57, i64* %8
%58 = load i64, i64* %8
store i64 %58, i64* %4
br label %59
59: ; preds = %55, %14
%60 = load i64, i64* %4
ret i64 %60
}

LLVM IR (-O2):
define i64 @load_short(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 i64 @get_reg_val(i32 %0)
br label %29
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, i32* %2, i32 2)
br label %26
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, i32* %2, i32 2)
br label %26
18: ; preds = %13
%19 = icmp slt i32 %8, -2113929216
br i1 %19, label %20, label %26
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 = bitcast i32* %23 to i64*
%25 = load i64, i64* %24
br label %26
26: ; preds = %16, %20, %18, %11
%27 = phi i64 [ %12, %11 ], [ %17, %16 ], [ %25, %20 ], [ 0, %18 ]
%28 = tail call i64 @ntohs(i64 %27)
br label %29
29: ; preds = %26, %5
%30 = phi i64 [ %6, %5 ], [ %28, %26 ]
ret i64 %30
}