#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*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, size_t const, int, int /*<<< orphan*/, int,
           size_t const);
int open(char *, int);
void *xumem_map_phys(const size_t base, const size_t size) {
  int memfd = open("/dev/mem", O_RDWR | O_SYNC);
  if (memfd < 0)
    return NULL;
  void *p = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED,
                 memfd, base);
  close(memfd);
  return p;
}


x86 assembly (clang, -O0):
<xumem_map_phys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1178 <xumem_map_phys+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11c5 <xumem_map_phys+0x95>
mov    -0x18(%rbp),%rsi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
mov    0x2e2c(%rip),%rax
or     (%rax),%edx
mov    0x2e33(%rip),%rax
or     (%rax),%edx
mov    0x2e4a(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x10(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xumem_map_phys>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e97(%rip),%rax
mov    0x2e88(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11a1 <xumem_map_phys+0x71>
mov    %eax,%ebp
mov    0x2e93(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e51(%rip),%rax
or     (%rax),%edx
mov    0x2e68(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
mov    %r14,%r9
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
jmp    11a3 <xumem_map_phys+0x73>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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
@PROT_EXEC = global i32 0
@MAP_SHARED = global i32 0
define i8* @xumem_map_phys(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %29
15: ; preds = %2
%16 = load i64, i64* %5
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %17, %18
%20 = load i32, i32* @PROT_EXEC
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %6
%24 = load i64, i64* %4
%25 = call i8* @mmap(i32* null, i64 %16, i32 %21, i32 %22, i32 %23, i64 %24)
store i8* %25, i8** %7
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i8*, i8** %7
store i8* %28, i8** %3
br label %29
29: ; preds = %15, %14
%30 = load i8*, i8** %3
ret i8* %30
}

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
@PROT_EXEC = global i32 0
@MAP_SHARED = global i32 0
define i8* @xumem_map_phys(i64 %0, i64 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %2
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %10, %9
%12 = load i32, i32* @PROT_EXEC
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i64 %1, i32 %13, i32 %14, i32 %6, i64 %0)
%16 = tail call i32 @close(i32 %6)
br label %17
17: ; preds = %2, %8
%18 = phi i8* [ %15, %8 ], [ null, %2 ]
ret i8* %18
}