unsigned long kva2pa(void *x) {
  unsigned long a = (unsigned long)x;
  if (a > 0xffffffffa0000000)
    return a - 0xffffffffa0000000;
  else if (a > 0xffffffff80000000)
    return a - 0xffffffff80000000;
  else if (a > (0x8000000000000000 + 0x1800000000000000))
    return a - (0x8000000000000000 + 0x1800000000000000);
  else if (a > (0x8000000000000000 + 0x1000000000000000))
    return a - (0x8000000000000000 + 0x1000000000000000);
  else
    return -1;
}


x86 assembly (clang, -O0):
<kva2pa>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffa0000000,-0x18(%rbp)
a0
jbe    1131 <kva2pa+0x31>
mov    -0x18(%rbp),%rax
sub    $0xffffffffa0000000,%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <kva2pa+0xb6>
cmpq   $0xffffffff80000000,-0x18(%rbp)
80
jbe    1152 <kva2pa+0x52>
mov    -0x18(%rbp),%rax
sub    $0xffffffff80000000,%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <kva2pa+0xb6>
movabs $0x9800000000000000,%rax
cmp    %rax,-0x18(%rbp)
jbe    1180 <kva2pa+0x80>
mov    -0x18(%rbp),%rax
movabs $0x9800000000000000,%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <kva2pa+0xb6>
movabs $0x9000000000000000,%rax
cmp    %rax,-0x18(%rbp)
jbe    11ae <kva2pa+0xae>
mov    -0x18(%rbp),%rax
movabs $0x9000000000000000,%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11b6 <kva2pa+0xb6>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<kva2pa>:
mov    %rdi,%rax
cmp    $0xffffffffa0000001,%rdi
jb     1113 <kva2pa+0x13>
add    $0x60000000,%rax
ret
cmp    $0xffffffff80000001,%rax
jb     1122 <kva2pa+0x22>
sub    $0xffffffff80000000,%rax
ret
movabs $0x9800000000000001,%rcx
cmp    %rcx,%rax
jb     113f <kva2pa+0x3f>
movabs $0x6800000000000000,%rcx
add    %rcx,%rax
ret
movabs $0x7000000000000000,%rcx
add    %rax,%rcx
movabs $0x9000000000000000,%rdx
cmp    %rdx,%rax
mov    $0xffffffffffffffff,%rax
cmova  %rcx,%rax
ret

LLVM IR (-O0):
define i64 @kva2pa(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = ptrtoint i8* %5 to i64
store i64 %6, i64* %4
%7 = load i64, i64* %4
%8 = icmp ugt i64 %7, -1610612736
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i64, i64* %4
%11 = sub i64 %10, -1610612736
store i64 %11, i64* %2
br label %31
12: ; preds = %1
%13 = load i64, i64* %4
%14 = icmp ugt i64 %13, -2147483648
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i64, i64* %4
%17 = sub i64 %16, -2147483648
store i64 %17, i64* %2
br label %31
18: ; preds = %12
%19 = load i64, i64* %4
%20 = icmp ugt i64 %19, -7493989779944505344
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = load i64, i64* %4
%23 = sub i64 %22, -7493989779944505344
store i64 %23, i64* %2
br label %31
24: ; preds = %18
%25 = load i64, i64* %4
%26 = icmp ugt i64 %25, -8070450532247928832
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i64, i64* %4
%29 = sub i64 %28, -8070450532247928832
store i64 %29, i64* %2
br label %31
30: ; preds = %24
store i64 -1, i64* %2
br label %31
31: ; preds = %30, %27, %21, %15, %9
%32 = load i64, i64* %2
ret i64 %32
}

LLVM IR (-O2):
define i64 @kva2pa(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = icmp ugt i8* %0, inttoptr (i64 -1610612736 to i8*)
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = add i64 %2, 1610612736
br label %18
6: ; preds = %1
%7 = icmp ugt i8* %0, inttoptr (i64 -2147483648 to i8*)
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = add i64 %2, 2147483648
br label %18
10: ; preds = %6
%11 = icmp ugt i8* %0, inttoptr (i64 -7493989779944505344 to i8*)
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = add i64 %2, 7493989779944505344
br label %18
14: ; preds = %10
%15 = icmp ugt i8* %0, inttoptr (i64 -8070450532247928832 to i8*)
%16 = add i64 %2, 8070450532247928832
%17 = select i1 %15, i64 %16, i64 -1
br label %18
18: ; preds = %14, %12, %8, %4
%19 = phi i64 [ %5, %4 ], [ %9, %8 ], [ %13, %12 ], [ %17, %14 ]
ret i64 %19
}