#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 int32_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int hexdump(int in_fd, int bytes) {
  char buffer[16];
  int result, i;
  int32_t pos = 0;
  while (1) {
    result = read(in_fd, buffer, 16);
    if (result < 0) {
      printf("Error reading\n");
      break;
    }
    if (result == 0) {
      printf("\n");
      printf("Total size=%d\n", pos);
      break;
    }
    for (i = 0; i < 16; i++) {
      if (i % 16 == 0)
        printf("%08x: ", pos + i);
      if (i % 16 == 8)
        printf(" ");
      if (i < result)
        printf("%02x ", buffer[i] & 0xff);
      else
        printf("   ");
    }
    printf("  |");
    for (i = 0; i < 16; i++) {
      if (i >= result)
        break;
      if ((buffer[i] >= ' ') && (buffer[i] < 127)) {
        printf("%c", buffer[i]);
      } else {
        printf(".");
      }
    }
    printf("|\n");
    pos += result;
    if ((bytes > 0) && (pos >= bytes))
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1166 <hexdump+0x46>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1306 <hexdump+0x1e6>
cmpl   $0x0,-0x24(%rbp)
jne    1194 <hexdump+0x74>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1306 <hexdump+0x1e6>
movl   $0x0,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jge    123e <hexdump+0x11e>
mov    -0x28(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11cd <hexdump+0xad>
mov    -0x2c(%rbp),%esi
add    -0x28(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x8,%edx
jne    11ef <hexdump+0xcf>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    121d <hexdump+0xfd>
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%esi
and    $0xff,%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122b <hexdump+0x10b>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1230 <hexdump+0x110>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    119b <hexdump+0x7b>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jge    12cf <hexdump+0x1af>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     126e <hexdump+0x14e>
jmp    12cf <hexdump+0x1af>
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%eax
cmp    $0x20,%eax
jl     12ae <hexdump+0x18e>
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%eax
cmp    $0x7f,%eax
jge    12ae <hexdump+0x18e>
movslq -0x28(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12bc <hexdump+0x19c>
lea    0xd81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12c1 <hexdump+0x1a1>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1253 <hexdump+0x133>
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1301 <hexdump+0x1e1>
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     1301 <hexdump+0x1e1>
jmp    1306 <hexdump+0x1e6>
jmp    1135 <hexdump+0x15>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,0x8(%rsp)
mov    %edi,0xc(%rsp)
lea    0xeb9(%rip),%rbp
mov    $0x2e,%r12d
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
test   %eax,%eax
js     127a <hexdump+0x13a>
je     1288 <hexdump+0x148>
mov    %eax,%r14d
mov    %eax,%r15d
xor    %ebx,%ebx
jmp    11b8 <hexdump+0x78>
nopl   0x0(%rax,%rax,1)
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    $0x10,%rbx
je     1200 <hexdump+0xc0>
cmp    $0x8,%ebx
je     11e0 <hexdump+0xa0>
test   %ebx,%ebx
jne    11d2 <hexdump+0x92>
lea    0xe47(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %r15,%rbx
jae    11a0 <hexdump+0x60>
jmp    11ef <hexdump+0xaf>
nopl   0x0(%rax)
mov    $0x20,%edi
call   1030 <putchar@plt>
cmp    %r15,%rbx
jae    11a0 <hexdump+0x60>
movzbl 0x10(%rsp,%rbx,1),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11ae <hexdump+0x6e>
mov    %rbp,%r15
lea    0xe16(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r14d,%r14d
jle    1250 <hexdump+0x110>
lea    -0x1(%r14),%ebx
cmp    $0xf,%ebx
mov    $0xf,%eax
cmovae %eax,%ebx
add    $0x1,%ebx
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
movzbl 0x10(%rsp,%rbp,1),%eax
lea    -0x7f(%rax),%ecx
cmp    $0xa1,%cl
cmovb  %r12d,%eax
movzbl %al,%edi
call   1030 <putchar@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1230 <hexdump+0xf0>
lea    0xdcb(%rip),%rdi
call   1040 <puts@plt>
add    %r14d,%r13d
cmpl   $0x0,0x8(%rsp)
mov    %r15,%rbp
jle    1170 <hexdump+0x30>
cmp    0x8(%rsp),%r13d
jl     1170 <hexdump+0x30>
jmp    12a3 <hexdump+0x163>
lea    0xda3(%rip),%rdi
call   1040 <puts@plt>
jmp    12a3 <hexdump+0x163>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xd67(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Error reading\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [15 x i8] c"Total size=%d\0A\00"
@.str.3 = [7 x i8] c"%08x: \00"
@.str.4 = [2 x i8] c" \00"
@.str.5 = [6 x i8] c"%02x \00"
@.str.6 = [4 x i8] c" \00"
@.str.7 = [4 x i8] c" |\00"
@.str.8 = [3 x i8] c"%c\00"
@.str.9 = [2 x i8] c".\00"
@.str.10 = [3 x i8] c"|\0A\00"
define i32 @hexdump(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [16 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %8
br label %9
9: ; preds = %2, %110
%10 = load i32, i32* %3
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 16)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %111
17: ; preds = %9
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %111
24: ; preds = %17
store i32 0, i32* %7
br label %25
25: ; preds = %58, %24
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 16
br i1 %27, label %28, label %61
28: ; preds = %25
%29 = load i32, i32* %7
%30 = srem i32 %29, 16
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = add nsw i32 %33, %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %32, %28
%38 = load i32, i32* %7
%39 = srem i32 %38, 16
%40 = icmp eq i32 %39, 8
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %43
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = and i32 %52, 255
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %53)
br label %57
55: ; preds = %43
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
br label %57
57: ; preds = %55, %47
br label %58
58: ; preds = %57
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
br label %25
61: ; preds = %25
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %7
br label %63
63: ; preds = %95, %61
%64 = load i32, i32* %7
%65 = icmp slt i32 %64, 16
br i1 %65, label %66, label %98
66: ; preds = %63
%67 = load i32, i32* %7
%68 = load i32, i32* %6
%69 = icmp sge i32 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66
br label %98
71: ; preds = %66
%72 = load i32, i32* %7
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %73
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = icmp sge i32 %76, 32
br i1 %77, label %78, label %92
78: ; preds = %71
%79 = load i32, i32* %7
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = icmp slt i32 %83, 127
br i1 %84, label %85, label %92
85: ; preds = %78
%86 = load i32, i32* %7
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 %87
%89 = load i8, i8* %88
%90 = sext i8 %89 to i32
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %90)
br label %94
92: ; preds = %78, %71
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
br label %94
94: ; preds = %92, %85
br label %95
95: ; preds = %94
%96 = load i32, i32* %7
%97 = add nsw i32 %96, 1
store i32 %97, i32* %7
br label %63
98: ; preds = %70, %63
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%100 = load i32, i32* %6
%101 = load i32, i32* %8
%102 = add nsw i32 %101, %100
store i32 %102, i32* %8
%103 = load i32, i32* %4
%104 = icmp sgt i32 %103, 0
br i1 %104, label %105, label %110
105: ; preds = %98
%106 = load i32, i32* %8
%107 = load i32, i32* %4
%108 = icmp sge i32 %106, %107
br i1 %108, label %109, label %110
109: ; preds = %105
br label %111
110: ; preds = %105, %98
br label %9
111: ; preds = %109, %20, %15
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [15 x i8] c"Total size=%d\0A\00"
@.str.3 = [7 x i8] c"%08x: \00"
@.str.5 = [6 x i8] c"%02x \00"
@.str.6 = [4 x i8] c" \00"
@.str.7 = [4 x i8] c" |\00"
@str = [2 x i8] c"|\00"
@str.11 = [14 x i8] c"Error reading\00"
define i32 @hexdump(i32 %0, i32 %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 1
br label %6
6: ; preds = %57, %2
%7 = phi i32 [ 0, %2 ], [ %59, %57 ]
%8 = call i32 @read(i32 %0, i8* %4, i32 16)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, i64 0, i64 0))
br label %62
12: ; preds = %6
%13 = icmp eq i32 %8, 0
br i1 %13, label %16, label %14
14: ; preds = %12
%15 = zext i32 %8 to i64
br label %19
16: ; preds = %12
%17 = tail call i32 @putchar(i32 10)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %62
19: ; preds = %14, %35
%20 = phi i64 [ 0, %14 ], [ %36, %35 ]
%21 = trunc i64 %20 to i32
switch i32 %21, label %26 [
i32 0, label %22
i32 8, label %24
]
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %7)
br label %26
24: ; preds = %19
%25 = tail call i32 @putchar(i32 32)
br label %26
26: ; preds = %22, %19, %24
%27 = icmp ult i64 %20, %15
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %20
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i32 %31)
br label %35
33: ; preds = %26
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
br label %35
35: ; preds = %28, %33
%36 = add nuw nsw i64 %20, 1
%37 = icmp eq i64 %36, 16
br i1 %37, label %38, label %19
38: ; preds = %35
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%40 = icmp sgt i32 %8, 0
br i1 %40, label %41, label %57
41: ; preds = %38
%42 = add nsw i32 %8, -1
%43 = call i32 @llvm.umin.i32(i32 %42, i32 15)
%44 = add nuw nsw i32 %43, 1
%45 = zext i32 %44 to i64
br label %46
46: ; preds = %41, %46
%47 = phi i64 [ 0, %41 ], [ %55, %46 ]
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 %47
%49 = load i8, i8* %48
%50 = add i8 %49, -127
%51 = icmp ult i8 %50, -95
%52 = select i1 %51, i8 46, i8 %49
%53 = zext i8 %52 to i32
%54 = tail call i32 @putchar(i32 %53)
%55 = add nuw nsw i64 %47, 1
%56 = icmp eq i64 %55, %45
br i1 %56, label %57, label %46
57: ; preds = %46, %38
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%59 = add nsw i32 %8, %7
%60 = icmp slt i32 %59, %1
%61 = select i1 %5, i1 true, i1 %60
br i1 %61, label %6, label %62
62: ; preds = %57, %16, %10
ret i32 0
}