#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 */
typedef int /*<<< orphan*/ caddr_t;

/* Variables and functions */
int MAP_FIXED;
int MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
scalar_t__ malloc(int);
scalar_t__ mmap(int /*<<< orphan*/, int, int, int, int, long);
int open(char *, int);
int *phys_to_virt(long base) {
  int mem_fd;
  char *io_mem;
  char *io_m;
  volatile unsigned *io;
  mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  io_mem = (char *)malloc(8192);
  io_mem += 4096;
  io_mem = (char *)(((int)io_mem) & 0xfffff000);
  io_m = (char *)mmap((caddr_t)io_mem, 4096, PROT_READ | PROT_WRITE,
                      MAP_SHARED | MAP_FIXED, mem_fd, base);
  return ((int *)io_m);
}


x86 assembly (clang, -O0):
<phys_to_virt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
lea    0xeab(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0x2000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
and    $0xfffff000,%eax
mov    %eax,%eax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e08(%rip),%rax
or     (%rax),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x8(%rbp),%r9
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phys_to_virt>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    $0x2000,%edi
call   1040 <malloc@plt>
lea    0x1000(%rax),%edi
and    $0xfffff000,%edi
mov    0x2e85(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e63(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x1000,%esi
mov    %ebp,%r8d
mov    %rbx,%r9
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <mmap@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
define i32* @phys_to_virt(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i64 %0, i64* %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = call i64 @malloc(i32 8192)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 4096
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = ptrtoint i8* %15 to i32
%17 = and i32 %16, -4096
%18 = zext i32 %17 to i64
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %20 to i32
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* @MAP_SHARED
%26 = load i32, i32* @MAP_FIXED
%27 = or i32 %25, %26
%28 = load i32, i32* %3
%29 = load i64, i64* %2
%30 = call i64 @mmap(i32 %21, i32 4096, i32 %24, i32 %27, i32 %28, i64 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = bitcast i8* %32 to i32*
ret i32* %33
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
define i32* @phys_to_virt(i64 %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_SYNC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i64 @malloc(i32 8192)
%7 = inttoptr i64 %6 to i8*
%8 = getelementptr inbounds i8, i8* %7, i64 4096
%9 = ptrtoint i8* %8 to i64
%10 = trunc i64 %9 to i32
%11 = and i32 %10, -4096
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* @MAP_FIXED
%17 = or i32 %16, %15
%18 = tail call i64 @mmap(i32 %11, i32 4096, i32 %14, i32 %17, i32 %5, i64 %0)
%19 = inttoptr i64 %18 to i32*
ret i32* %19
}