#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 O_RDWR;
int O_SYNC;
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);
unsigned int sysconf(int /*<<< orphan*/);
int map_reg(void *reg, volatile void **reg_mapped) {
  int fd;
  unsigned int addr_start, addr_offset;
  unsigned int pagesize, pagemask;
  void *pc;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 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
mov    0x2e70(%rip),%rax
or     (%rax),%esi
lea    0xe97(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1186 <map_reg+0x46>
movl   $0xfffffd9c,-0x4(%rbp)
jmp    1230 <map_reg+0xf0>
mov    0x2e3b(%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    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2de6(%rip),%rax
or     (%rax),%edx
mov    0x2e0d(%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    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1210 <map_reg+0xd0>
movl   $0xfffffd9b,-0x4(%rbp)
jmp    1230 <map_reg+0xf0>
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    0x2e71(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe96(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11df <map_reg+0x9f>
mov    %eax,%r13d
mov    0x2e4b(%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    0x2e5f(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e3d(%rip),%rax
mov    (%rax),%ecx
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r13d,%r8d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dec(%rip),%rax
cmp    %rbp,(%rax)
je     11e7 <map_reg+0xa7>
add    $0xffffffff,%ebx
and    %r15d,%ebx
mov    %r13d,%edi
call   1040 <close@plt>
add    %rbx,%rbp
mov    %rbp,(%r14)
jmp    11ed <map_reg+0xad>
mov    $0xfffffd9c,%r12d
jmp    11ed <map_reg+0xad>
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
@O_SYNC = 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 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -612, i32* %3
br label %55
18: ; preds = %2
%19 = load i32, i32* @_SC_PAGESIZE
%20 = call i32 @sysconf(i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = sub i32 %21, 1
%23 = xor i32 %22, -1
store i32 %23, i32* %10
%24 = load i8*, i8** %4
%25 = ptrtoint i8* %24 to i32
%26 = load i32, i32* %10
%27 = and i32 %25, %26
store i32 %27, i32* %7
%28 = load i8*, i8** %4
%29 = ptrtoint i8* %28 to i32
%30 = load i32, i32* %10
%31 = xor i32 %30, -1
%32 = and i32 %29, %31
store i32 %32, i32* %8
%33 = load i32, i32* %9
%34 = mul i32 %33, 2
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %35, %36
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %6
%40 = load i32, i32* %7
%41 = call i64 @mmap(i32 0, i32 %34, i32 %37, i32 %38, i32 %39, i32 %40)
%42 = inttoptr i64 %41 to i8*
store i8* %42, i8** %11
%43 = load i8*, i8** %11
%44 = load i8*, i8** @MAP_FAILED
%45 = icmp eq i8* %43, %44
br i1 %45, label %46, label %47
46: ; preds = %18
store i32 -613, i32* %3
br label %55
47: ; preds = %18
%48 = load i32, i32* %6
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %11
%51 = load i32, i32* %8
%52 = zext i32 %51 to i64
%53 = getelementptr i8, i8* %50, i64 %52
%54 = load i8**, i8*** %5
store i8* %53, i8** %54
store i32 0, i32* %3
br label %55
55: ; preds = %47, %46, %17
%56 = load i32, i32* %3
ret i32 %56
}

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