#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 /*<<< orphan*/ exception_set_epn(int /*<<< orphan*/);
int /*<<< orphan*/ exception_set_ma(int);
int /*<<< orphan*/ machine_get_ipreg();
int /*<<< orphan*/ machine_register_exception(char *, int);
int /*<<< orphan*/ memory_addr_page(int);
int memory_translate_address(int, int, int, int);
int /*<<< orphan*/ registers_get_register(char *);
int word_get_integer(int /*<<< orphan*/);
int /*<<< orphan*/ word_store_integer(int /*<<< orphan*/, int);
int machine_translate_address(int address, int write, int type, int mode) {
  int ptbr, ptlr, ret_addr, curr_ip;
  if (mode == 1)
    return address;
  ptbr = word_get_integer(registers_get_register("PTBR"));
  ptlr = word_get_integer(registers_get_register("PTLR"));
  ret_addr = memory_translate_address(ptbr, ptlr, address, write);
  if (ret_addr < 0 && type == -7)
    return ret_addr;
  if (ret_addr < 0 && type == -5) {
    curr_ip = word_get_integer(machine_get_ipreg());
    curr_ip = curr_ip + 2;
    word_store_integer(machine_get_ipreg(), curr_ip);
  }
  if (-1 == ret_addr) {
    exception_set_ma(address);
    machine_register_exception("Access violation", 2);
  } else if (-2 == ret_addr) {
    exception_set_epn(memory_addr_page(address));
    machine_register_exception("Page fault", 0);
  } else if (-3 == ret_addr) {
    exception_set_ma(address);
    machine_register_exception("Address outside logical address space", 2);
  }
  return ret_addr;
}


x86 assembly (clang, -O0):
<machine_translate_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    11b9 <machine_translate_address+0x29>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <machine_translate_address+0x15a>
lea    0xe40(%rip),%rdi
call   1030 <registers_get_register@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
mov    %eax,-0x18(%rbp)
lea    0xe2f(%rip),%rdi
call   1030 <registers_get_register@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
call   1070 <memory_translate_address@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1218 <machine_translate_address+0x88>
cmpl   $0xfffffff9,-0x10(%rbp)
jne    1218 <machine_translate_address+0x88>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <machine_translate_address+0x15a>
cmpl   $0x0,-0x20(%rbp)
jge    1257 <machine_translate_address+0xc7>
cmpl   $0xfffffffb,-0x10(%rbp)
jne    1257 <machine_translate_address+0xc7>
mov    $0x0,%al
call   1050 <machine_get_ipreg@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <machine_get_ipreg@plt>
mov    %eax,%edi
mov    -0x24(%rbp),%esi
call   1040 <word_store_integer@plt>
mov    $0xffffffff,%eax
cmp    -0x20(%rbp),%eax
jne    1283 <machine_translate_address+0xf3>
mov    -0x8(%rbp),%edi
call   10b0 <exception_set_ma@plt>
lea    0xd96(%rip),%rdi
mov    $0x2,%esi
call   10a0 <machine_register_exception@plt>
jmp    12e4 <machine_translate_address+0x154>
mov    $0xfffffffe,%eax
cmp    -0x20(%rbp),%eax
jne    12b3 <machine_translate_address+0x123>
mov    -0x8(%rbp),%edi
call   1090 <memory_addr_page@plt>
mov    %eax,%edi
call   1080 <exception_set_epn@plt>
lea    0xd74(%rip),%rdi
xor    %esi,%esi
call   10a0 <machine_register_exception@plt>
jmp    12df <machine_translate_address+0x14f>
mov    $0xfffffffd,%eax
cmp    -0x20(%rbp),%eax
jne    12da <machine_translate_address+0x14a>
mov    -0x8(%rbp),%edi
call   10b0 <exception_set_ma@plt>
lea    0xd56(%rip),%rdi
mov    $0x2,%esi
call   10a0 <machine_register_exception@plt>
jmp    12df <machine_translate_address+0x14f>
jmp    12e4 <machine_translate_address+0x154>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<machine_translate_address>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
cmp    $0x1,%ecx
jne    11a7 <machine_translate_address+0x17>
mov    %r14d,%ebp
jmp    128e <machine_translate_address+0xfe>
mov    %edx,%r15d
mov    %esi,%ebp
lea    0xe4d(%rip),%rdi
call   1030 <registers_get_register@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
mov    %eax,%ebx
lea    0xe3d(%rip),%rdi
call   1030 <registers_get_register@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
mov    %ebx,%edi
mov    %eax,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1070 <memory_translate_address@plt>
mov    %eax,%ebp
cmp    $0xfffffff9,%r15d
jne    11f2 <machine_translate_address+0x62>
test   %ebp,%ebp
js     128e <machine_translate_address+0xfe>
cmp    $0xfffffffb,%r15d
jne    121d <machine_translate_address+0x8d>
test   %ebp,%ebp
jns    121d <machine_translate_address+0x8d>
xor    %eax,%eax
call   1050 <machine_get_ipreg@plt>
mov    %eax,%edi
call   1060 <word_get_integer@plt>
lea    0x2(%rax),%ebx
xor    %eax,%eax
call   1050 <machine_get_ipreg@plt>
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <word_store_integer@plt>
cmp    $0xfffffffd,%ebp
je     124c <machine_translate_address+0xbc>
cmp    $0xfffffffe,%ebp
je     126c <machine_translate_address+0xdc>
cmp    $0xffffffff,%ebp
jne    128e <machine_translate_address+0xfe>
mov    %r14d,%edi
call   10b0 <exception_set_ma@plt>
lea    0xdcf(%rip),%rdi
mov    $0x2,%esi
call   10a0 <machine_register_exception@plt>
mov    $0xffffffff,%ebp
jmp    128e <machine_translate_address+0xfe>
mov    %r14d,%edi
call   10b0 <exception_set_ma@plt>
lea    0xdcb(%rip),%rdi
mov    $0x2,%esi
call   10a0 <machine_register_exception@plt>
mov    $0xfffffffd,%ebp
jmp    128e <machine_translate_address+0xfe>
mov    %r14d,%edi
call   1090 <memory_addr_page@plt>
mov    %eax,%edi
call   1080 <exception_set_epn@plt>
lea    0xd99(%rip),%rdi
xor    %esi,%esi
call   10a0 <machine_register_exception@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PTBR\00"
@.str.1 = [5 x i8] c"PTLR\00"
@.str.2 = [17 x i8] c"Access violation\00"
@.str.3 = [11 x i8] c"Page fault\00"
@.str.4 = [38 x i8] c"Address outside logical address space\00"
define i32 @machine_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
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* %6
store i32 %17, i32* %5
br label %75
18: ; preds = %4
%19 = call i32 @registers_get_register(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @word_get_integer(i32 %19)
store i32 %20, i32* %10
%21 = call i32 @registers_get_register(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @word_get_integer(i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = call i32 @memory_translate_address(i32 %23, i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %18
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, -7
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load i32, i32* %12
store i32 %34, i32* %5
br label %75
35: ; preds = %30, %18
%36 = load i32, i32* %12
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -5
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = call i32 (...) @machine_get_ipreg()
%43 = call i32 @word_get_integer(i32 %42)
store i32 %43, i32* %13
%44 = load i32, i32* %13
%45 = add nsw i32 %44, 2
store i32 %45, i32* %13
%46 = call i32 (...) @machine_get_ipreg()
%47 = load i32, i32* %13
%48 = call i32 @word_store_integer(i32 %46, i32 %47)
br label %49
49: ; preds = %41, %38, %35
%50 = load i32, i32* %12
%51 = icmp eq i32 -1, %50
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* %6
%54 = call i32 @exception_set_ma(i32 %53)
%55 = call i32 @machine_register_exception(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %73
56: ; preds = %49
%57 = load i32, i32* %12
%58 = icmp eq i32 -2, %57
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32, i32* %6
%61 = call i32 @memory_addr_page(i32 %60)
%62 = call i32 @exception_set_epn(i32 %61)
%63 = call i32 @machine_register_exception(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %72
64: ; preds = %56
%65 = load i32, i32* %12
%66 = icmp eq i32 -3, %65
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = load i32, i32* %6
%69 = call i32 @exception_set_ma(i32 %68)
%70 = call i32 @machine_register_exception(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 2)
br label %71
71: ; preds = %67, %64
br label %72
72: ; preds = %71, %59
br label %73
73: ; preds = %72, %52
%74 = load i32, i32* %12
store i32 %74, i32* %5
br label %75
75: ; preds = %73, %33, %16
%76 = load i32, i32* %5
ret i32 %76
}

LLVM IR (-O2):
@.str = [5 x i8] c"PTBR\00"
@.str.1 = [5 x i8] c"PTLR\00"
@.str.2 = [17 x i8] c"Access violation\00"
@.str.3 = [11 x i8] c"Page fault\00"
@.str.4 = [38 x i8] c"Address outside logical address space\00"
define i32 @machine_translate_address(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 1
br i1 %5, label %35, label %6
6: ; preds = %4
%7 = tail call i32 @registers_get_register(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @word_get_integer(i32 %7)
%9 = tail call i32 @registers_get_register(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @word_get_integer(i32 %9)
%11 = tail call i32 @memory_translate_address(i32 %8, i32 %10, i32 %0, i32 %1)
%12 = icmp slt i32 %11, 0
%13 = icmp eq i32 %2, -7
%14 = and i1 %13, %12
br i1 %14, label %35, label %15
15: ; preds = %6
%16 = icmp eq i32 %2, -5
%17 = and i1 %16, %12
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = tail call i32 (...) @machine_get_ipreg()
%20 = tail call i32 @word_get_integer(i32 %19)
%21 = add nsw i32 %20, 2
%22 = tail call i32 (...) @machine_get_ipreg()
%23 = tail call i32 @word_store_integer(i32 %22, i32 %21)
br label %24
24: ; preds = %18, %15
switch i32 %11, label %35 [
i32 -1, label %25
i32 -2, label %28
i32 -3, label %32
]
25: ; preds = %24
%26 = tail call i32 @exception_set_ma(i32 %0)
%27 = tail call i32 @machine_register_exception(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %35
28: ; preds = %24
%29 = tail call i32 @memory_addr_page(i32 %0)
%30 = tail call i32 @exception_set_epn(i32 %29)
%31 = tail call i32 @machine_register_exception(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %35
32: ; preds = %24
%33 = tail call i32 @exception_set_ma(i32 %0)
%34 = tail call i32 @machine_register_exception(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 2)
br label %35
35: ; preds = %25, %32, %28, %24, %6, %4
%36 = phi i32 [ %0, %4 ], [ %11, %6 ], [ %11, %24 ], [ -2, %28 ], [ -3, %32 ], [ -1, %25 ]
ret i32 %36
}