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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(unsigned char);
int /*<<< orphan*/ stderr;
int strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  void *map_base;
  off_t map_addr;
  off_t size;
  int fp;
  size_t i;
  if (argc < 2) {
    fprintf(stderr, "Usage %s: <start_address> <size> > file\n", argv[0]);
    return -1;
  }
  map_addr = strtoul(argv[1], NULL, 0);
  size = strtoul(argv[2], NULL, 0);
  fprintf(stderr, "Address 0x%x Size 0x%x\n", (unsigned long)map_addr, size);
  fp = open("/dev/mem", O_RDONLY);
  if (fp == -1) {
    fprintf(stderr, "Can 't open /dev/mem!");
  }
  map_base = mmap(NULL, size, PROT_READ, MAP_SHARED, fp, map_addr);
  for (i = 0; i < size; i++) {
    putchar(*((unsigned char *)map_base + i));
  }
  munmap(map_base, size);
  close(fp);
}


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   $0x2,-0x8(%rbp)
jge    11ba <main+0x4a>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b6 <main+0x146>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x20(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
movslq -0x1c(%rbp),%rdx
mov    -0x20(%rbp),%ecx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    123d <main+0xcd>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
mov    0x2d98(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmp    %rcx,%rax
jae    12a2 <main+0x132>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    126f <main+0xff>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11a8 <main+0x38>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe6a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%r13d
jmp    1276 <main+0x106>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,%r12d
mov    0x2df9(%rip),%rbx
mov    (%rbx),%edi
movslq %r14d,%rdx
lea    0xe4e(%rip),%rsi
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1211 <main+0xa1>
mov    (%rbx),%edi
lea    0xe40(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
xor    %r13d,%r13d
xor    %edi,%edi
mov    %r12d,%esi
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
test   %r12d,%r12d
je     1263 <main+0xf3>
movslq %r12d,%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%r14,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1250 <main+0xe0>
mov    %r14,%rdi
mov    %r12d,%esi
call   1070 <munmap@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"Usage %s: <start_address> <size> > file\0A\00"
@.str.1 = [24 x i8] c"Address 0x%x Size 0x%x\0A\00"
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Can 't open /dev/mem!\00"
@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
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %67
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strtoul(i8* %22, i32* null, i32 0)
store i32 %23, i32* %7
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @strtoul(i8* %26, i32* null, i32 0)
store i32 %27, i32* %8
%28 = load i32, i32* @stderr
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = load i32, i32* %8
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %30, i32 %31)
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %19
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %19
%41 = load i32, i32* %8
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_SHARED
%44 = load i32, i32* %9
%45 = load i32, i32* %7
%46 = call i8* @mmap(i32* null, i32 %41, i32 %42, i32 %43, i32 %44, i32 %45)
store i8* %46, i8** %6
store i64 0, i64* %10
br label %47
47: ; preds = %58, %40
%48 = load i64, i64* %10
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = icmp ult i64 %48, %50
br i1 %51, label %52, label %61
52: ; preds = %47
%53 = load i8*, i8** %6
%54 = load i64, i64* %10
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i8, i8* %55
%57 = call i32 @putchar(i8 zeroext %56)
br label %58
58: ; preds = %52
%59 = load i64, i64* %10
%60 = add i64 %59, 1
store i64 %60, i64* %10
br label %47
61: ; preds = %47
%62 = load i8*, i8** %6
%63 = load i32, i32* %8
%64 = call i32 @munmap(i8* %62, i32 %63)
%65 = load i32, i32* %9
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %61, %13
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"Usage %s: <start_address> <size> > file\0A\00"
@.str.1 = [24 x i8] c"Address 0x%x Size 0x%x\0A\00"
@.str.2 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.3 = [22 x i8] c"Can 't open /dev/mem!\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %41
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strtoul(i8* nocapture %10, i32* null, i32 0)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @strtoul(i8* nocapture %13, i32* null, i32 0)
%15 = load i32, i32* @stderr
%16 = sext i32 %11 to i64
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %16, i32 %14)
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %8
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %24
24: ; preds = %21, %8
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32* null, i32 %14, i32 %25, i32 %26, i32 %19, i32 %11)
%28 = icmp eq i32 %14, 0
br i1 %28, label %38, label %29
29: ; preds = %24
%30 = sext i32 %14 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ %36, %31 ], [ 0, %29 ]
%33 = getelementptr inbounds i8, i8* %27, i64 %32
%34 = load i8, i8* %33
%35 = tail call i32 @putchar(i8 zeroext %34)
%36 = add nuw i64 %32, 1
%37 = icmp eq i64 %36, %30
br i1 %37, label %38, label %31
38: ; preds = %31, %24
%39 = tail call i32 @munmap(i8* %27, i32 %14)
%40 = tail call i32 @close(i32 %19)
br label %41
41: ; preds = %38, %4
%42 = phi i32 [ -1, %4 ], [ 0, %38 ]
ret i32 %42
}