#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 */
scalar_t__ z180_bbr();
unsigned char z180_cbar();
scalar_t__ z180_cbr();
unsigned long virtual_to_physical(void *_vaddr) {
  unsigned int vaddr = (unsigned int)_vaddr;
  unsigned char cbar;
  cbar = z180_cbar();
  if (vaddr < ((unsigned int)(cbar & 0x0F)) << 12) {
    return vaddr;
  }
  if (vaddr < ((unsigned int)(cbar & 0xF0)) << 8) {
    return ((unsigned long)z180_bbr() << 12) + vaddr;
  }
  return ((unsigned long)z180_cbr() << 12) + vaddr;
}


x86 assembly (clang, -O0):
<virtual_to_physical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <z180_cbar@plt>
mov    %al,-0x15(%rbp)
mov    -0x14(%rbp),%eax
movzbl -0x15(%rbp),%ecx
and    $0xf,%ecx
shl    $0xc,%ecx
cmp    %ecx,%eax
jae    116e <virtual_to_physical+0x3e>
mov    -0x14(%rbp),%eax
mov    %rax,-0x8(%rbp)
jmp    11b5 <virtual_to_physical+0x85>
mov    -0x14(%rbp),%eax
movzbl -0x15(%rbp),%ecx
and    $0xf0,%ecx
shl    $0x8,%ecx
cmp    %ecx,%eax
jae    11a0 <virtual_to_physical+0x70>
mov    $0x0,%al
call   1030 <z180_bbr@plt>
shl    $0xc,%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b5 <virtual_to_physical+0x85>
mov    $0x0,%al
call   1050 <z180_cbr@plt>
shl    $0xc,%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virtual_to_physical>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <z180_cbar@plt>
movzbl %al,%eax
mov    %eax,%ecx
shl    $0xc,%ecx
movzwl %cx,%ecx
cmp    %ebx,%ecx
jbe    114e <virtual_to_physical+0x1e>
mov    %ebx,%eax
pop    %rbx
ret
shl    $0x8,%eax
and    $0xf000,%eax
cmp    %ebx,%eax
jbe    1163 <virtual_to_physical+0x33>
xor    %eax,%eax
call   1030 <z180_bbr@plt>
jmp    116a <virtual_to_physical+0x3a>
xor    %eax,%eax
call   1050 <z180_cbr@plt>
mov    %rax,%rcx
shl    $0xc,%rcx
mov    %ebx,%eax
add    %rcx,%rax
pop    %rbx
ret

LLVM IR (-O0):
define i64 @virtual_to_physical(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = ptrtoint i8* %6 to i32
store i32 %7, i32* %4
%8 = call zeroext i8 (...) @z180_cbar()
store i8 %8, i8* %5
%9 = load i32, i32* %4
%10 = load i8, i8* %5
%11 = zext i8 %10 to i32
%12 = and i32 %11, 15
%13 = shl i32 %12, 12
%14 = icmp ult i32 %9, %13
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i32, i32* %4
%17 = zext i32 %16 to i64
store i64 %17, i64* %2
br label %37
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i8, i8* %5
%21 = zext i8 %20 to i32
%22 = and i32 %21, 240
%23 = shl i32 %22, 8
%24 = icmp ult i32 %19, %23
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = call i64 (...) @z180_bbr()
%27 = shl i64 %26, 12
%28 = load i32, i32* %4
%29 = zext i32 %28 to i64
%30 = add i64 %27, %29
store i64 %30, i64* %2
br label %37
31: ; preds = %18
%32 = call i64 (...) @z180_cbr()
%33 = shl i64 %32, 12
%34 = load i32, i32* %4
%35 = zext i32 %34 to i64
%36 = add i64 %33, %35
store i64 %36, i64* %2
br label %37
37: ; preds = %31, %25, %15
%38 = load i64, i64* %2
ret i64 %38
}

LLVM IR (-O2):
define i64 @virtual_to_physical(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = trunc i64 %2 to i32
%4 = tail call zeroext i8 (...) @z180_cbar()
%5 = zext i8 %4 to i32
%6 = shl nuw nsw i32 %5, 12
%7 = and i32 %6, 61440
%8 = icmp ugt i32 %7, %3
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = and i64 %2, 4294967295
br label %25
11: ; preds = %1
%12 = shl nuw nsw i32 %5, 8
%13 = and i32 %12, 61440
%14 = icmp ugt i32 %13, %3
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = tail call i64 (...) @z180_bbr()
%17 = shl i64 %16, 12
%18 = and i64 %2, 4294967295
%19 = add i64 %17, %18
br label %25
20: ; preds = %11
%21 = tail call i64 (...) @z180_cbr()
%22 = shl i64 %21, 12
%23 = and i64 %2, 4294967295
%24 = add i64 %22, %23
br label %25
25: ; preds = %20, %15, %9
%26 = phi i64 [ %10, %9 ], [ %19, %15 ], [ %24, %20 ]
ret i64 %26
}