#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 */
int memory_addr_page(int);
int memory_translate_page(int, int, int, int);
int memory_translate_address(int ptbr, int ptlr, int address, int write) {
  int page, offset;
  int target_page;
  page = memory_addr_page(address);
  offset = address % 512;
  target_page = memory_translate_page(ptbr, ptlr, page, write);
  if (target_page < 0)
    return target_page;
  return target_page * 512 + offset;
}


x86 assembly (clang, -O0):
<memory_translate_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <memory_addr_page@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    $0x200,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1040 <memory_translate_page@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1176 <memory_translate_address+0x56>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1182 <memory_translate_address+0x62>
mov    -0x20(%rbp),%eax
shl    $0x9,%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memory_translate_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%ebp
mov    %edx,%edi
call   1030 <memory_addr_page@plt>
mov    %ebp,%edi
mov    %r15d,%esi
mov    %eax,%edx
mov    %r14d,%ecx
call   1040 <memory_translate_page@plt>
test   %eax,%eax
js     1163 <memory_translate_address+0x43>
lea    0x1ff(%rbx),%ecx
test   %ebx,%ebx
cmovns %ebx,%ecx
and    $0xfffffe00,%ecx
sub    %ecx,%ebx
shl    $0x9,%eax
add    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @memory_translate_address(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* %8
%14 = call i32 @memory_addr_page(i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %8
%16 = srem i32 %15, 512
store i32 %16, i32* %11
%17 = load i32, i32* %6
%18 = load i32, i32* %7
%19 = load i32, i32* %10
%20 = load i32, i32* %9
%21 = call i32 @memory_translate_page(i32 %17, i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %4
%25 = load i32, i32* %12
store i32 %25, i32* %5
br label %31
26: ; preds = %4
%27 = load i32, i32* %12
%28 = mul nsw i32 %27, 512
%29 = load i32, i32* %11
%30 = add nsw i32 %28, %29
store i32 %30, i32* %5
br label %31
31: ; preds = %26, %24
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
define i32 @memory_translate_address(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 @memory_addr_page(i32 %2)
%6 = tail call i32 @memory_translate_page(i32 %0, i32 %1, i32 %5, i32 %3)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = srem i32 %2, 512
%10 = shl nsw i32 %6, 9
%11 = add nsw i32 %10, %9
br label %12
12: ; preds = %4, %8
%13 = phi i32 [ %11, %8 ], [ %6, %4 ]
ret i32 %13
}