#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*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned long, unsigned long);
unsigned long strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  FILE *fd_mem, *fd_out;
  unsigned long i, addr, len;
  int int_no;
  char outname[80];
  unsigned char ch;
  if (argc < 3) {
    printf("Usage: %s address length (in hex)\n", argv[0]);
    return EXIT_FAILURE;
  }
  addr = strtol(argv[1], NULL, 16);
  len = strtol(argv[2], NULL, 16);
  if (!(fd_mem = fopen("/dev/mem", "rb"))) {
    perror("Can't open file - /dev/mem");
    return EXIT_FAILURE;
  }
  sprintf(outname, "mem%08X_%08X.dump", addr, len);
  if (!(fd_out = fopen(outname, "wb"))) {
    perror("Can't open output file");
    fclose(fd_mem);
    return EXIT_FAILURE;
  }
  fseek(fd_mem, addr, SEEK_SET);
  for (i = 0; i < len; i++) {
    fread(&ch, 1, 1, fd_mem);
    fwrite(&ch, 1, 1, fd_out);
  }
  fclose(fd_out);
  fclose(fd_mem);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <main+0x1a4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %rax,-0x38(%rbp)
lea    0xe26(%rip),%rdi
lea    0xe0a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1249 <main+0xb9>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <main+0x1a4>
lea    -0x90(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xde2(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12ab <main+0x11b>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1334 <main+0x1a4>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    1316 <main+0x186>
mov    -0x18(%rbp),%rcx
lea    -0x91(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rcx
lea    -0x91(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   10b0 <fwrite@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12c9 <main+0x139>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11be <main+0x2e>
mov    (%rbx),%rsi
lea    0xe53(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e1f(%rip),%rax
jmp    129f <main+0x10f>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %rax,%rbx
lea    0xe4d(%rip),%rdi
lea    0xe31(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12ad <main+0x11d>
mov    %rax,%r14
lea    0xe37(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
call   10b0 <sprintf@plt>
lea    0xe2f(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12c2 <main+0x132>
mov    %rax,%r12
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <fseek@plt>
test   %rbx,%rbx
je     1288 <main+0xf8>
lea    0xf(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
add    $0xffffffffffffffff,%rbx
jne    1260 <main+0xd0>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d18(%rip),%rax
jmp    129f <main+0x10f>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2cfb(%rip),%rax
jmp    129f <main+0x10f>

LLVM IR (-O0):
@.str = [35 x i8] c"Usage: %s address length (in hex)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/mem\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [27 x i8] c"Can't open file - /dev/mem\00"
@.str.4 = [18 x i8] c"mem%08X_%08X.dump\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [23 x i8] c"Can't open output file\00"
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca [80 x i8]
%13 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %3
br label %72
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i64 @strtol(i8* %25, i32* null, i32 16)
store i64 %26, i64* %9
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call i64 @strtol(i8* %29, i32* null, i32 16)
store i64 %30, i64* %10
%31 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %6
%32 = icmp ne i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %72
36: ; preds = %22
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%38 = load i64, i64* %9
%39 = load i64, i64* %10
%40 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i64 %38, i64 %39)
%41 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %42, i32** %7
%43 = icmp ne i32* %42, null
br i1 %43, label %49, label %44
44: ; preds = %36
%45 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32*, i32** %6
%47 = call i32 @fclose(i32* %46)
%48 = load i32, i32* @EXIT_FAILURE
store i32 %48, i32* %3
br label %72
49: ; preds = %36
%50 = load i32*, i32** %6
%51 = load i64, i64* %9
%52 = load i32, i32* @SEEK_SET
%53 = call i32 @fseek(i32* %50, i64 %51, i32 %52)
store i64 0, i64* %8
br label %54
54: ; preds = %63, %49
%55 = load i64, i64* %8
%56 = load i64, i64* %10
%57 = icmp ult i64 %55, %56
br i1 %57, label %58, label %66
58: ; preds = %54
%59 = load i32*, i32** %6
%60 = call i32 @fread(i8* %13, i32 1, i32 1, i32* %59)
%61 = load i32*, i32** %7
%62 = call i32 @fwrite(i8* %13, i32 1, i32 1, i32* %61)
br label %63
63: ; preds = %58
%64 = load i64, i64* %8
%65 = add i64 %64, 1
store i64 %65, i64* %8
br label %54
66: ; preds = %54
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
%69 = load i32*, i32** %6
%70 = call i32 @fclose(i32* %69)
%71 = load i32, i32* @EXIT_SUCCESS
store i32 %71, i32* %3
br label %72
72: ; preds = %66, %44, %33, %16
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [35 x i8] c"Usage: %s address length (in hex)\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/dev/mem\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [27 x i8] c"Can't open file - /dev/mem\00"
@.str.4 = [18 x i8] c"mem%08X_%08X.dump\00"
@.str.5 = [3 x i8] c"wb\00"
@.str.6 = [23 x i8] c"Can't open output file\00"
@SEEK_SET = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca i8
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %43
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @strtol(i8* nocapture %12, i32* null, i32 16)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i64 @strtol(i8* nocapture %15, i32* null, i32 16)
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %43
21: ; preds = %10
%22 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i64 %13, i64 %16)
%23 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%27 = tail call i32 @fclose(i32* %17)
br label %43
28: ; preds = %21
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @fseek(i32* %17, i64 %13, i32 %29)
%31 = icmp eq i64 %16, 0
br i1 %31, label %40, label %32
32: ; preds = %28, %32
%33 = phi i64 [ %38, %32 ], [ 0, %28 ]
%34 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %17)
%35 = load i8, i8* %4
%36 = sext i8 %35 to i32
%37 = tail call i32 @fputc(i32 %36, i32* %23)
%38 = add nuw i64 %33, 1
%39 = icmp eq i64 %38, %16
br i1 %39, label %40, label %32
40: ; preds = %32, %28
%41 = tail call i32 @fclose(i32* %23)
%42 = tail call i32 @fclose(i32* %17)
br label %43
43: ; preds = %40, %25, %19, %7
%44 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_SUCCESS, %40 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_FAILURE, %19 ]
%45 = load i32, i32* %44
ret i32 %45
}