#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*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(char);
int main(void) {
  char *addr;
  int fd;
  int i;
  fd = open("/dev/mem", O_RDONLY);
  addr = (char *)mmap(NULL, 0x20000, PROT_READ, MAP_SHARED, fd, 0x43000000);
  for (i = 0; i < 0x20000; i++)
    putchar(addr[i]);
  munmap(addr, 0x20000);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x20000,%esi
mov    $0x43000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x20000,-0x18(%rbp)
jge    11d8 <main+0x88>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ac <main+0x5c>
mov    -0x10(%rbp),%rdi
mov    $0x20000,%esi
call   1060 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
mov    0x2e6c(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x20000,%esi
mov    %r14d,%r8d
mov    $0x43000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
movsbl (%rbx,%rbp,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    $0x20000,%rbp
jne    11a0 <main+0x50>
mov    %rbx,%rdi
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() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @MAP_SHARED
%9 = load i32, i32* %3
%10 = call i64 @mmap(i32* null, i32 131072, i32 %7, i32 %8, i32 %9, i32 1124073472)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %2
store i32 0, i32* %4
br label %12
12: ; preds = %22, %0
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 131072
br i1 %14, label %15, label %25
15: ; preds = %12
%16 = load i8*, i8** %2
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = call i32 @putchar(i8 signext %20)
br label %22
22: ; preds = %15
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %12
25: ; preds = %12
%26 = load i8*, i8** %2
%27 = call i32 @munmap(i8* %26, i32 131072)
%28 = load i32, i32* %3
%29 = call i32 @close(i32 %28)
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() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @MAP_SHARED
%5 = tail call i64 @mmap(i32* null, i32 131072, i32 %3, i32 %4, i32 %2, i32 1124073472)
%6 = inttoptr i64 %5 to i8*
br label %7
7: ; preds = %0, %7
%8 = phi i64 [ 0, %0 ], [ %12, %7 ]
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = load i8, i8* %9
%11 = tail call i32 @putchar(i8 signext %10)
%12 = add nuw nsw i64 %8, 1
%13 = icmp eq i64 %12, 131072
br i1 %13, label %14, label %7
14: ; preds = %7
%15 = tail call i32 @munmap(i8* %6, i32 131072)
%16 = tail call i32 @close(i32 %2)
ret i32 0
}