#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 */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_byte(unsigned long, char *, unsigned long);
int /*<<< orphan*/ dump_word(unsigned long, char *, unsigned long);
int getopt(int, char **, char *);
char *mmap(int /*<<< orphan*/, unsigned long, int /*<<< orphan*/,
           int /*<<< orphan*/, int, unsigned long);
int /*<<< orphan*/ munmap(char *, unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ show_help();
scalar_t__ strtoll(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int fd, opt = -1, byte_mode = 0;
  unsigned long kaddr = 0;
  unsigned long size = 0;
  char *va = NULL;
  while ((opt = getopt(argc, argv, "b:s:Bh")) != -1) {
    switch (opt) {
    case 'h':
      show_help();
      return 0;
    case 'b':
      kaddr = (unsigned long)strtoll(optarg, NULL, 16);
      break;
    case 's':
      size = (unsigned long)strtoll(optarg, NULL, 16);
      break;
    case 'B':
      byte_mode = 1;
      break;
    default:
      show_help();
      return 0;
    }
  }
  fd = open("/dev/kmem", O_RDONLY);
  if (fd == -1) {
    perror("open");
    return -1;
  }
  printf("addr=%x, size=%x\n", kaddr, size);
  va = mmap(0, size, PROT_READ, MAP_SHARED, fd, kaddr);
  if (va == MAP_FAILED) {
    perror("mmap");
    return -1;
  }
  printf("va:%p\n", va);
  if (byte_mode)
    dump_byte(kaddr, va, size);
  else
    dump_word(kaddr, va, size);
  close(fd);
  munmap(va, size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe06(%rip),%rdx
call   10d0 <getopt@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     12c9 <main+0x119>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x42,%eax
je     12a5 <main+0xf5>
jmp    121f <main+0x6f>
mov    -0x3c(%rbp),%eax
sub    $0x62,%eax
je     1265 <main+0xb5>
jmp    1230 <main+0x80>
mov    -0x3c(%rbp),%eax
sub    $0x68,%eax
je     1252 <main+0xa2>
jmp    1241 <main+0x91>
mov    -0x3c(%rbp),%eax
sub    $0x73,%eax
je     1285 <main+0xd5>
jmp    12b1 <main+0x101>
mov    $0x0,%al
call   1080 <show_help@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cd <main+0x21d>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoll@plt>
mov    %rax,-0x28(%rbp)
jmp    12c4 <main+0x114>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtoll@plt>
mov    %rax,-0x30(%rbp)
jmp    12c4 <main+0x114>
movl   $0x1,-0x1c(%rbp)
jmp    12c4 <main+0x114>
mov    $0x0,%al
call   1080 <show_help@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cd <main+0x21d>
jmp    11ec <main+0x3c>
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
lea    0xd2e(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1303 <main+0x153>
lea    0xd1f(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <main+0x21d>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rsi
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
mov    0x2cb3(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x28(%rbp),%r9
xor    %edi,%edi
call   1040 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2c73(%rip),%rcx
cmp    (%rcx),%rax
jne    136e <main+0x1be>
lea    0xccb(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cd <main+0x21d>
mov    -0x38(%rbp),%rsi
lea    0xcb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     13a0 <main+0x1f0>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1090 <dump_byte@plt>
jmp    13b1 <main+0x201>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <dump_word@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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
mov    %edi,%ebp
mov    $0x1,%al
mov    %eax,0x4(%rsp)
xor    %r14d,%r14d
lea    0xe30(%rip),%r12
mov    0x2e21(%rip),%r13
xor    %r15d,%r15d
jmp    11f3 <main+0x43>
nopl   0x0(%rax)
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoll@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10d0 <getopt@plt>
cmp    $0x73,%eax
je     11e0 <main+0x30>
cmp    $0x61,%eax
jg     121e <main+0x6e>
cmp    $0xffffffff,%eax
je     1249 <main+0x99>
cmp    $0x42,%eax
jne    123b <main+0x8b>
movl   $0x0,0x4(%rsp)
jmp    11f3 <main+0x43>
cmp    $0x62,%eax
jne    1238 <main+0x88>
mov    0x0(%r13),%edi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtoll@plt>
mov    %rax,%r14
jmp    11f3 <main+0x43>
cmp    $0x68,%eax
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <show_help@plt>
jmp    12f8 <main+0x148>
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
lea    0xdae(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1309 <main+0x159>
mov    %eax,%ebx
lea    0xda6(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%r8d
mov    %r14,%r9
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d17(%rip),%rax
cmp    (%rax),%rbp
je     1312 <main+0x162>
lea    0xd78(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
testb  $0x1,0x4(%rsp)
je     12d6 <main+0x126>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1030 <dump_word@plt>
jmp    12e4 <main+0x134>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1090 <dump_byte@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <munmap@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd01(%rip),%rdi
jmp    1319 <main+0x169>
lea    0xd0f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12f8 <main+0x148>

LLVM IR (-O0):
@.str = [7 x i8] c"b:s:Bh\00"
@optarg = global i32 0
@.str.1 = [10 x i8] c"/dev/kmem\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [18 x i8] c"addr=%x, size=%x\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [7 x i8] c"va:%p\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 i64
%10 = alloca i64
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
store i32 0, i32* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i8* null, i8** %11
br label %12
12: ; preds = %30, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %7
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %31
17: ; preds = %12
%18 = load i32, i32* %7
switch i32 %18, label %28 [
i32 104, label %19
i32 98, label %21
i32 115, label %24
i32 66, label %27
]
19: ; preds = %17
%20 = call i32 (...) @show_help()
store i32 0, i32* %3
br label %74
21: ; preds = %17
%22 = load i32, i32* @optarg
%23 = call i64 @strtoll(i32 %22, i32* null, i32 16)
store i64 %23, i64* %9
br label %30
24: ; preds = %17
%25 = load i32, i32* @optarg
%26 = call i64 @strtoll(i32 %25, i32* null, i32 16)
store i64 %26, i64* %10
br label %30
27: ; preds = %17
store i32 1, i32* %8
br label %30
28: ; preds = %17
%29 = call i32 (...) @show_help()
store i32 0, i32* %3
br label %74
30: ; preds = %27, %24, %21
br label %12
31: ; preds = %12
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
38: ; preds = %31
%39 = load i64, i64* %9
%40 = load i64, i64* %10
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %39, i64 %40)
%42 = load i64, i64* %10
%43 = load i32, i32* @PROT_READ
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %6
%46 = load i64, i64* %9
%47 = call i8* @mmap(i32 0, i64 %42, i32 %43, i32 %44, i32 %45, i64 %46)
store i8* %47, i8** %11
%48 = load i8*, i8** %11
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %53
51: ; preds = %38
%52 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %74
53: ; preds = %38
%54 = load i8*, i8** %11
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %8
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = load i64, i64* %9
%60 = load i8*, i8** %11
%61 = load i64, i64* %10
%62 = call i32 @dump_byte(i64 %59, i8* %60, i64 %61)
br label %68
63: ; preds = %53
%64 = load i64, i64* %9
%65 = load i8*, i8** %11
%66 = load i64, i64* %10
%67 = call i32 @dump_word(i64 %64, i8* %65, i64 %66)
br label %68
68: ; preds = %63, %58
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load i8*, i8** %11
%72 = load i64, i64* %10
%73 = call i32 @munmap(i8* %71, i64 %72)
store i32 0, i32* %3
br label %74
74: ; preds = %68, %51, %36, %28, %19
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [7 x i8] c"b:s:Bh\00"
@optarg = global i32 0
@.str.1 = [10 x i8] c"/dev/kmem\00"
@O_RDONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [18 x i8] c"addr=%x, size=%x\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [7 x i8] c"va:%p\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %10, %2
%4 = phi i1 [ true, %2 ], [ false, %10 ]
%5 = phi i64 [ 0, %2 ], [ %8, %10 ]
%6 = phi i64 [ 0, %2 ], [ %11, %10 ]
br label %7
7: ; preds = %3, %15
%8 = phi i64 [ %5, %3 ], [ %17, %15 ]
%9 = phi i64 [ %6, %3 ], [ %11, %15 ]
br label %10
10: ; preds = %7, %18
%11 = phi i64 [ %20, %18 ], [ %9, %7 ]
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
switch i32 %12, label %21 [
i32 -1, label %23
i32 104, label %13
i32 98, label %15
i32 115, label %18
i32 66, label %3
]
13: ; preds = %10
%14 = tail call i32 (...) @show_help()
br label %47
15: ; preds = %10
%16 = load i32, i32* @optarg
%17 = tail call i64 @strtoll(i32 %16, i32* null, i32 16)
br label %7
18: ; preds = %10
%19 = load i32, i32* @optarg
%20 = tail call i64 @strtoll(i32 %19, i32* null, i32 16)
br label %10
21: ; preds = %10
%22 = tail call i32 (...) @show_help()
br label %47
23: ; preds = %10
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %47
29: ; preds = %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %8, i64 %11)
%31 = load i32, i32* @PROT_READ
%32 = load i32, i32* @MAP_SHARED
%33 = tail call i8* @mmap(i32 0, i64 %11, i32 %31, i32 %32, i32 %25, i64 %8)
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %47
38: ; preds = %29
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br i1 %4, label %42, label %40
40: ; preds = %38
%41 = tail call i32 @dump_byte(i64 %8, i8* %33, i64 %11)
br label %44
42: ; preds = %38
%43 = tail call i32 @dump_word(i64 %8, i8* %33, i64 %11)
br label %44
44: ; preds = %42, %40
%45 = tail call i32 @close(i32 %25)
%46 = tail call i32 @munmap(i8* %33, i64 %11)
br label %47
47: ; preds = %44, %36, %27, %21, %13
%48 = phi i32 [ 0, %21 ], [ 0, %13 ], [ -1, %27 ], [ -1, %36 ], [ 0, %44 ]
ret i32 %48
}