#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 ssize_t;
typedef unsigned long off_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(unsigned long);
int /*<<< orphan*/ memcpy(char *, char *, unsigned long);
void *mmap(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/,
           int /*<<< orphan*/, int, unsigned long);
int /*<<< orphan*/ munmap(void *, unsigned long);
int open(char *, int /*<<< orphan*/);
unsigned long parse_int(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
long sysconf(int /*<<< orphan*/);
int write(int /*<<< orphan*/, char *, unsigned long);
int main(int argc, char *argv[]) {
  unsigned long addr, length;
  int devmem;
  void *mapping;
  long page_size;
  off_t map_base, extra_bytes;
  char *buf;
  ssize_t ret;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s ADDR LENGTH\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  addr = parse_int(argv[1]);
  length = parse_int(argv[2]);
  devmem = open("/dev/mem", O_RDONLY);
  if (devmem == -1) {
    perror("Could not open /dev/mem");
    goto open_fail;
  }
  page_size = sysconf(_SC_PAGE_SIZE);
  map_base = addr & ~(page_size - 1);
  extra_bytes = addr - map_base;
  mapping =
      mmap(NULL, length + extra_bytes, PROT_READ, MAP_SHARED, devmem, map_base);
  if (mapping == MAP_FAILED) {
    perror("Could not map memory");
    goto map_fail;
  }
  buf = (char *)malloc(length);
  if (buf == NULL) {
    fprintf(stderr, "Failed to allocate memory\n");
    goto alloc_fail;
  }
  memcpy(buf, (char *)mapping + extra_bytes, length);
  ret = write(STDOUT_FILENO, buf, length);
  if (ret == -1) {
    perror("Could not write data");
  } else if (ret != (ssize_t)length) {
    fprintf(stderr, "Only wrote %d bytes\n", ret);
  }
  free(buf);
alloc_fail:
  munmap(mapping, length + extra_bytes);
map_fail:
  close(devmem);
open_fail:
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     121c <main+0x4c>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <parse_int@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <parse_int@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1271 <main+0xa1>
lea    0xdb0(%rip),%rdi
call   10c0 <perror@plt>
jmp    13c7 <main+0x1f7>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
mov    0x2d22(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x40(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%rax
jne    12fa <main+0x12a>
lea    0xd3f(%rip),%rdi
call   10c0 <perror@plt>
jmp    13bf <main+0x1ef>
mov    -0x20(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    132e <main+0x15e>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13ae <main+0x1de>
mov    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <memcpy@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1377 <main+0x1a7>
lea    0xcf2(%rip),%rdi
call   10c0 <perror@plt>
jmp    13a5 <main+0x1d5>
mov    -0x54(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
je     13a0 <main+0x1d0>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
mov    -0x54(%rbp),%edx
lea    0xcdb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13a5 <main+0x1d5>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    -0x48(%rbp),%rsi
call   10a0 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2bda(%rip),%rax
mov    (%rax),%eax
add    $0x60,%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    $0x3,%edi
jne    1344 <main+0x174>
mov    0x8(%rbx),%rdi
call   10f0 <parse_int@plt>
mov    %rax,%rbp
mov    0x10(%rbx),%rdi
call   10f0 <parse_int@plt>
mov    %rax,%r13
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xe17(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x14a>
mov    %eax,%r15d
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   10d0 <sysconf@plt>
neg    %rax
and    %rbp,%rax
sub    %rax,%rbp
mov    %rbp,%r14
add    %r13,%r14
mov    0x2db4(%rip),%rcx
mov    (%rcx),%edx
mov    0x2d93(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15d,%r8d
mov    %rax,%r9
call   1050 <mmap@plt>
mov    %rax,%r12
mov    0x2d37(%rip),%rax
cmp    (%rax),%r12
je     1328 <main+0x158>
mov    %r13,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12d8 <main+0x108>
mov    %rax,%rbx
mov    %r12,%rsi
add    %rbp,%rsi
mov    %rax,%rdi
mov    %r13,%rdx
call   1080 <memcpy@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1336 <main+0x166>
cmp    %r13d,%eax
je     12ce <main+0xfe>
mov    0x2d04(%rip),%rcx
mov    (%rcx),%edi
lea    0xdaf(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    12ef <main+0x11f>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <munmap@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf6(%rip),%rdi
call   10c0 <perror@plt>
jmp    1302 <main+0x132>
lea    0xd00(%rip),%rdi
call   10c0 <perror@plt>
jmp    12fa <main+0x12a>
lea    0xd22(%rip),%rdi
call   10c0 <perror@plt>
jmp    12ce <main+0xfe>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xca9(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2c53(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s ADDR LENGTH\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.2 = [24 x i8] c"Could not open /dev/mem\00"
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [21 x i8] c"Could not map memory\00"
@.str.4 = [27 x i8] c"Failed to allocate memory\0A\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [21 x i8] c"Could not write data\00"
@.str.6 = [21 x i8] c"Only wrote %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %25
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i64 @parse_int(i8* %28)
store i64 %29, i64* %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
%33 = call i64 @parse_int(i8* %32)
store i64 %33, i64* %7
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %110
40: ; preds = %25
%41 = load i32, i32* @_SC_PAGE_SIZE
%42 = call i64 @sysconf(i32 %41)
store i64 %42, i64* %10
%43 = load i64, i64* %6
%44 = load i64, i64* %10
%45 = sub nsw i64 %44, 1
%46 = xor i64 %45, -1
%47 = and i64 %43, %46
store i64 %47, i64* %11
%48 = load i64, i64* %6
%49 = load i64, i64* %11
%50 = sub i64 %48, %49
store i64 %50, i64* %12
%51 = load i64, i64* %7
%52 = load i64, i64* %12
%53 = add i64 %51, %52
%54 = load i32, i32* @PROT_READ
%55 = load i32, i32* @MAP_SHARED
%56 = load i32, i32* %8
%57 = load i64, i64* %11
%58 = call i8* @mmap(i32* null, i64 %53, i32 %54, i32 %55, i32 %56, i64 %57)
store i8* %58, i8** %9
%59 = load i8*, i8** %9
%60 = load i8*, i8** @MAP_FAILED
%61 = icmp eq i8* %59, %60
br i1 %61, label %62, label %64
62: ; preds = %40
%63 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %107
64: ; preds = %40
%65 = load i64, i64* %7
%66 = call i64 @malloc(i64 %65)
%67 = inttoptr i64 %66 to i8*
store i8* %67, i8** %13
%68 = load i8*, i8** %13
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %101
73: ; preds = %64
%74 = load i8*, i8** %13
%75 = load i8*, i8** %9
%76 = load i64, i64* %12
%77 = getelementptr inbounds i8, i8* %75, i64 %76
%78 = load i64, i64* %7
%79 = call i32 @memcpy(i8* %74, i8* %77, i64 %78)
%80 = load i32, i32* @STDOUT_FILENO
%81 = load i8*, i8** %13
%82 = load i64, i64* %7
%83 = call i32 @write(i32 %80, i8* %81, i64 %82)
store i32 %83, i32* %14
%84 = load i32, i32* %14
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %88
86: ; preds = %73
%87 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %98
88: ; preds = %73
%89 = load i32, i32* %14
%90 = load i64, i64* %7
%91 = trunc i64 %90 to i32
%92 = icmp ne i32 %89, %91
br i1 %92, label %93, label %97
93: ; preds = %88
%94 = load i32, i32* @stderr
%95 = load i32, i32* %14
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %95)
br label %97
97: ; preds = %93, %88
br label %98
98: ; preds = %97, %86
%99 = load i8*, i8** %13
%100 = call i32 @free(i8* %99)
br label %101
101: ; preds = %98, %70
%102 = load i8*, i8** %9
%103 = load i64, i64* %7
%104 = load i64, i64* %12
%105 = add i64 %103, %104
%106 = call i32 @munmap(i8* %102, i64 %105)
br label %107
107: ; preds = %101, %62
%108 = load i32, i32* %8
%109 = call i32 @close(i32 %108)
br label %110
110: ; preds = %107, %38
%111 = load i32, i32* @EXIT_SUCCESS
ret i32 %111
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"Usage: %s ADDR LENGTH\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.2 = [24 x i8] c"Could not open /dev/mem\00"
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [21 x i8] c"Could not map memory\00"
@.str.4 = [27 x i8] c"Failed to allocate memory\0A\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [21 x i8] c"Could not write data\00"
@.str.6 = [21 x i8] c"Only wrote %d bytes\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %10, label %4
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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @parse_int(i8* %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @parse_int(i8* %15)
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %63
22: ; preds = %10
%23 = load i32, i32* @_SC_PAGE_SIZE
%24 = tail call i64 @sysconf(i32 %23)
%25 = sub i64 0, %24
%26 = and i64 %13, %25
%27 = sub i64 %13, %26
%28 = add i64 %27, %16
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i8* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %18, i64 %26)
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %61
36: ; preds = %22
%37 = tail call i64 @malloc(i64 %16)
%38 = inttoptr i64 %37 to i8*
%39 = icmp eq i64 %37, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %59
43: ; preds = %36
%44 = getelementptr inbounds i8, i8* %31, i64 %27
%45 = tail call i32 @memcpy(i8* %38, i8* %44, i64 %16)
%46 = load i32, i32* @STDOUT_FILENO
%47 = tail call i32 @write(i32 %46, i8* %38, i64 %16)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %57
51: ; preds = %43
%52 = trunc i64 %16 to i32
%53 = icmp eq i32 %47, %52
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %47)
br label %57
57: ; preds = %51, %54, %49
%58 = tail call i32 @free(i8* %38)
br label %59
59: ; preds = %57, %40
%60 = tail call i32 @munmap(i8* %31, i64 %28)
br label %61
61: ; preds = %59, %34
%62 = tail call i32 @close(i32 %18)
br label %63
63: ; preds = %61, %20
%64 = load i32, i32* @EXIT_SUCCESS
ret i32 %64
}