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

/* Variables and functions */
int /*<<< orphan*/ map_4k_page(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tube_parasite_write(int, int);
void tube_parasite_write_banksel(uint32_t addr, uint8_t val) {
  if ((addr & 0xFFF8) == 0xFEF8) {
    tube_parasite_write(addr, val);
  } else if ((addr & 0xFFF8) == 0xFEE0) {
    int logical = (addr & 7) << 1;
    int physical = (val << 1);
    map_4k_page(logical, physical);
    map_4k_page(logical + 1, physical + 1);
    if (logical == 0) {
      printf("Remapping page zero!\r\n");
      map_4k_page(16, physical);
    }
  }
}


x86 assembly (clang, -O0):
<tube_parasite_write_banksel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xfff8,%eax
cmp    $0xfef8,%eax
jne    1161 <tube_parasite_write_banksel+0x31>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <tube_parasite_write@plt>
jmp    11d2 <tube_parasite_write_banksel+0xa2>
mov    -0x4(%rbp),%eax
and    $0xfff8,%eax
cmp    $0xfee0,%eax
jne    11cd <tube_parasite_write_banksel+0x9d>
mov    -0x4(%rbp),%eax
and    $0x7,%eax
shl    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1040 <map_4k_page@plt>
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%esi
add    $0x1,%esi
call   1040 <map_4k_page@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11c8 <tube_parasite_write_banksel+0x98>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
mov    $0x10,%edi
call   1040 <map_4k_page@plt>
jmp    11cd <tube_parasite_write_banksel+0x9d>
jmp    11d2 <tube_parasite_write_banksel+0xa2>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tube_parasite_write_banksel>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebx
mov    %edi,%eax
and    $0xfff8,%eax
cmp    $0xfee0,%eax
je     115f <tube_parasite_write_banksel+0x2f>
movzwl %ax,%eax
cmp    $0xfef8,%eax
jne    1184 <tube_parasite_write_banksel+0x54>
mov    %ebx,%edi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <tube_parasite_write@plt>
add    %ebx,%ebx
and    $0xe,%ebx
lea    (%r14,%r14,1),%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <map_4k_page@plt>
lea    0x1(%rbx),%edi
lea    (%r14,%r14,1),%esi
add    $0x1,%esi
call   1040 <map_4k_page@plt>
test   %ebx,%ebx
je     1189 <tube_parasite_write_banksel+0x59>
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
mov    $0x10,%edi
mov    %ebp,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <map_4k_page@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Remapping page zero!\0D\0A\00"
define void @tube_parasite_write_banksel(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = and i32 %7, 65528
%9 = icmp eq i32 %8, 65272
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = call i32 @tube_parasite_write(i32 %11, i32 %12)
br label %40
14: ; preds = %2
%15 = load i32, i32* %3
%16 = and i32 %15, 65528
%17 = icmp eq i32 %16, 65248
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = load i32, i32* %3
%20 = and i32 %19, 7
%21 = shl i32 %20, 1
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = shl i32 %22, 1
store i32 %23, i32* %6
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = call i32 @map_4k_page(i32 %24, i32 %25)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = load i32, i32* %6
%30 = add nsw i32 %29, 1
%31 = call i32 @map_4k_page(i32 %28, i32 %30)
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %18
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %6
%37 = call i32 @map_4k_page(i32 16, i32 %36)
br label %38
38: ; preds = %34, %18
br label %39
39: ; preds = %38, %14
br label %40
40: ; preds = %39, %10
ret void
}

LLVM IR (-O2):
@str = [22 x i8] c"Remapping page zero!\0D\00"
define void @tube_parasite_write_banksel(i32 %0, i32 %1) {
%3 = trunc i32 %0 to i16
%4 = and i16 %3, -8
switch i16 %4, label %19 [
i16 -264, label %5
i16 -288, label %7
]
5: ; preds = %2
%6 = tail call i32 @tube_parasite_write(i32 %0, i32 %1)
br label %19
7: ; preds = %2
%8 = shl i32 %0, 1
%9 = and i32 %8, 14
%10 = shl i32 %1, 1
%11 = tail call i32 @map_4k_page(i32 %9, i32 %10)
%12 = or i32 %9, 1
%13 = or i32 %10, 1
%14 = tail call i32 @map_4k_page(i32 %12, i32 %13)
%15 = icmp eq i32 %9, 0
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @map_4k_page(i32 16, i32 %10)
br label %19
19: ; preds = %7, %16, %2, %5
ret void
}