#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_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, void volatile *);
int /*<<< orphan*/ help();
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, void *, size_t);
int main(int argc, char *argv[]) {
  int i;
  volatile unsigned char *cp;
  int fd;
  volatile void *v;
  off_t nvram = 0xfff00000;
  size_t length = 0x100000;
  help();
  if (argc > 1)
    nvram = (strtol(argv[1], 0, 0)) << 16;
  if (argc > 2)
    length = (strtol(argv[2], 0, 0));
  if ((fd = open("/dev/mem", O_RDWR)) != -1) {
    v = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, nvram);
    fprintf(stderr, "mmap returns %p\n", v);
    if ((int)v == -1) {
      perror("mmap");
      _exit(1);
    }
  } else {
    perror("open /dev/mem");
    _exit(1);
  }
  write(1, (void *)v, length);
  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   $0xfff00000,-0x34(%rbp)
movq   $0x100000,-0x40(%rbp)
mov    $0x0,%al
call   10a0 <help@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11cd <main+0x4d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <strtol@plt>
shl    $0x10,%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11ee <main+0x6e>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <strtol@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1282 <main+0x102>
mov    -0x40(%rbp),%rsi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    0x2d9d(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x34(%rbp),%r9d
xor    %edi,%edi
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rax
cmp    $0xffffffff,%eax
jne    127d <main+0xfd>
lea    0xda3(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    1298 <main+0x118>
lea    0xd8d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x30(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    $0x1,%edi
call   1040 <write@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
xor    %eax,%eax
call   10a0 <help@plt>
mov    $0x100000,%r14d
mov    $0xfff00000,%ebp
cmp    $0x2,%ebx
jl     11ca <main+0x4a>
mov    0x8(%r15),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
mov    %eax,%ebp
shl    $0x10,%ebp
cmp    $0x2,%ebx
je     11ca <main+0x4a>
mov    0x10(%r15),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <strtol@plt>
movslq %eax,%r14
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xcc>
mov    0x2e0d(%rip),%rcx
mov    0x2dce(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2deb(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %eax,%r8d
mov    %ebp,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
cmp    $0xffffffff,%ebx
je     1255 <main+0xd5>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
jmp    125c <main+0xdc>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"mmap returns %p\0A\00"
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [14 x i8] c"open /dev/mem\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1048576, i32* %10
store i64 1048576, i64* %11
%12 = call i32 (...) @help()
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strtol(i8* %18, i32 0, i32 0)
%20 = shl i32 %19, 16
store i32 %20, i32* %10
br label %21
21: ; preds = %15, %2
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 2
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i32 0, i32 0)
%29 = sext i32 %28 to i64
store i64 %29, i64* %11
br label %30
30: ; preds = %24, %21
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %31)
store i32 %32, i32* %8
%33 = icmp ne i32 %32, -1
br i1 %33, label %34, label %53
34: ; preds = %30
%35 = load i64, i64* %11
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %8
%41 = load i32, i32* %10
%42 = call i8* @mmap(i32 0, i64 %35, i32 %38, i32 %39, i32 %40, i32 %41)
store i8* %42, i8** %9
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %9
%45 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %9
%47 = ptrtoint i8* %46 to i32
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %34
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @_exit(i32 1)
unreachable
52: ; preds = %34
br label %56
53: ; preds = %30
%54 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @_exit(i32 1)
unreachable
56: ; preds = %52
%57 = load i8*, i8** %9
%58 = load i64, i64* %11
%59 = call i32 @write(i32 1, i8* %57, i64 %58)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"mmap returns %p\0A\00"
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [14 x i8] c"open /dev/mem\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @help()
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %16
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strtol(i8* %7, i32 0, i32 0)
%9 = shl i32 %8, 16
%10 = icmp eq i32 %0, 2
br i1 %10, label %16, label %11
11: ; preds = %5
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @strtol(i8* %13, i32 0, i32 0)
%15 = sext i32 %14 to i64
br label %16
16: ; preds = %2, %11, %5
%17 = phi i32 [ %9, %11 ], [ %9, %5 ], [ -1048576, %2 ]
%18 = phi i64 [ %15, %11 ], [ 1048576, %5 ], [ 1048576, %2 ]
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %36, label %22
22: ; preds = %16
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %24, %23
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32 0, i64 %18, i32 %25, i32 %26, i32 %20, i32 %17)
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%30 = ptrtoint i8* %27 to i64
%31 = trunc i64 %30 to i32
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %22
%34 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @_exit(i32 1)
unreachable
36: ; preds = %16
%37 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @_exit(i32 1)
unreachable
39: ; preds = %22
%40 = tail call i32 @write(i32 1, i8* %27, i64 %18)
ret i32 0
}