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


x86 assembly (clang, -O0):
<bus_to_virt>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x80000000,%eax
cmp    %rax,-0x10(%rbp)
jb     1155 <bus_to_virt+0x55>
mov    $0x80080000,%eax
cmp    %rax,-0x10(%rbp)
jae    1155 <bus_to_virt+0x55>
mov    -0x10(%rbp),%rax
movabs $0x80000000,%rcx
sub    %rcx,%rax
movabs $0x88000000,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11ad <bus_to_virt+0xad>
cmpq   $0x0,-0x10(%rbp)
jb     118c <bus_to_virt+0x8c>
cmpq   $0x8000000,-0x10(%rbp)
08
jae    118c <bus_to_virt+0x8c>
mov    -0x10(%rbp),%rax
sub    $0x0,%rax
movabs $0x80000000,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11a8 <bus_to_virt+0xa8>
mov    $0xc0000000,%eax
cmp    %rax,-0x10(%rbp)
jae    11a3 <bus_to_virt+0xa3>
movq   $0x0,-0x10(%rbp)
jmp    11a8 <bus_to_virt+0xa8>
jmp    11ad <bus_to_virt+0xad>
mov    -0x10(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bus_to_virt>:
mov    %rdi,%rax
mov    %rdi,%rcx
shr    $0x13,%rcx
cmp    $0x1000,%rcx
jne    111a <bus_to_virt+0x1a>
add    $0x8000000,%rax
ret
cmp    $0x7ffffff,%rax
ja     1129 <bus_to_virt+0x29>
sub    $0xffffffff80000000,%rax
ret
mov    $0x80000000,%ecx
add    $0x40000000,%rcx
xor    %edx,%edx
cmp    %rcx,%rax
cmovb  %rdx,%rax
ret

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

LLVM IR (-O2):
define weak i8* @bus_to_virt(i64 %0) {
%2 = and i64 %0, -524288
%3 = icmp eq i64 %2, 2147483648
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = add nuw nsw i64 %0, 134217728
br label %13
6: ; preds = %1
%7 = icmp ult i64 %0, 134217728
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = add nuw nsw i64 %0, 2147483648
br label %13
10: ; preds = %6
%11 = icmp ult i64 %0, 3221225472
%12 = select i1 %11, i64 0, i64 %0
br label %13
13: ; preds = %8, %10, %4
%14 = phi i64 [ %5, %4 ], [ %9, %8 ], [ %12, %10 ]
%15 = inttoptr i64 %14 to i8*
ret i8* %15
}