#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 /*<<< orphan*/ val;
typedef int uint32_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
          int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int /*<<< orphan*/ strtoul(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  int fd;
  uint32_t val;
  off_t off;
  assert(argc == 3);
  errno = 0;
  off = strtoul(argv[1], NULL, 16);
  assert(errno == 0);
  assert((fd = open("/dev/mem", O_RDWR)) != -1);
  if (argv[2][0] == 'm') {
    uint32_t *p;
    printf("Accessing, via pointer indirection:");
    assert((p = mmap(NULL, sizeof(val), PROT_READ, MAP_SHARED, fd, off)) !=
           (uint32_t *)-1);
    val = *p;
  } else {
    printf("Accessing, via seek/read:");
    assert(lseek(fd, off, SEEK_SET) != -1);
    assert(read(fd, &val, sizeof(val)) == sizeof(val));
  }
  printf("value: 0x%x\n", val);
  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)
cmpl   $0x3,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2e2a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e03(%rip),%rax
cmpq   $0x0,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe1a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl (%rax),%eax
cmp    $0x6d,%eax
jne    1271 <main+0x101>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
mov    0x2db8(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    12c7 <main+0x157>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cltq
cmp    $0x4,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%esi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x3,%edi
sete   %al
mov    %eax,%edi
call   1070 <assert@plt>
mov    0x2e37(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <strtoul@plt>
mov    %eax,%r14d
xor    %edi,%edi
cmpq   $0x0,0x0(%rbp)
sete   %dil
call   1070 <assert@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1070 <assert@plt>
mov    0x10(%rbx),%rax
cmpb   $0x6d,(%rax)
jne    1228 <main+0xb8>
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
setne  %al
jmp    1271 <main+0x101>
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <lseek@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1070 <assert@plt>
lea    0xc(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
sete   %al
movzbl %al,%edi
call   1070 <assert@plt>
mov    (%rbx),%esi
lea    0xdc5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [36 x i8] c"Accessing, via pointer indirection:\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [26 x i8] c"Accessing, via seek/read:\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"value: 0x%x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 3
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
store i64 0, i64* @errno
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strtoul(i8* %16, i32* null, i32 16)
store i32 %17, i32* %8
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22)
store i32 %23, i32* %6
%24 = icmp ne i32 %23, -1
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 109
br i1 %33, label %34, label %46
34: ; preds = %2
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* %6
%39 = load i32, i32* %8
%40 = call i32* @mmap(i32* null, i32 4, i32 %36, i32 %37, i32 %38, i32 %39)
store i32* %40, i32** %9
%41 = icmp ne i32* %40, inttoptr (i64 -1 to i32*)
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32*, i32** %9
%45 = load i32, i32* %44
store i32 %45, i32* %7
br label %61
46: ; preds = %2
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %6
%49 = load i32, i32* %8
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @lseek(i32 %48, i32 %49, i32 %50)
%52 = icmp ne i32 %51, -1
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = load i32, i32* %6
%56 = call i32 @read(i32 %55, i32* %7, i32 4)
%57 = sext i32 %56 to i64
%58 = icmp eq i64 %57, 4
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
br label %61
61: ; preds = %46, %34
%62 = load i32, i32* %7
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %62)
ret i32 0
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [36 x i8] c"Accessing, via pointer indirection:\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [26 x i8] c"Accessing, via seek/read:\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"value: 0x%x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 3
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
store i64 0, i64* @errno
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strtoul(i8* nocapture %9, i32* null, i32 16)
%11 = load i64, i64* @errno
%12 = icmp eq i64 %11, 0
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp ne i32 %16, -1
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 2
%21 = load i8*, i8** %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 109
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i32* @mmap(i32* null, i32 4, i32 %26, i32 %27, i32 %16, i32 %10)
%29 = icmp ne i32* %28, inttoptr (i64 -1 to i32*)
br label %39
30: ; preds = %2
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %16, i32 %10, i32 %32)
%34 = icmp ne i32 %33, -1
%35 = zext i1 %34 to i32
%36 = tail call i32 @assert(i32 %35)
%37 = call i32 @read(i32 %16, i32* %3, i32 4)
%38 = icmp eq i32 %37, 4
br label %39
39: ; preds = %30, %24
%40 = phi i1 [ %38, %30 ], [ %29, %24 ]
%41 = phi i32* [ %3, %30 ], [ %28, %24 ]
%42 = zext i1 %40 to i32
%43 = tail call i32 @assert(i32 %42)
%44 = load i32, i32* %41
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %44)
ret i32 0
}