#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 */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ exit(int);
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned int);
unsigned int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int fd;
  void *ptr;
  unsigned addr, page_addr, page_offset;
  unsigned page_size = sysconf(_SC_PAGESIZE);
  if (argc != 2) {
    usage(argv[0]);
    exit(-1);
  }
  fd = open("/dev/mem", O_RDONLY);
  if (fd < 1) {
    perror(argv[0]);
    exit(-1);
  }
  addr = strtoul(argv[1], NULL, 0);
  page_addr = (addr & ~(page_size - 1));
  page_offset = addr - page_addr;
  ptr = mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd,
             (addr & ~(page_size - 1)));
  if ((int)ptr == -1) {
    perror(argv[0]);
    exit(-1);
  }
  printf("0x%08x\n", *((unsigned *)(ptr + page_offset)));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <usage@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11ff <main+0x7f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    0x2da6(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x30(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    $0xffffffff,%eax
jne    1288 <main+0x108>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,1),%esi
lea    0xd70(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
cmp    $0x2,%ebx
jne    122e <main+0xae>
mov    %eax,%r15d
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    121c <main+0x9c>
mov    %eax,%r12d
mov    0x8(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,%ebp
mov    %r15d,%ebx
neg    %ebx
and    %eax,%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    0x2dff(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r12d,%r8d
mov    %ebx,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffff,%eax
je     121c <main+0x9c>
sub    %ebx,%ebp
mov    (%rax,%rbp,1),%esi
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    (%r14),%rdi
call   1050 <usage@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [8 x i8] c"0x%08x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @_SC_PAGESIZE
%13 = call i32 @sysconf(i32 %12)
store i32 %13, i32* %11
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @usage(i8* %19)
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = call i32 @perror(i8* %30)
%32 = call i32 @exit(i32 -1)
unreachable
33: ; preds = %22
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @strtoul(i8* %36, i32* null, i32 0)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = load i32, i32* %11
%40 = sub i32 %39, 1
%41 = xor i32 %40, -1
%42 = and i32 %38, %41
store i32 %42, i32* %9
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = sub i32 %43, %44
store i32 %45, i32* %10
%46 = load i32, i32* %11
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @MAP_SHARED
%49 = load i32, i32* %6
%50 = load i32, i32* %8
%51 = load i32, i32* %11
%52 = sub i32 %51, 1
%53 = xor i32 %52, -1
%54 = and i32 %50, %53
%55 = call i8* @mmap(i32* null, i32 %46, i32 %47, i32 %48, i32 %49, i32 %54)
store i8* %55, i8** %7
%56 = load i8*, i8** %7
%57 = ptrtoint i8* %56 to i32
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %65
59: ; preds = %33
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load i8*, i8** %61
%63 = call i32 @perror(i8* %62)
%64 = call i32 @exit(i32 -1)
unreachable
65: ; preds = %33
%66 = load i8*, i8** %7
%67 = load i32, i32* %10
%68 = zext i32 %67 to i64
%69 = getelementptr i8, i8* %66, i64 %68
%70 = bitcast i8* %69 to i32*
%71 = load i32, i32* %70
%72 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %71)
ret i32 0
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.1 = [8 x i8] c"0x%08x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i8*, i8** %1
%16 = tail call i32 @perror(i8* %15)
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %10
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @strtoul(i8* nocapture %20, i32* null, i32 0)
%22 = sub i32 0, %4
%23 = and i32 %21, %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_SHARED
%26 = tail call i8* @mmap(i32* null, i32 %4, i32 %24, i32 %25, i32 %12, i32 %23)
%27 = ptrtoint i8* %26 to i64
%28 = trunc i64 %27 to i32
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %18
%31 = load i8*, i8** %1
%32 = tail call i32 @perror(i8* %31)
%33 = tail call i32 @exit(i32 -1)
unreachable
34: ; preds = %18
%35 = sub i32 %21, %23
%36 = zext i32 %35 to i64
%37 = getelementptr i8, i8* %26, i64 %36
%38 = bitcast i8* %37 to i32*
%39 = load i32, i32* %38
%40 = tail call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %39)
ret i32 0
}