#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 */
unsigned long get_pfn(unsigned long, unsigned int, unsigned int);
unsigned long getpagesize();
unsigned long translate_virt_addr(unsigned long virt_addr, unsigned pid,
                                  unsigned int big_endian) {
  unsigned long pfn = get_pfn(virt_addr, pid, big_endian);
  if (!pfn)
    return 0x0;
  pfn &= 0x7FFFFFFFFFFFFF;
  pfn *= getpagesize();
  unsigned long last_12_bits = (0x0000000000000FFF) & virt_addr;
  return pfn + last_12_bits;
}


x86 assembly (clang, -O0):
<translate_virt_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <get_pfn@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    115d <translate_virt_addr+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1199 <translate_virt_addr+0x79>
movabs $0x7fffffffffffff,%rax
and    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
imul   -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
and    $0xfff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<translate_virt_addr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1030 <get_pfn@plt>
test   %rax,%rax
je     1158 <translate_virt_addr+0x38>
mov    %rax,%rbx
movabs $0x7fffffffffffff,%rax
and    %rax,%rbx
xor    %eax,%eax
call   1040 <getpagesize@plt>
imul   %rbx,%rax
and    $0xfff,%r14d
add    %rax,%r14
jmp    115b <translate_virt_addr+0x3b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i64 @translate_virt_addr(i64 %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i64 %0, i64* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i64, i64* %5
%11 = load i32, i32* %6
%12 = load i32, i32* %7
%13 = call i64 @get_pfn(i64 %10, i32 %11, i32 %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp ne i64 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %3
store i64 0, i64* %4
br label %28
17: ; preds = %3
%18 = load i64, i64* %8
%19 = and i64 %18, 36028797018963967
store i64 %19, i64* %8
%20 = call i64 (...) @getpagesize()
%21 = load i64, i64* %8
%22 = mul i64 %21, %20
store i64 %22, i64* %8
%23 = load i64, i64* %5
%24 = and i64 4095, %23
store i64 %24, i64* %9
%25 = load i64, i64* %8
%26 = load i64, i64* %9
%27 = add i64 %25, %26
store i64 %27, i64* %4
br label %28
28: ; preds = %17, %16
%29 = load i64, i64* %4
ret i64 %29
}

LLVM IR (-O2):
define i64 @translate_virt_addr(i64 %0, i32 %1, i32 %2) {
%4 = tail call i64 @get_pfn(i64 %0, i32 %1, i32 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = and i64 %4, 36028797018963967
%8 = tail call i64 (...) @getpagesize()
%9 = mul i64 %8, %7
%10 = and i64 %0, 4095
%11 = add i64 %9, %10
br label %12
12: ; preds = %3, %6
%13 = phi i64 [ %11, %6 ], [ 0, %3 ]
ret i64 %13
}