#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ close(int);
int *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
          int, int);
scalar_t__ munmap(int *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int sysconf(int /*<<< orphan*/);
uint32_t readMem(uint32_t addr) {
  uint32_t *pmem = NULL;
  uint32_t page_addr = addr & ~(sysconf(_SC_PAGESIZE) - 1);
  uint32_t offs = addr - page_addr;
  int ret = 0, fd;
  if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
    perror("error open /dev/mem:");
    return 0;
  }
  pmem =
      mmap(NULL, sysconf(_SC_PAGESIZE), PROT_WRITE, MAP_SHARED, fd, page_addr);
  if (!pmem) {
    perror("error mmap:");
    goto error;
  }
  ret = pmem[offs / sizeof(uint32_t)];
error:
  if (pmem && munmap(pmem, sysconf(_SC_PAGESIZE)) != 0) {
    perror("error munmap:");
    return 0;
  }
  if (close(fd) != 0) {
    perror("error close:");
    return 0;
  }
  return ret;
}


x86 assembly (clang, -O0):
<readMem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
lea    0xe40(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11e9 <readMem+0x89>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <readMem+0x16c>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%esi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x14(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123c <readMem+0xdc>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
jmp    124e <readMem+0xee>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
shr    $0x2,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     129c <readMem+0x13c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1050 <munmap@plt>
cmp    $0x0,%rax
je     129c <readMem+0x13c>
lea    0xd9f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <readMem+0x16c>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     12c6 <readMem+0x166>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12cc <readMem+0x16c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r12d
mov    0x2e5b(%rip),%r13
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebx
mov    0x2e59(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe67(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1229 <readMem+0xc9>
mov    %eax,%r15d
neg    %ebx
and    %r12d,%ebx
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    0x2e02(%rip),%rcx
mov    (%rcx),%edx
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %eax,%esi
mov    %r15d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
je     1232 <readMem+0xd2>
mov    %rax,%rbp
sub    %ebx,%r12d
movslq %r12d,%rax
and    $0xfffffffffffffffc,%rax
mov    0x0(%rbp,%rax,1),%r14d
mov    0x0(%r13),%edi
call   1080 <sysconf@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1050 <munmap@plt>
test   %rax,%rax
jne    1249 <readMem+0xe9>
mov    %r15d,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1240 <readMem+0xe0>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
jmp    1250 <readMem+0xf0>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
jmp    120a <readMem+0xaa>
lea    0xdf1(%rip),%rdi
jmp    1250 <readMem+0xf0>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
xor    %r14d,%r14d
jmp    1217 <readMem+0xb7>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [21 x i8] c"error open /dev/mem:\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [12 x i8] c"error mmap:\00"
@.str.3 = [14 x i8] c"error munmap:\00"
@.str.4 = [13 x i8] c"error close:\00"
define i32 @readMem(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32* null, i32** %4
%9 = load i32, i32* %3
%10 = load i32, i32* @_SC_PAGESIZE
%11 = call i32 @sysconf(i32 %10)
%12 = sub nsw i32 %11, 1
%13 = xor i32 %12, -1
%14 = and i32 %9, %13
store i32 %14, i32* %5
%15 = load i32, i32* %3
%16 = load i32, i32* %5
%17 = sub nsw i32 %15, %16
store i32 %17, i32* %6
store i32 0, i32* %7
%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* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %63
25: ; preds = %1
%26 = load i32, i32* @_SC_PAGESIZE
%27 = call i32 @sysconf(i32 %26)
%28 = load i32, i32* @PROT_WRITE
%29 = load i32, i32* @MAP_SHARED
%30 = load i32, i32* %8
%31 = load i32, i32* %5
%32 = call i32* @mmap(i32* null, i32 %27, i32 %28, i32 %29, i32 %30, i32 %31)
store i32* %32, i32** %4
%33 = load i32*, i32** %4
%34 = icmp ne i32* %33, null
br i1 %34, label %37, label %35
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %44
37: ; preds = %25
%38 = load i32*, i32** %4
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = udiv i64 %40, 4
%42 = getelementptr inbounds i32, i32* %38, i64 %41
%43 = load i32, i32* %42
store i32 %43, i32* %7
br label %44
44: ; preds = %37, %35
%45 = load i32*, i32** %4
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = load i32*, i32** %4
%49 = load i32, i32* @_SC_PAGESIZE
%50 = call i32 @sysconf(i32 %49)
%51 = call i64 @munmap(i32* %48, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %63
55: ; preds = %47, %44
%56 = load i32, i32* %8
%57 = call i64 @close(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %63
61: ; preds = %55
%62 = load i32, i32* %7
store i32 %62, i32* %2
br label %63
63: ; preds = %61, %59, %53, %23
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [21 x i8] c"error open /dev/mem:\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [12 x i8] c"error mmap:\00"
@.str.3 = [14 x i8] c"error munmap:\00"
@.str.4 = [13 x i8] c"error close:\00"
define i32 @readMem(i32 %0) {
%2 = load i32, i32* @_SC_PAGESIZE
%3 = tail call i32 @sysconf(i32 %2)
%4 = sub i32 0, %3
%5 = and i32 %4, %0
%6 = sub nsw i32 %0, %5
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %34, label %12
12: ; preds = %1
%13 = load i32, i32* @_SC_PAGESIZE
%14 = tail call i32 @sysconf(i32 %13)
%15 = load i32, i32* @PROT_WRITE
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i32* @mmap(i32* null, i32 %14, i32 %15, i32 %16, i32 %10, i32 %5)
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %30
21: ; preds = %12
%22 = sext i32 %6 to i64
%23 = lshr i64 %22, 2
%24 = getelementptr inbounds i32, i32* %17, i64 %23
%25 = load i32, i32* %24
%26 = load i32, i32* @_SC_PAGESIZE
%27 = tail call i32 @sysconf(i32 %26)
%28 = tail call i64 @munmap(i32* %17, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %19, %21
%31 = phi i32 [ 0, %19 ], [ %25, %21 ]
%32 = tail call i64 @close(i32 %10)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30, %21, %1
%35 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %21 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %30 ]
%36 = tail call i32 @perror(i8* %35)
br label %37
37: ; preds = %34, %30
%38 = phi i32 [ %31, %30 ], [ 0, %34 ]
ret i32 %38
}