__attribute__((always_)) unsigned long virt_to_bus(const void *addr) {
  unsigned long ret = (unsigned long)addr;
  if ((((ret) >= (0x88000000)) &&
       ((ret) < (0x88000000) + ((8 * (64 * 1024)))))) {
    ret = ret - 0x88000000 + 0x80000000;
  } else if ((((ret) >= (0x80000000)) &&
              ((ret) < (0x80000000) + ((128 * 1024 * 1024))))) {
    ret = ret - 0x80000000 + 0x0;
  } else if (ret < 0xC0000000) {
    ret = ((unsigned long)0);
  }
  return ret;
}


x86 assembly (clang, -O0):
<virt_to_bus>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x88000000,%eax
cmp    %rax,-0x10(%rbp)
jb     1155 <virt_to_bus+0x55>
mov    $0x88080000,%eax
cmp    %rax,-0x10(%rbp)
jae    1155 <virt_to_bus+0x55>
mov    -0x10(%rbp),%rax
movabs $0x88000000,%rcx
sub    %rcx,%rax
movabs $0x80000000,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11b2 <virt_to_bus+0xb2>
mov    $0x80000000,%eax
cmp    %rax,-0x10(%rbp)
jb     1191 <virt_to_bus+0x91>
mov    $0x88000000,%eax
cmp    %rax,-0x10(%rbp)
jae    1191 <virt_to_bus+0x91>
mov    -0x10(%rbp),%rax
movabs $0x80000000,%rcx
sub    %rcx,%rax
add    $0x0,%rax
mov    %rax,-0x10(%rbp)
jmp    11ad <virt_to_bus+0xad>
mov    $0xc0000000,%eax
cmp    %rax,-0x10(%rbp)
jae    11a8 <virt_to_bus+0xa8>
movq   $0x0,-0x10(%rbp)
jmp    11ad <virt_to_bus+0xad>
jmp    11b2 <virt_to_bus+0xb2>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<virt_to_bus>:
mov    $0x87ffffff,%ecx
mov    %rdi,%rax
shr    $0x1b,%rax
cmp    $0x11,%rax
jb     1129 <virt_to_bus+0x29>
lea    0x80001(%rcx),%rax
cmp    %rax,%rdi
jae    1129 <virt_to_bus+0x29>
add    $0xfffffffff8000000,%rdi
mov    %rdi,%rax
ret
test   $0xffffffff80000000,%rdi
je     1146 <virt_to_bus+0x46>
lea    0x1(%rcx),%rax
cmp    %rax,%rdi
jae    1146 <virt_to_bus+0x46>
add    $0xffffffff80000000,%rdi
mov    %rdi,%rax
ret
add    $0x38000001,%rcx
xor    %eax,%eax
cmp    %rcx,%rdi
cmovae %rdi,%rax
ret

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

LLVM IR (-O2):
define i64 @virt_to_bus(i8* %0) {
%2 = ptrtoint i8* %0 to i64
%3 = icmp ugt i8* %0, inttoptr (i64 2281701375 to i8*)
%4 = icmp ult i8* %0, inttoptr (i64 2282225664 to i8*)
%5 = and i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = add i64 %2, -134217728
br label %17
8: ; preds = %1
%9 = icmp ugt i8* %0, inttoptr (i64 2147483647 to i8*)
%10 = icmp ult i8* %0, inttoptr (i64 2281701376 to i8*)
%11 = and i1 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = add i64 %2, -2147483648
br label %17
14: ; preds = %8
%15 = icmp ult i8* %0, inttoptr (i64 3221225472 to i8*)
%16 = select i1 %15, i64 0, i64 %2
br label %17
17: ; preds = %12, %14, %6
%18 = phi i64 [ %7, %6 ], [ %13, %12 ], [ %16, %14 ]
ret i64 %18
}