#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 */

/* Variables and functions */
unsigned int getchar();
unsigned int read16(unsigned int);
unsigned int read8(unsigned int addr) {
  unsigned int data;
  switch (addr) {
  case 0x4006A007:
    data = getchar();
    if (data == 127) {
      data = 8;
    }
    return (data);
  case 0x4006A004:
    data = 0x00A0;
    return (data);
  default:
    data = read16(addr & (~1));
    if (addr & 1) {
      data >>= 8;
    }
    return (data & 0xFF);
  }
}


x86 assembly (clang, -O0):
<read8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0x4006a004,%eax
je     117a <read8+0x5a>
jmp    1141 <read8+0x21>
mov    -0x10(%rbp),%eax
sub    $0x4006a007,%eax
jne    118c <read8+0x6c>
jmp    1154 <read8+0x34>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x7f,-0xc(%rbp)
jne    116f <read8+0x4f>
movl   $0x8,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <read8+0x9d>
movl   $0xa0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <read8+0x9d>
mov    -0x8(%rbp),%edi
and    $0xfffffffe,%edi
call   1030 <read16@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11b2 <read8+0x92>
mov    -0xc(%rbp),%eax
shr    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read8>:
push   %rbx
cmp    $0x4006a004,%edi
je     1149 <read8+0x29>
mov    %edi,%ebx
cmp    $0x4006a007,%edi
jne    1150 <read8+0x30>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ecx
cmp    $0x7f,%eax
mov    $0x8,%eax
cmovne %ecx,%eax
pop    %rbx
ret
mov    $0xa0,%eax
pop    %rbx
ret
mov    %ebx,%edi
and    $0xfffffffe,%edi
call   1030 <read16@plt>
mov    %eax,%ecx
shr    $0x8,%ecx
test   $0x1,%bl
cmove  %eax,%ecx
movzbl %cl,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @read8(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
switch i32 %5, label %15 [
i32 1074176007, label %6
i32 1074176004, label %13
]
6: ; preds = %1
%7 = call i32 (...) @getchar()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 127
br i1 %9, label %10, label %11
10: ; preds = %6
store i32 8, i32* %4
br label %11
11: ; preds = %10, %6
%12 = load i32, i32* %4
store i32 %12, i32* %2
br label %28
13: ; preds = %1
store i32 160, i32* %4
%14 = load i32, i32* %4
store i32 %14, i32* %2
br label %28
15: ; preds = %1
%16 = load i32, i32* %3
%17 = and i32 %16, -2
%18 = call i32 @read16(i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %3
%20 = and i32 %19, 1
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %4
%24 = lshr i32 %23, 8
store i32 %24, i32* %4
br label %25
25: ; preds = %22, %15
%26 = load i32, i32* %4
%27 = and i32 %26, 255
store i32 %27, i32* %2
br label %28
28: ; preds = %25, %13, %11
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
define i32 @read8(i32 %0) {
switch i32 %0, label %6 [
i32 1074176007, label %2
i32 1074176004, label %14
]
2: ; preds = %1
%3 = tail call i32 (...) @getchar()
%4 = icmp eq i32 %3, 127
%5 = select i1 %4, i32 8, i32 %3
br label %14
6: ; preds = %1
%7 = and i32 %0, -2
%8 = tail call i32 @read16(i32 %7)
%9 = and i32 %0, 1
%10 = icmp eq i32 %9, 0
%11 = lshr i32 %8, 8
%12 = select i1 %10, i32 %8, i32 %11
%13 = and i32 %12, 255
br label %14
14: ; preds = %1, %6, %2
%15 = phi i32 [ %13, %6 ], [ %5, %2 ], [ 160, %1 ]
ret i32 %15
}