#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 unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int munmap(void *, unsigned long);
int open(char *, int);
unsigned long reg_read(off_t addr) {
  int memfd;
  void *map_base, *virt_addr;
  unsigned long val;
  if ((memfd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
    exit(1);
  map_base = mmap(0, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, memfd,
                  addr & ~(4096UL - 1));
  if (map_base == (void *)-1)
    exit(1);
  virt_addr = map_base + (addr & (4096UL - 1));
  val = *((unsigned long *)virt_addr);
  if (munmap(map_base, 4096UL) == -1)
    exit(1);
  close(memfd);
  return val;
}


x86 assembly (clang, -O0):
<reg_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1190 <reg_read+0x40>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
mov    0x2e3f(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x8(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %edi,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    11e5 <reg_read+0x95>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1227 <reg_read+0xd7>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reg_read>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e7b(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e2 <reg_read+0x92>
mov    %eax,%ebp
mov    0x2e76(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%r9
and    $0xfffffffffffff000,%r9
mov    $0x1000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11e2 <reg_read+0x92>
and    $0xfff,%ebx
mov    (%rax,%rbx,1),%rbx
mov    $0x1000,%esi
mov    %rax,%rdi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
je     11e2 <reg_read+0x92>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%edi
call   1070 <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
define i64 @reg_read(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
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 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %3
%20 = load i64, i64* %2
%21 = and i64 %20, -4096
%22 = call i8* @mmap(i32 0, i64 4096, i32 %17, i32 %18, i32 %19, i64 %21)
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = load i8*, i8** %4
%29 = load i64, i64* %2
%30 = and i64 %29, 4095
%31 = getelementptr i8, i8* %28, i64 %30
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = bitcast i8* %32 to i64*
%34 = load i64, i64* %33
store i64 %34, i64* %6
%35 = load i8*, i8** %4
%36 = call i32 @munmap(i8* %35, i64 4096)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %27
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %27
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = load i64, i64* %6
ret i64 %43
}

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
define i64 @reg_read(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 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = and i64 %0, -4096
%15 = tail call i8* @mmap(i32 0, i64 4096, i32 %12, i32 %13, i32 %5, i64 %14)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = and i64 %0, 4095
%21 = getelementptr i8, i8* %15, i64 %20
%22 = bitcast i8* %21 to i64*
%23 = load i64, i64* %22
%24 = tail call i32 @munmap(i8* %15, i64 4096)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %19
%29 = tail call i32 @close(i32 %5)
ret i64 %23
}