#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);
size_t 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;
  size_t pagesize = getpagesize();
  pfn *= pagesize;
  unsigned long last_12_bits = virt_addr % pagesize;
  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    11a3 <translate_virt_addr+0x83>
movabs $0x7fffffffffffff,%rax
and    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpagesize@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
imul   -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x28(%rbp)
mov    %rdx,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    -0x30(%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     1162 <translate_virt_addr+0x42>
mov    %rax,%rbx
movabs $0x7fffffffffffff,%rax
and    %rax,%rbx
xor    %eax,%eax
call   1040 <getpagesize@plt>
mov    %rax,%rcx
imul   %rax,%rbx
or     %r14,%rax
shr    $0x20,%rax
je     1166 <translate_virt_addr+0x46>
mov    %r14,%rax
xor    %edx,%edx
div    %rcx
jmp    116d <translate_virt_addr+0x4d>
xor    %ebx,%ebx
jmp    1170 <translate_virt_addr+0x50>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
add    %rdx,%rbx
mov    %rbx,%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
%10 = alloca i64
store i64 %0, i64* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i64, i64* %5
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = call i64 @get_pfn(i64 %11, i32 %12, i32 %13)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = icmp ne i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %3
store i64 0, i64* %4
br label %31
18: ; preds = %3
%19 = load i64, i64* %8
%20 = and i64 %19, 36028797018963967
store i64 %20, i64* %8
%21 = call i64 (...) @getpagesize()
store i64 %21, i64* %9
%22 = load i64, i64* %9
%23 = load i64, i64* %8
%24 = mul i64 %23, %22
store i64 %24, i64* %8
%25 = load i64, i64* %5
%26 = load i64, i64* %9
%27 = urem i64 %25, %26
store i64 %27, i64* %10
%28 = load i64, i64* %8
%29 = load i64, i64* %10
%30 = add i64 %28, %29
store i64 %30, i64* %4
br label %31
31: ; preds = %18, %17
%32 = load i64, i64* %4
ret i64 %32
}

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 = urem i64 %0, %8
%11 = add i64 %9, %10
br label %12
12: ; preds = %3, %6
%13 = phi i64 [ %11, %6 ], [ 0, %3 ]
ret i64 %13
}