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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int main(int argc, char *argv[]) {
  int fd, bytes_read, i, start_addr = 0;
  unsigned char line[16];
  char *filename = "/etc/passwd";
  if (argc > 1)
    filename = argv[1];
  if (argc > 2)
    start_addr = atoi(argv[2]);
  printf("\n **** DISPLAYING (IN HEX) FILE: %s at offset = %d (=0x%x) ****\n\n",
         filename, start_addr, start_addr);
  if ((fd = open(filename, O_RDONLY)) < 0) {
    perror(filename);
    exit(errno);
  };
  if (-1 == lseek(fd, start_addr, SEEK_SET)) {
    perror(filename);
    exit(errno);
  };
  printf("starting at 0x%x:\n", start_addr);
  do {
    if ((bytes_read = read(fd, line, 16)) < 0) {
      perror(filename);
      exit(errno);
    };
    for (i = 0; i < bytes_read; i++)
      printf("%2.2x ", line[i]);
    printf("\n");
  } while (bytes_read > 0);
  close(fd);
  exit(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   $0x0,-0x20(%rbp)
lea    0xe5c(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11be <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11d8 <main+0x58>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1225 <main+0xa5>
mov    -0x38(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    125f <main+0xdf>
mov    -0x38(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12a4 <main+0x124>
mov    -0x38(%rbp),%rdi
call   1080 <perror@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12dc <main+0x15c>
movslq -0x1c(%rbp),%rax
movzbl -0x30(%rbp,%rax,1),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12ab <main+0x12b>
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jg     1270 <main+0xf0>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %ebx,%ebx
cmp    $0x2,%edi
jl     11b8 <main+0x28>
mov    0x8(%rsi),%rbp
je     11bf <main+0x2f>
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
jmp    11bf <main+0x2f>
lea    0xe41(%rip),%rbp
lea    0xe46(%rip),%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1204 <main+0x74>
mov    %eax,%r12d
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %ebx,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    121a <main+0x8a>
mov    %rbp,%rdi
call   1090 <perror@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %rbp,0x8(%rsp)
lea    0xe28(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%r15
lea    0xe26(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
test   %eax,%eax
js     128a <main+0xfa>
je     1294 <main+0x104>
mov    %eax,%r13d
mov    %eax,%ebp
xor    %r14d,%r14d
xchg   %ax,%ax
movzbl 0x10(%rsp,%r14,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    1260 <main+0xd0>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %r13d,%r13d
jg     1240 <main+0xb0>
jmp    129e <main+0x10e>
mov    0x8(%rsp),%rdi
jmp    1207 <main+0x77>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [66 x i8] c"\0A **** DISPLAYING (IN HEX) FILE: %s at offset = %d (=0x%x) ****\0A\0A\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"starting at 0x%x:\0A\00"
@.str.3 = [7 x i8] c"%2.2x \00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @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 [16 x i8]
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %11
br label %18
18: ; preds = %14, %2
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %9
br label %26
26: ; preds = %21, %18
%27 = load i8*, i8** %11
%28 = load i32, i32* %9
%29 = load i32, i32* %9
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i8* %27, i32 %28, i32 %29)
%31 = load i8*, i8** %11
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i8*, i8** %11
%37 = call i32 @perror(i8* %36)
%38 = load i32, i32* @errno
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %26
%41 = load i32, i32* %6
%42 = load i32, i32* %9
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @lseek(i32 %41, i32 %42, i32 %43)
%45 = icmp eq i32 -1, %44
br i1 %45, label %46, label %51
46: ; preds = %40
%47 = load i8*, i8** %11
%48 = call i32 @perror(i8* %47)
%49 = load i32, i32* @errno
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %40
%52 = load i32, i32* %9
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %81, %51
%55 = load i32, i32* %6
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 16)
store i32 %57, i32* %7
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %54
%60 = load i8*, i8** %11
%61 = call i32 @perror(i8* %60)
%62 = load i32, i32* @errno
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %54
store i32 0, i32* %8
br label %65
65: ; preds = %76, %64
%66 = load i32, i32* %8
%67 = load i32, i32* %7
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %79
69: ; preds = %65
%70 = load i32, i32* %8
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = zext i8 %73 to i32
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %69
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
store i32 %78, i32* %8
br label %65
79: ; preds = %65
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %81
81: ; preds = %79
%82 = load i32, i32* %7
%83 = icmp sgt i32 %82, 0
br i1 %83, label %54, label %84
84: ; preds = %81
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
%87 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [66 x i8] c"\0A **** DISPLAYING (IN HEX) FILE: %s at offset = %d (=0x%x) ****\0A\0A\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"starting at 0x%x:\0A\00"
@.str.3 = [7 x i8] c"%2.2x \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %2, %10, %6
%15 = phi i8* [ %8, %10 ], [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%16 = phi i32 [ %13, %10 ], [ 0, %6 ], [ 0, %2 ]
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %16, i32 %16)
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %14
%22 = tail call i32 @perror(i8* %15)
%23 = load i32, i32* @errno
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %14
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %19, i32 %16, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = tail call i32 @perror(i8* %15)
%31 = load i32, i32* @errno
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %25
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %16)
br label %35
35: ; preds = %56, %33
%36 = call i32 @read(i32 %19, i8* %4, i32 16)
%37 = icmp slt i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %35
%39 = icmp eq i32 %36, 0
br i1 %39, label %40, label %42
40: ; preds = %38
%41 = tail call i32 @putchar(i32 10)
br label %59
42: ; preds = %38
%43 = zext i32 %36 to i64
br label %48
44: ; preds = %35
%45 = tail call i32 @perror(i8* %15)
%46 = load i32, i32* @errno
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %42, %48
%49 = phi i64 [ 0, %42 ], [ %54, %48 ]
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = add nuw nsw i64 %49, 1
%55 = icmp eq i64 %54, %43
br i1 %55, label %56, label %48
56: ; preds = %48
%57 = tail call i32 @putchar(i32 10)
%58 = icmp sgt i32 %36, 0
br i1 %58, label %35, label %59
59: ; preds = %56, %40
%60 = tail call i32 @close(i32 %19)
%61 = tail call i32 @exit(i32 0)
unreachable
}