#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 atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(int /*<<< orphan*/ *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int main(int argc, char *argv[]) {
  char *addr;
  int fd;
  int i;
  int size;
  fd = open("/dev/mem", O_RDONLY);
  size = 0x20000;
  if (argc)
    size = atoi(argv[1]);
  addr = (char *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0x43000000);
  for (i = 0; i < 0x20000; i++)
    putchar(addr[i]);
  munmap(NULL, 0x20000);
  close(fd);
  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    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x20000,-0x24(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11af <main+0x4f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x43000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x20000,-0x20(%rbp)
jge    120e <main+0xae>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e2 <main+0x82>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x20000,%esi
call   1060 <munmap@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
test   %ebp,%ebp
je     1192 <main+0x32>
mov    0x8(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%esi
jmp    1197 <main+0x37>
mov    $0x20000,%esi
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r14d,%r8d
mov    $0x43000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
xchg   %ax,%ax
movsbl (%rbx,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    $0x20000,%rbp
jne    11c0 <main+0x60>
xor    %edi,%edi
mov    $0x20000,%esi
call   1060 <munmap@plt>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%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* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %7
store i32 131072, i32* %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %9
br label %19
19: ; preds = %14, %2
%20 = load i32, i32* %9
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %7
%24 = call i64 @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %23, i32 1124073472)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %6
store i32 0, i32* %8
br label %26
26: ; preds = %36, %19
%27 = load i32, i32* %8
%28 = icmp slt i32 %27, 131072
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call i32 @putchar(i8 signext %34)
br label %36
36: ; preds = %29
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %26
39: ; preds = %26
%40 = call i32 @munmap(i32* null, i32 131072)
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %0, 0
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
br label %10
10: ; preds = %6, %2
%11 = phi i32 [ %9, %6 ], [ 131072, %2 ]
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i64 @mmap(i32* null, i32 %11, i32 %12, i32 %13, i32 %4, i32 1124073472)
%15 = inttoptr i64 %14 to i8*
br label %16
16: ; preds = %10, %16
%17 = phi i64 [ 0, %10 ], [ %21, %16 ]
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = tail call i32 @putchar(i8 signext %19)
%21 = add nuw nsw i64 %17, 1
%22 = icmp eq i64 %21, 131072
br i1 %22, label %23, label %16
23: ; preds = %16
%24 = tail call i32 @munmap(i32* null, i32 131072)
%25 = tail call i32 @close(i32 %4)
ret i32 0
}