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

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, void *);
int munmap(void *, unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(unsigned char);
int /*<<< orphan*/ stderr;
void *strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd;
  void *map_base;
  off_t start;
  unsigned int len;
  if (argc != 3) {
    fprintf(stderr,
            "Summary: Dump bytes from /dev/mem to stdout\n"
            "Usage: %s <start_addr> <num_bytes>\n"
            "start_addr: Start address to dump.  Must be page aligned.\n"
            "num_bytes: Number of bytes to dump.\n",
            argv[0]);
    exit(1);
  }
  start = strtoul(argv[1], NULL, 0);
  len = strtoul(argv[2], NULL, 0);
  if ((fd = open("/dev/mem", O_RDONLY)) == 1)
    return 1;
  map_base = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, start);
  if (map_base == MAP_FAILED)
    return 2;
  for (unsigned int i = 0; i < len; i++) {
    putchar(*((unsigned char *)(map_base + i)));
  }
  if (munmap(map_base, len) == -1)
    return 3;
  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)
cmpl   $0x3,-0x8(%rbp)
je     11c6 <main+0x46>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%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    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x1,%eax
jne    1220 <main+0xa0>
movl   $0x1,-0x4(%rbp)
jmp    12cb <main+0x14b>
mov    -0x2c(%rbp),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d6b(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <main+0xea>
movl   $0x2,-0x4(%rbp)
jmp    12cb <main+0x14b>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    129b <main+0x11b>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%ecx
movzbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1271 <main+0xf1>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0x13c>
movl   $0x3,-0x4(%rbp)
jmp    12cb <main+0x14b>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1264 <main+0xe4>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <strtoul@plt>
mov    %rax,%r15
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    0xeea(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r12d
mov    $0x1,%eax
cmp    $0x1,%r12d
je     125b <main+0xdb>
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2dfd(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r12d,%r8d
mov    %r14,%r9
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2db9(%rip),%rax
cmp    (%rax),%r14
je     124f <main+0xcf>
test   %r15d,%r15d
je     1233 <main+0xb3>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1220 <main+0xa0>
mov    %r14,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
je     1256 <main+0xd6>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    125b <main+0xdb>
mov    $0x2,%eax
jmp    125b <main+0xdb>
mov    $0x3,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd89(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [174 x i8] c"Summary: Dump bytes from /dev/mem to stdout\0AUsage: %s <start_addr> <num_bytes>\0Astart_addr: Start address to dump. Must be page aligned.\0Anum_bytes: Number of bytes to dump.\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %20
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 @fprintf(i32 %14, i8* getelementptr inbounds ([174 x i8], [174 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i8* @strtoul(i8* %23, i32* null, i32 0)
store i8* %24, i8** %8
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i8* @strtoul(i8* %27, i32* null, i32 0)
%29 = ptrtoint i8* %28 to i32
store i32 %29, i32* %9
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 %31, i32* %6
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %20
store i32 1, i32* %3
br label %69
34: ; preds = %20
%35 = load i32, i32* %9
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @MAP_SHARED
%38 = load i32, i32* %6
%39 = load i8*, i8** %8
%40 = call i8* @mmap(i32* null, i32 %35, i32 %36, i32 %37, i32 %38, i8* %39)
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = load i8*, i8** @MAP_FAILED
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %45
44: ; preds = %34
store i32 2, i32* %3
br label %69
45: ; preds = %34
store i32 0, i32* %10
br label %46
46: ; preds = %57, %45
%47 = load i32, i32* %10
%48 = load i32, i32* %9
%49 = icmp ult i32 %47, %48
br i1 %49, label %50, label %60
50: ; preds = %46
%51 = load i8*, i8** %7
%52 = load i32, i32* %10
%53 = zext i32 %52 to i64
%54 = getelementptr i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = call i32 @putchar(i8 zeroext %55)
br label %57
57: ; preds = %50
%58 = load i32, i32* %10
%59 = add i32 %58, 1
store i32 %59, i32* %10
br label %46
60: ; preds = %46
%61 = load i8*, i8** %7
%62 = load i32, i32* %9
%63 = call i32 @munmap(i8* %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %60
store i32 3, i32* %3
br label %69
66: ; preds = %60
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
store i32 0, i32* %3
br label %69
69: ; preds = %66, %65, %44, %33
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [174 x i8] c"Summary: Dump bytes from /dev/mem to stdout\0AUsage: %s <start_addr> <num_bytes>\0Astart_addr: Start address to dump. Must be page aligned.\0Anum_bytes: Number of bytes to dump.\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([174 x i8], [174 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i8* @strtoul(i8* nocapture %11, i32* null, i32 0)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i8* @strtoul(i8* nocapture %14, i32* null, i32 0)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, 1
br i1 %20, label %43, label %21
21: ; preds = %9
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i8* @mmap(i32* null, i32 %17, i32 %22, i32 %23, i32 %19, i8* %12)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %43, label %27
27: ; preds = %21
%28 = icmp eq i32 %17, 0
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = and i64 %16, 4294967295
br label %34
31: ; preds = %34, %27
%32 = tail call i32 @munmap(i8* %24, i32 %17)
%33 = icmp eq i32 %32, -1
br i1 %33, label %43, label %41
34: ; preds = %29, %34
%35 = phi i64 [ 0, %29 ], [ %39, %34 ]
%36 = getelementptr i8, i8* %24, i64 %35
%37 = load i8, i8* %36
%38 = tail call i32 @putchar(i8 zeroext %37)
%39 = add nuw nsw i64 %35, 1
%40 = icmp eq i64 %39, %30
br i1 %40, label %31, label %34
41: ; preds = %31
%42 = tail call i32 @close(i32 %19)
br label %43
43: ; preds = %31, %21, %9, %41
%44 = phi i32 [ 0, %41 ], [ 1, %9 ], [ 2, %21 ], [ 3, %31 ]
ret i32 %44
}