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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ closedev(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int opendev(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, unsigned int, unsigned int);
int read(int, unsigned int *, int);
int strtoul(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ usage();
int md_main(int argc, char *argv[]) {
  int i, fd, count;
  unsigned val;
  loff_t addr;
  off_t ret;
  if (argc < 2 || argc > 3) {
    usage();
    return EINVAL;
  }
  if ((fd = opendev(O_RDONLY)) < 0) {
    return fd;
  }
  if (argc == 2) {
    count = 1;
  } else {
    count = atoi(argv[2]);
  }
  addr = strtoul(argv[1], NULL, 16) & 0xffffffff;
  lseek(fd, addr, SEEK_SET);
  for (i = 0; i < count; i++, addr += sizeof(val)) {
    if (read(fd, &val, sizeof(val)) != sizeof(val)) {
      perror("read");
      closedev(fd);
      return -1;
    }
    printf("%08llx : 0x%08x %12d\n", addr, val, val);
  }
  closedev(fd);
  return 0;
}


x86 assembly (clang, -O0):
<md_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11b3 <md_main+0x23>
cmpl   $0x3,-0x8(%rbp)
jle    11cb <md_main+0x3b>
mov    $0x0,%al
call   1060 <usage@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <md_main+0x143>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   10a0 <opendev@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11f0 <md_main+0x60>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d3 <md_main+0x143>
cmpl   $0x2,-0x8(%rbp)
jne    1206 <md_main+0x76>
movl   $0x1,-0x1c(%rbp)
jmp    1216 <md_main+0x86>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
and    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c4 <md_main+0x134>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     1296 <md_main+0x106>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   10b0 <closedev@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d3 <md_main+0x143>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xd5f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movslq -0x24(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x24(%rbp)
jmp    124d <md_main+0xbd>
mov    -0x18(%rbp),%edi
call   10b0 <closedev@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    -0x4(%rbx),%eax
cmp    $0xfffffffd,%eax
ja     11bb <md_main+0x2b>
xor    %eax,%eax
call   1060 <usage@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%r14d
jmp    126a <md_main+0xda>
mov    %rsi,%r14
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   10a0 <opendev@plt>
mov    %eax,%r15d
test   %eax,%eax
js     1267 <md_main+0xd7>
mov    $0x1,%ebp
cmp    $0x2,%ebx
je     11ec <md_main+0x5c>
mov    0x10(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%r14),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <strtoul@plt>
mov    %eax,%ebx
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %ebx,%esi
call   1040 <lseek@plt>
test   %ebp,%ebp
jle    125d <md_main+0xcd>
lea    0x4(%rsp),%r12
lea    0xde1(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    127c <md_main+0xec>
mov    0x4(%rsp),%ecx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %ecx,%edx
call   1030 <printf@plt>
add    $0x4,%ebx
add    $0xffffffff,%ebp
jne    1230 <md_main+0xa0>
mov    %r15d,%edi
call   10b0 <closedev@plt>
jmp    126a <md_main+0xda>
mov    %r15d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
call   1070 <perror@plt>
mov    %r15d,%edi
call   10b0 <closedev@plt>
mov    $0xffffffff,%r14d
jmp    126a <md_main+0xda>

LLVM IR (-O0):
@EINVAL = global i32 0
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [5 x i8] c"read\00"
@.str.1 = [22 x i8] c"%08llx : 0x%08x %12d\0A\00"
define i32 @md_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 i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %14, %2
%18 = call i32 (...) @usage()
%19 = load i32, i32* @EINVAL
store i32 %19, i32* %3
br label %73
20: ; preds = %14
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @opendev(i32 %21)
store i32 %22, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i32, i32* %7
store i32 %25, i32* %3
br label %73
26: ; preds = %20
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 1, i32* %8
br label %35
30: ; preds = %26
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %8
br label %35
35: ; preds = %30, %29
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @strtoul(i8* %38, i32* null, i32 16)
%40 = and i32 %39, -1
store i32 %40, i32* %10
%41 = load i32, i32* %7
%42 = load i32, i32* %10
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @lseek(i32 %41, i32 %42, i32 %43)
store i32 0, i32* %6
br label %45
45: ; preds = %63, %35
%46 = load i32, i32* %6
%47 = load i32, i32* %8
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %70
49: ; preds = %45
%50 = load i32, i32* %7
%51 = call i32 @read(i32 %50, i32* %9, i32 4)
%52 = sext i32 %51 to i64
%53 = icmp ne i64 %52, 4
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%56 = load i32, i32* %7
%57 = call i32 @closedev(i32 %56)
store i32 -1, i32* %3
br label %73
58: ; preds = %49
%59 = load i32, i32* %10
%60 = load i32, i32* %9
%61 = load i32, i32* %9
%62 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %59, i32 %60, i32 %61)
br label %63
63: ; preds = %58
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
store i32 %65, i32* %6
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = add i64 %67, 4
%69 = trunc i64 %68 to i32
store i32 %69, i32* %10
br label %45
70: ; preds = %45
%71 = load i32, i32* %7
%72 = call i32 @closedev(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %70, %54, %24, %17
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@EINVAL = global i32 0
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str = [5 x i8] c"read\00"
@.str.1 = [22 x i8] c"%08llx : 0x%08x %12d\0A\00"
define i32 @md_main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = add i32 %0, -4
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 (...) @usage()
%9 = load i32, i32* @EINVAL
br label %44
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @opendev(i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %44, label %14
14: ; preds = %10
%15 = icmp eq i32 %0, 2
br i1 %15, label %20, label %16
16: ; preds = %14
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
br label %20
20: ; preds = %14, %16
%21 = phi i32 [ %19, %16 ], [ 1, %14 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = tail call i32 @strtoul(i8* nocapture %23, i32* null, i32 16)
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @lseek(i32 %12, i32 %24, i32 %25)
%27 = icmp sgt i32 %21, 0
br i1 %27, label %28, label %42
28: ; preds = %20, %36
%29 = phi i32 [ %40, %36 ], [ %24, %20 ]
%30 = phi i32 [ %39, %36 ], [ 0, %20 ]
%31 = call i32 @read(i32 %12, i32* %3, i32 4)
%32 = icmp eq i32 %31, 4
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%35 = tail call i32 @closedev(i32 %12)
br label %44
36: ; preds = %28
%37 = load i32, i32* %3
%38 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %37, i32 %37)
%39 = add nuw nsw i32 %30, 1
%40 = add i32 %29, 4
%41 = icmp eq i32 %39, %21
br i1 %41, label %42, label %28
42: ; preds = %36, %20
%43 = tail call i32 @closedev(i32 %12)
br label %44
44: ; preds = %10, %42, %33, %7
%45 = phi i32 [ %9, %7 ], [ -1, %33 ], [ 0, %42 ], [ %12, %10 ]
ret i32 %45
}