#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/, unsigned int, int, int /*<<< orphan*/, int,
                unsigned int);
int open(char *, int /*<<< orphan*/);
unsigned int sysconf(int /*<<< orphan*/);
int map_reg(void *reg, void **reg_mapped) {
  int fd;
  unsigned int addr_start, addr_offset;
  unsigned int pagesize, pagemask;
  void *pc;
  fd = open("/dev/mem", O_RDWR);
  if (fd < 0) {
    return -612;
  }
  pagesize = sysconf(_SC_PAGESIZE);
  pagemask = (~(pagesize - 1));
  addr_start = (unsigned int)reg & pagemask;
  addr_offset = (unsigned int)reg & ~pagemask;
  pc = (void *)mmap(0, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                    addr_start);
  if (pc == MAP_FAILED) {
    return -613;
  }
  close(fd);
  *reg_mapped = (pc + addr_offset);
  return 0;
}


x86 assembly (clang, -O0):
<map_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117e <map_reg+0x3e>
movl   $0xfffffd9c,-0x4(%rbp)
jmp    1228 <map_reg+0xe8>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
and    -0x2c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%esi
shl    $0x1,%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
xor    %edi,%edi
call   1030 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <map_reg+0xc8>
movl   $0xfffffd9b,-0x4(%rbp)
jmp    1228 <map_reg+0xe8>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rcx
mov    -0x24(%rbp),%eax
add    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_reg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11d6 <map_reg+0x96>
mov    %eax,%r13d
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%ebx
mov    %eax,%r9d
neg    %r9d
and    %r15d,%r9d
lea    (%rbx,%rbx,1),%esi
mov    0x2e68(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r13d,%r8d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dfd(%rip),%rax
cmp    %rbp,(%rax)
je     11de <map_reg+0x9e>
add    $0xffffffff,%ebx
and    %r15d,%ebx
mov    %r13d,%edi
call   1040 <close@plt>
add    %rbx,%rbp
mov    %rbp,(%r14)
jmp    11e4 <map_reg+0xa4>
mov    $0xfffffd9c,%r12d
jmp    11e4 <map_reg+0xa4>
mov    $0xfffffd9b,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @map_reg(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -612, i32* %3
br label %54
17: ; preds = %2
%18 = load i32, i32* @_SC_PAGESIZE
%19 = call i32 @sysconf(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = sub i32 %20, 1
%22 = xor i32 %21, -1
store i32 %22, i32* %10
%23 = load i8*, i8** %4
%24 = ptrtoint i8* %23 to i32
%25 = load i32, i32* %10
%26 = and i32 %24, %25
store i32 %26, i32* %7
%27 = load i8*, i8** %4
%28 = ptrtoint i8* %27 to i32
%29 = load i32, i32* %10
%30 = xor i32 %29, -1
%31 = and i32 %28, %30
store i32 %31, i32* %8
%32 = load i32, i32* %9
%33 = mul i32 %32, 2
%34 = load i32, i32* @PROT_READ
%35 = load i32, i32* @PROT_WRITE
%36 = or i32 %34, %35
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = call i64 @mmap(i32 0, i32 %33, i32 %36, i32 %37, i32 %38, i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %11
%42 = load i8*, i8** %11
%43 = load i8*, i8** @MAP_FAILED
%44 = icmp eq i8* %42, %43
br i1 %44, label %45, label %46
45: ; preds = %17
store i32 -613, i32* %3
br label %54
46: ; preds = %17
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = load i8*, i8** %11
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = getelementptr i8, i8* %49, i64 %51
%53 = load i8**, i8*** %5
store i8* %52, i8** %53
store i32 0, i32* %3
br label %54
54: ; preds = %46, %45, %16
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @map_reg(i8* %0, i8** nocapture writeonly %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %2
%7 = load i32, i32* @_SC_PAGESIZE
%8 = tail call i32 @sysconf(i32 %7)
%9 = sub i32 0, %8
%10 = ptrtoint i8* %0 to i64
%11 = trunc i64 %10 to i32
%12 = and i32 %9, %11
%13 = shl i32 %8, 1
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i64 @mmap(i32 0, i32 %13, i32 %16, i32 %17, i32 %4, i32 %12)
%19 = inttoptr i64 %18 to i8*
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %20, %19
br i1 %21, label %28, label %22
22: ; preds = %6
%23 = add i32 %8, -1
%24 = and i32 %23, %11
%25 = tail call i32 @close(i32 %4)
%26 = zext i32 %24 to i64
%27 = getelementptr i8, i8* %19, i64 %26
store i8* %27, i8** %1
br label %28
28: ; preds = %6, %2, %22
%29 = phi i32 [ 0, %22 ], [ -612, %2 ], [ -613, %6 ]
ret i32 %29
}