#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 /*<<< orphan*/ uint64_t;
typedef int off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDONLY;
int O_SYNC;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
unsigned int getpagesize();
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int);
int munmap(void *, unsigned int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ writeval;
int dumpmem(char *name, off_t start, off_t end) {
  void *map_base, *virt_addr;
  uint64_t read_result;
  uint64_t writeval = writeval;
  off_t target;
  unsigned page_size, mapped_size, offset_in_page;
  int fd;
  unsigned width = 8 * sizeof(int);
  FILE *p = NULL;
  printf("start dump mem\n");
  fd = open("/dev/mem", (O_RDONLY | O_SYNC));
  p = fopen(name, "wb");
  for (target = start; target <= end; target += 4) {
    mapped_size = page_size = getpagesize();
    offset_in_page = (unsigned)target & (page_size - 1);
    if (offset_in_page + width > page_size) {
      mapped_size *= 2;
    }
    map_base = mmap(NULL, mapped_size, PROT_READ, MAP_SHARED, fd,
                    target & ~(off_t)(page_size - 1));
    if (map_base == MAP_FAILED) {
      printf("memmap failed\n");
      return 0;
    }
    if (p) {
      printf("offset_in_page %x \n", offset_in_page);
      fwrite((char *)map_base + offset_in_page, 4, 1, p);
    } else {
      printf("open file failed\n");
    }
    if (munmap(map_base, mapped_size) == -1) {
      printf("munmap failed\n");
      return 0;
    }
  }
  close(fd);
  fclose(p);
}


x86 assembly (clang, -O0):
<dumpmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x20,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
lea    0xe32(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1331 <dumpmem+0x1a1>
mov    $0x0,%al
call   10a0 <getpagesize@plt>
mov    %eax,-0x38(%rbp)
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
add    -0x48(%rbp),%eax
cmp    -0x38(%rbp),%eax
jbe    123f <dumpmem+0xaf>
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
mov    0x2d96(%rip),%rax
mov    (%rax),%ecx
mov    -0x44(%rbp),%r8d
mov    -0x34(%rbp),%r9d
mov    -0x38(%rbp),%eax
sub    $0x1,%eax
xor    $0xffffffff,%eax
and    %eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
jne    12a3 <dumpmem+0x113>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1342 <dumpmem+0x1b2>
cmpq   $0x0,-0x50(%rbp)
je     12e1 <dumpmem+0x151>
mov    -0x40(%rbp),%esi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%eax
add    %rax,%rdi
mov    -0x50(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
jmp    12ef <dumpmem+0x15f>
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    131e <dumpmem+0x18e>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1342 <dumpmem+0x1b2>
jmp    1323 <dumpmem+0x193>
mov    -0x34(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x34(%rbp)
jmp    1200 <dumpmem+0x70>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dumpmem>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0a(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe28(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%r15d
lea    0xe22(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %r14d,0x4(%rsp)
cmp    %r14d,%ebp
jg     12b9 <dumpmem+0x119>
xor    %eax,%eax
call   10b0 <getpagesize@plt>
lea    -0x1(%rax),%r13d
and    %ebp,%r13d
lea    0x20(%r13),%ecx
cmp    %eax,%ecx
seta   %cl
mov    %eax,%r12d
shl    %cl,%r12d
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%edx
mov    0x2dbb(%rip),%rcx
mov    (%rcx),%ecx
neg    %eax
and    %ebp,%eax
xor    %edi,%edi
mov    %r12d,%esi
mov    %r15d,%r8d
mov    %eax,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    0x2d73(%rip),%rax
cmp    (%rax),%r14
je     12cb <dumpmem+0x12b>
test   %rbx,%rbx
je     1290 <dumpmem+0xf0>
lea    0xdae(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
add    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
jmp    129c <dumpmem+0xfc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
mov    %r12d,%esi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
je     12d4 <dumpmem+0x134>
add    $0x4,%ebp
cmp    0x4(%rsp),%ebp
jle    1200 <dumpmem+0x60>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    12e0 <dumpmem+0x140>
lea    0xd7c(%rip),%rdi
jmp    12db <dumpmem+0x13b>
lea    0xd65(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"start dump mem\0A\00"
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [3 x i8] c"wb\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [15 x i8] c"memmap failed\0A\00"
@.str.4 = [20 x i8] c"offset_in_page %x \0A\00"
@.str.5 = [18 x i8] c"open file failed\0A\00"
@.str.6 = [15 x i8] c"munmap failed\0A\00"
@writeval = global i32 0
define i32 @dumpmem(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%19 = load i32, i32* %11
store i32 %19, i32* %11
store i32 32, i32* %17
store i32* null, i32** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_SYNC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %16
%25 = load i8*, i8** %5
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %18
%27 = load i32, i32* %6
store i32 %27, i32* %12
br label %28
28: ; preds = %84, %3
%29 = load i32, i32* %12
%30 = load i32, i32* %7
%31 = icmp sle i32 %29, %30
br i1 %31, label %32, label %87
32: ; preds = %28
%33 = call i32 (...) @getpagesize()
store i32 %33, i32* %13
store i32 %33, i32* %14
%34 = load i32, i32* %12
%35 = load i32, i32* %13
%36 = sub i32 %35, 1
%37 = and i32 %34, %36
store i32 %37, i32* %15
%38 = load i32, i32* %15
%39 = load i32, i32* %17
%40 = add i32 %38, %39
%41 = load i32, i32* %13
%42 = icmp ugt i32 %40, %41
br i1 %42, label %43, label %46
43: ; preds = %32
%44 = load i32, i32* %14
%45 = mul i32 %44, 2
store i32 %45, i32* %14
br label %46
46: ; preds = %43, %32
%47 = load i32, i32* %14
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %16
%51 = load i32, i32* %12
%52 = load i32, i32* %13
%53 = sub i32 %52, 1
%54 = xor i32 %53, -1
%55 = and i32 %51, %54
%56 = call i8* @mmap(i32* null, i32 %47, i32 %48, i32 %49, i32 %50, i32 %55)
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = load i8*, i8** @MAP_FAILED
%59 = icmp eq i8* %57, %58
br i1 %59, label %60, label %62
60: ; preds = %46
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %92
62: ; preds = %46
%63 = load i32*, i32** %18
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %74
65: ; preds = %62
%66 = load i32, i32* %15
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %66)
%68 = load i8*, i8** %8
%69 = load i32, i32* %15
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32*, i32** %18
%73 = call i32 @fwrite(i8* %71, i32 4, i32 1, i32* %72)
br label %76
74: ; preds = %62
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %65
%77 = load i8*, i8** %8
%78 = load i32, i32* %14
%79 = call i32 @munmap(i8* %77, i32 %78)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %4
br label %92
83: ; preds = %76
br label %84
84: ; preds = %83
%85 = load i32, i32* %12
%86 = add nsw i32 %85, 4
store i32 %86, i32* %12
br label %28
87: ; preds = %28
%88 = load i32, i32* %16
%89 = call i32 @close(i32 %88)
%90 = load i32*, i32** %18
%91 = call i32 @fclose(i32* %90)
br label %92
92: ; preds = %87, %81, %60
%93 = load i32, i32* %4
ret i32 %93
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
@.str.2 = [3 x i8] c"wb\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [20 x i8] c"offset_in_page %x \0A\00"
@writeval = global i32 0
@str = [15 x i8] c"start dump mem\00"
@str.7 = [17 x i8] c"open file failed\00"
@str.8 = [14 x i8] c"munmap failed\00"
@str.9 = [14 x i8] c"memmap failed\00"
define i32 @dumpmem(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_SYNC
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp sgt i32 %1, %2
br i1 %10, label %47, label %11
11: ; preds = %3
%12 = icmp eq i32* %9, null
br label %15
13: ; preds = %41
%14 = icmp sgt i32 %44, %2
br i1 %14, label %47, label %15
15: ; preds = %11, %13
%16 = phi i32 [ %1, %11 ], [ %44, %13 ]
%17 = tail call i32 (...) @getpagesize()
%18 = add i32 %17, -1
%19 = and i32 %18, %16
%20 = add i32 %19, 32
%21 = icmp ugt i32 %20, %17
%22 = zext i1 %21 to i32
%23 = shl i32 %17, %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_SHARED
%26 = sub i32 0, %17
%27 = and i32 %16, %26
%28 = tail call i8* @mmap(i32* null, i32 %23, i32 %24, i32 %25, i32 %8, i32 %27)
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %50
33: ; preds = %15
br i1 %12, label %39, label %34
34: ; preds = %33
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%36 = zext i32 %19 to i64
%37 = getelementptr inbounds i8, i8* %28, i64 %36
%38 = tail call i32 @fwrite(i8* %37, i32 4, i32 1, i32* %9)
br label %41
39: ; preds = %33
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = tail call i32 @munmap(i8* %28, i32 %23)
%43 = icmp eq i32 %42, -1
%44 = add nsw i32 %16, 4
br i1 %43, label %45, label %13
45: ; preds = %41
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
br label %50
47: ; preds = %13, %3
%48 = tail call i32 @close(i32 %8)
%49 = tail call i32 @fclose(i32* %9)
br label %50
50: ; preds = %47, %45, %31
ret i32 0
}