#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 */
unsigned int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int getpagesize();
unsigned int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                   unsigned int);
int /*<<< orphan*/ munmap(unsigned int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int rdReg(unsigned reg, unsigned *val) {
  int fd;
  int i, linenum, count = 0;
  unsigned int *map_base;
  unsigned int PHYADDR, length;
  unsigned char val_str[4];
  int pagesize = getpagesize();
  PHYADDR = reg;
  length = 1;
  fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd == -1) {
    printf("open /dev/mem failed\n");
    return -1;
  }
  map_base = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  PHYADDR & 0xfffff000);
  if (map_base == MAP_FAILED) {
    perror("mmap");
  };
  int pgoffset = (PHYADDR & 0x00000fff) >> 2;
  *val = map_base[pgoffset];
  close(fd);
  munmap(map_base, pagesize);
  return 0;
}


x86 assembly (clang, -O0):
<rdReg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x1,-0x30(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e0 <rdReg+0x70>
lea    0xe3a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <rdReg+0x104>
mov    -0x38(%rbp),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x2c(%rbp),%r9d
and    $0xfffff000,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <rdReg+0xca>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    -0x2c(%rbp),%eax
and    $0xfff,%eax
shr    $0x2,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1060 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdReg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %eax,%r15d
mov    0x2e47(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1219 <rdReg+0xa9>
mov    %eax,%r13d
mov    0x2e41(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%r9d
and    $0xfffff000,%r9d
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r15d,%esi
mov    %r13d,%r8d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2dc9(%rip),%rax
cmp    (%rax),%rbp
je     123d <rdReg+0xcd>
shr    $0x2,%ebx
and    $0x3ff,%ebx
mov    0x0(%rbp,%rbx,4),%eax
mov    %eax,(%r14)
mov    %r13d,%edi
call   1050 <close@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1060 <munmap@plt>
jmp    122b <rdReg+0xbb>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc5(%rip),%rdi
call   1080 <perror@plt>
jmp    11f4 <rdReg+0x84>

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

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
@.str.2 = [5 x i8] c"mmap\00"
@str = [21 x i8] c"open /dev/mem failed\00"
define i32 @rdReg(i32 %0, i32* nocapture writeonly %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %30
11: ; preds = %2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = and i32 %0, -4096
%17 = tail call i32* @mmap(i32* null, i32 %3, i32 %14, i32 %15, i32 %7, i32 %16)
%18 = load i32*, i32** @MAP_FAILED
%19 = icmp eq i32* %17, %18
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = lshr i32 %0, 2
%24 = and i32 %23, 1023
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %17, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %1
%28 = tail call i32 @close(i32 %7)
%29 = tail call i32 @munmap(i32* %17, i32 %3)
br label %30
30: ; preds = %22, %9
%31 = phi i32 [ -1, %9 ], [ 0, %22 ]
ret i32 %31
}