#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(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;
  unsigned char line[16];
  char *filename = "/etc/passwd";
  if (argc > 1)
    filename = argv[1];
  printf("\n **** DISPLAYING (IN HEX) FILE: %s ****\n\n", filename);
  if ((fd = open(filename, O_RDONLY)) < 0) {
    perror(filename);
    exit(errno);
  };
  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)
lea    0xe83(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1197 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11de <main+0x7e>
mov    -0x38(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11e3 <main+0x83>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1217 <main+0xb7>
mov    -0x38(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    124f <main+0xef>
movslq -0x1c(%rbp),%rax
movzbl -0x30(%rbp,%rax,1),%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121e <main+0xbe>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jg     11e3 <main+0x83>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jl     1189 <main+0x19>
mov    0x8(%rsi),%rbx
jmp    1190 <main+0x20>
lea    0xe70(%rip),%rbx
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
jns    11bb <main+0x4b>
mov    %rbx,%rdi
jmp    121f <main+0xaf>
mov    %eax,%r12d
mov    %rbx,0x8(%rsp)
lea    0x10(%rsp),%r15
lea    0xe68(%rip),%rbx
nop
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1060 <read@plt>
test   %eax,%eax
js     121a <main+0xaa>
je     1232 <main+0xc2>
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    11f0 <main+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %r13d,%r13d
jg     11d0 <main+0x60>
jmp    123c <main+0xcc>
mov    0x8(%rsp),%rdi
call   1080 <perror@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [43 x i8] c"\0A **** DISPLAYING (IN HEX) FILE: %s ****\0A\0A\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.2 = [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 %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %6, %2
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %10, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = tail call i32 @perror(i8* %10)
%17 = load i32, i32* @errno
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9, %40
%20 = call i32 @read(i32 %13, i8* %4, i32 16)
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = icmp eq i32 %20, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = tail call i32 @putchar(i32 10)
br label %43
26: ; preds = %22
%27 = zext i32 %20 to i64
br label %32
28: ; preds = %19
%29 = tail call i32 @perror(i8* %10)
%30 = load i32, i32* @errno
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %26, %32
%33 = phi i64 [ 0, %26 ], [ %38, %32 ]
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = add nuw nsw i64 %33, 1
%39 = icmp eq i64 %38, %27
br i1 %39, label %40, label %32
40: ; preds = %32
%41 = tail call i32 @putchar(i32 10)
%42 = icmp sgt i32 %20, 0
br i1 %42, label %19, label %43
43: ; preds = %40, %24
%44 = tail call i32 @close(i32 %13)
%45 = tail call i32 @exit(i32 0)
unreachable
}