#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*/ uint8_t;
typedef long uint32_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ getpagesize();
scalar_t__ mmap(void *, int, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int);
uint8_t *dynamic_mmap(void *addr) {
  uint8_t *rv;
  int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  rv = (uint8_t *)mmap(
      addr, 0x01, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd,
      (off_t)((uint32_t)addr & ((long)~(((size_t)getpagesize()) - 1))));
  if (rv == MAP_FAILED) {
    exit(-1);
  }
  rv += (uint32_t)((uint32_t)addr & ~((long)~(((size_t)getpagesize()) - 1)));
  return rv;
}


x86 assembly (clang, -O0):
<dynamic_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    %rax,%rsi
mov    -0x20(%rbp),%rax
sub    $0x1,%rsi
xor    $0xffffffffffffffff,%rsi
and    %rsi,%rax
mov    %eax,%r9d
mov    $0x1,%esi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dd1(%rip),%rcx
cmp    (%rcx),%rax
jne    11fa <dynamic_mmap+0xba>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <getpagesize@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dynamic_mmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e87(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%r15d
mov    0x2e86(%rip),%rax
mov    0x2e47(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e64(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1050 <getpagesize@plt>
neg    %eax
mov    %r14d,%r9d
and    %eax,%r9d
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r15d,%r8d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e05(%rip),%rax
cmp    %rbx,(%rax)
je     11d5 <dynamic_mmap+0x95>
xor    %eax,%eax
call   1050 <getpagesize@plt>
add    $0xffffffffffffffff,%rax
and    %r14,%rax
lea    (%rbx,%rax,4),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1060 <exit@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_FAILED = global i32* null
define i32* @dynamic_mmap(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_SYNC
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i8*, i8** %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_SHARED
%14 = load i32, i32* %4
%15 = load i8*, i8** %2
%16 = ptrtoint i8* %15 to i64
%17 = call i64 (...) @getpagesize()
%18 = sub i64 %17, 1
%19 = xor i64 %18, -1
%20 = and i64 %16, %19
%21 = trunc i64 %20 to i32
%22 = call i64 @mmap(i8* %9, i32 1, i32 %12, i32 %13, i32 %14, i32 %21)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %3
%24 = load i32*, i32** %3
%25 = load i32*, i32** @MAP_FAILED
%26 = icmp eq i32* %24, %25
br i1 %26, label %27, label %29
27: ; preds = %1
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %1
%30 = load i8*, i8** %2
%31 = ptrtoint i8* %30 to i64
%32 = call i64 (...) @getpagesize()
%33 = sub i64 %32, 1
%34 = xor i64 %33, -1
%35 = xor i64 %34, -1
%36 = and i64 %31, %35
%37 = load i32*, i32** %3
%38 = getelementptr inbounds i32, i32* %37, i64 %36
store i32* %38, i32** %3
%39 = load i32*, i32** %3
ret i32* %39
}

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_FAILED = global i32* null
define i32* @dynamic_mmap(i8* %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 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_SHARED
%10 = ptrtoint i8* %0 to i64
%11 = tail call i64 (...) @getpagesize()
%12 = sub i64 0, %11
%13 = and i64 %12, %10
%14 = trunc i64 %13 to i32
%15 = tail call i64 @mmap(i8* %0, i32 1, i32 %8, i32 %9, i32 %5, i32 %14)
%16 = inttoptr i64 %15 to i32*
%17 = load i32*, i32** @MAP_FAILED
%18 = icmp eq i32* %17, %16
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %1
%22 = tail call i64 (...) @getpagesize()
%23 = add i64 %22, -1
%24 = and i64 %23, %10
%25 = getelementptr inbounds i32, i32* %16, i64 %24
ret i32* %25
}