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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
void hexdump(void *d, size_t len, int direction) {
  if (len == 0)
    return;
  unsigned char *s;
  uint16_t i = 0;
  if (direction == 0) {
    printf(">>  ");
  } else if (direction == 1) {
    printf("<<  ");
  } else {
    printf("--  ");
  }
  for (s = d; len; len--, s++) {
    printf("%02x ", *s);
    i++;
    if (i > 0) {
      if (i % 24 == 0)
        printf("\n");
      else if (i % 4 == 0)
        printf("  ");
    }
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1133 <hexdump+0x23>
jmp    1245 <hexdump+0x135>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1157 <hexdump+0x47>
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1187 <hexdump+0x77>
cmpl   $0x1,-0x14(%rbp)
jne    1174 <hexdump+0x64>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1182 <hexdump+0x72>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1187 <hexdump+0x77>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1237 <hexdump+0x127>
mov    -0x20(%rbp),%rax
movzbl (%rax),%esi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1215 <hexdump+0x105>
mov    -0x24(%rbp),%eax
mov    $0x18,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11e9 <hexdump+0xd9>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1210 <hexdump+0x100>
mov    -0x24(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    120b <hexdump+0xfb>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1210 <hexdump+0x100>
jmp    1215 <hexdump+0x105>
jmp    121a <hexdump+0x10a>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    118f <hexdump+0x7f>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexdump>:
test   %rsi,%rsi
je     11e9 <hexdump+0xc9>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0x1,%edx
lea    0xec1(%rip),%rax
lea    0xebf(%rip),%rcx
cmove  %rax,%rcx
test   %edx,%edx
lea    0xea8(%rip),%rdi
cmovne %rcx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
xor    %r13d,%r13d
jmp    1182 <hexdump+0x62>
nopl   (%rax)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%ebp
cmp    %r13,%rbx
je     11d1 <hexdump+0xb1>
mov    %r13,%r12
mov    %ebp,%eax
mov    $0xaaaaaaab,%ecx
imul   %rcx,%rax
shr    $0x24,%rax
lea    (%rax,%rax,2),%eax
lea    -0x1(,%rax,8),%r15d
ff
movzbl (%r14,%r13,1),%esi
lea    0xe64(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r13
cmp    %r12d,%r15d
je     1170 <hexdump+0x50>
test   $0x3,%r13b
jne    117a <hexdump+0x5a>
lea    0xe44(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    117a <hexdump+0x5a>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
ret

LLVM IR (-O0):
@.str = [5 x i8] c">> \00"
@.str.1 = [5 x i8] c"<< \00"
@.str.2 = [5 x i8] c"-- \00"
@.str.3 = [6 x i8] c"%02x \00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [3 x i8] c" \00"
define void @hexdump(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%9 = load i64, i64* %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %3
br label %61
12: ; preds = %3
store i32 0, i32* %8
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %25
17: ; preds = %12
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %20
br label %25
25: ; preds = %24, %15
%26 = load i8*, i8** %4
store i8* %26, i8** %7
br label %27
27: ; preds = %54, %25
%28 = load i64, i64* %5
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %59
30: ; preds = %27
%31 = load i8*, i8** %7
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %53
39: ; preds = %30
%40 = load i32, i32* %8
%41 = srem i32 %40, 24
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %52
45: ; preds = %39
%46 = load i32, i32* %8
%47 = srem i32 %46, 4
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
br label %52
52: ; preds = %51, %43
br label %53
53: ; preds = %52, %30
br label %54
54: ; preds = %53
%55 = load i64, i64* %5
%56 = add i64 %55, -1
store i64 %56, i64* %5
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %7
br label %27
59: ; preds = %27
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %11
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c">> \00"
@.str.1 = [5 x i8] c"<< \00"
@.str.2 = [5 x i8] c"-- \00"
@.str.3 = [6 x i8] c"%02x \00"
@.str.5 = [3 x i8] c" \00"
define void @hexdump(i8* nocapture %0, i64 %1, i32 %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %34, label %5
5: ; preds = %3
%6 = icmp eq i32 %2, 1
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0)
%8 = icmp eq i32 %2, 0
%9 = select i1 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %7
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %9)
br label %11
11: ; preds = %5, %28
%12 = phi i32 [ %18, %28 ], [ 0, %5 ]
%13 = phi i8* [ %30, %28 ], [ %0, %5 ]
%14 = phi i64 [ %29, %28 ], [ %1, %5 ]
%15 = load i8, i8* %13
%16 = zext i8 %15 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = add nuw nsw i32 %12, 1
%19 = urem i32 %18, 24
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = tail call i32 @putchar(i32 10)
br label %28
23: ; preds = %11
%24 = and i32 %18, 3
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %23, %26, %21
%29 = add i64 %14, -1
%30 = getelementptr inbounds i8, i8* %13, i64 1
%31 = icmp eq i64 %29, 0
br i1 %31, label %32, label %11
32: ; preds = %28
%33 = tail call i32 @putchar(i32 10)
br label %34
34: ; preds = %3, %32
ret void
}