#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*/ tmp;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ isprint(unsigned char);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
void hexdump(unsigned char *buf, size_t len, FILE *fd) {
  size_t loop = 0, diff = 0, left = 0;
  unsigned char *p = NULL;
  char tmp[24];
  p = buf;
  memset(tmp, 0, sizeof(tmp));
  for (loop = 0; loop < len; ++loop, ++p) {
    if (loop && !(loop % 16)) {
      fprintf(fd, "| %s\n", tmp);
      memset(tmp, 0, 16);
    }
    fprintf(fd, "%02x ", *p);
    tmp[loop % 16] = isprint(*p) ? *p : '.';
  }
  diff = loop % 16;
  if (!diff) {
    fprintf(fd, "| %s\n", tmp);
    return;
  }
  left = 16 - diff;
  for (loop = 0; loop < left; ++loop) {
    fprintf(fd, "   ");
  }
  fprintf(fd, "| %s\n", tmp);
}


x86 assembly (clang, -O0):
<hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    124e <hexdump+0x11e>
cmpq   $0x0,-0x20(%rbp)
je     11d5 <hexdump+0xa5>
mov    -0x20(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
jne    11d5 <hexdump+0xa5>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rax
movzbl (%rax),%edx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rax
movzbl (%rax),%edi
call   1050 <isprint@plt>
cmp    $0x0,%rax
je     1213 <hexdump+0xe3>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x54(%rbp)
jmp    1220 <hexdump+0xf0>
mov    $0x2e,%eax
mov    %eax,-0x54(%rbp)
jmp    1220 <hexdump+0xf0>
mov    -0x54(%rbp),%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
and    $0xf,%rax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1184 <hexdump+0x54>
mov    -0x20(%rbp),%rax
and    $0xf,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1280 <hexdump+0x150>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xd8c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12dc <hexdump+0x1ac>
mov    $0x10,%eax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12c6 <hexdump+0x196>
mov    -0x18(%rbp),%rdi
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1295 <hexdump+0x165>
mov    -0x18(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xd2b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x60,%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    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
test   %rbx,%rbx
je     123a <hexdump+0xfa>
lea    0xe8e(%rip),%r14
xor    %r12d,%r12d
mov    %rbp,0x8(%rsp)
jmp    11a4 <hexdump+0x64>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%rbp,%r12,1),%eax
mov    %al,0x10(%rsp,%r13,1)
add    $0x1,%r12
cmp    %r12,%rbx
je     1209 <hexdump+0xc9>
mov    %r12d,%r13d
and    $0xf,%r13d
test   %r12,%r12
je     11e2 <hexdump+0xa2>
test   %r13,%r13
jne    11e2 <hexdump+0xa2>
mov    %r15,%rdi
lea    0xe41(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbp,%rdi
mov    0x8(%rsp),%rbp
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movzbl 0x0(%rbp,%r12,1),%edx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
movzbl 0x0(%rbp,%r12,1),%edi
call   1050 <isprint@plt>
test   %rax,%rax
jne    1190 <hexdump+0x50>
mov    $0x2e,%al
jmp    1196 <hexdump+0x56>
and    $0xf,%rbx
je     123a <hexdump+0xfa>
add    $0xfffffffffffffff0,%rbx
lea    0xdf2(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbp,%rdi
mov    %r15,%rcx
call   1060 <fwrite@plt>
inc    %rbx
jne    1220 <hexdump+0xe0>
lea    0xdbf(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"| %s\0A\00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [4 x i8] c" \00"
define void @hexdump(i8* %0, i64 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca [24 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
store i8* null, i8** %10
%12 = load i8*, i8** %4
store i8* %12, i8** %10
%13 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 24)
store i64 0, i64* %7
br label %15
15: ; preds = %53, %3
%16 = load i64, i64* %7
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %58
19: ; preds = %15
%20 = load i64, i64* %7
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %19
%23 = load i64, i64* %7
%24 = urem i64 %23, 16
%25 = icmp ne i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load i32*, i32** %6
%28 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 0
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 16)
br label %32
32: ; preds = %26, %22, %19
%33 = load i32*, i32** %6
%34 = load i8*, i8** %10
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %10
%39 = load i8, i8* %38
%40 = call i64 @isprint(i8 zeroext %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %32
%43 = load i8*, i8** %10
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
br label %47
46: ; preds = %32
br label %47
47: ; preds = %46, %42
%48 = phi i32 [ %45, %42 ], [ 46, %46 ]
%49 = trunc i32 %48 to i8
%50 = load i64, i64* %7
%51 = urem i64 %50, 16
%52 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 %51
store i8 %49, i8* %52
br label %53
53: ; preds = %47
%54 = load i64, i64* %7
%55 = add i64 %54, 1
store i64 %55, i64* %7
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %10
br label %15
58: ; preds = %15
%59 = load i64, i64* %7
%60 = urem i64 %59, 16
store i64 %60, i64* %8
%61 = load i64, i64* %8
%62 = icmp ne i64 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %58
%64 = load i32*, i32** %6
%65 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 0
%66 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %65)
br label %84
67: ; preds = %58
%68 = load i64, i64* %8
%69 = sub i64 16, %68
store i64 %69, i64* %9
store i64 0, i64* %7
br label %70
70: ; preds = %77, %67
%71 = load i64, i64* %7
%72 = load i64, i64* %9
%73 = icmp ult i64 %71, %72
br i1 %73, label %74, label %80
74: ; preds = %70
%75 = load i32*, i32** %6
%76 = call i32 (i32*, i8*, ...) @fprintf(i32* %75, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %77
77: ; preds = %74
%78 = load i64, i64* %7
%79 = add i64 %78, 1
store i64 %79, i64* %7
br label %70
80: ; preds = %70
%81 = load i32*, i32** %6
%82 = getelementptr inbounds [24 x i8], [24 x i8]* %11, i64 0, i64 0
%83 = call i32 (i32*, i8*, ...) @fprintf(i32* %81, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %82)
br label %84
84: ; preds = %80, %63
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"| %s\0A\00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [4 x i8] c" \00"
define void @hexdump(i8* nocapture %0, i64 %1, i32* nocapture %2) {
%4 = alloca [24 x i8]
%5 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 24)
%7 = icmp eq i64 %1, 0
br i1 %7, label %43, label %8
8: ; preds = %3, %27
%9 = phi i8* [ %31, %27 ], [ %0, %3 ]
%10 = phi i64 [ %30, %27 ], [ 0, %3 ]
%11 = icmp ne i64 %10, 0
%12 = and i64 %10, 15
%13 = icmp eq i64 %12, 0
%14 = and i1 %11, %13
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %5)
%17 = call i32 @memset(i8* %5, i32 0, i32 16)
br label %18
18: ; preds = %15, %8
%19 = load i8, i8* %9
%20 = zext i8 %19 to i32
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i8, i8* %9
%23 = call i64 @isprint(i8 zeroext %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = load i8, i8* %9
br label %27
27: ; preds = %18, %25
%28 = phi i8 [ %26, %25 ], [ 46, %18 ]
%29 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 %12
store i8 %28, i8* %29
%30 = add nuw i64 %10, 1
%31 = getelementptr inbounds i8, i8* %9, i64 1
%32 = icmp eq i64 %30, %1
br i1 %32, label %33, label %8
33: ; preds = %27
%34 = and i64 %1, 15
%35 = icmp eq i64 %34, 0
br i1 %35, label %43, label %36
36: ; preds = %33
%37 = sub nuw nsw i64 16, %34
br label %38
38: ; preds = %36, %38
%39 = phi i64 [ %41, %38 ], [ 0, %36 ]
%40 = call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i64 3, i64 1, i32* %2)
%41 = add nuw nsw i64 %39, 1
%42 = icmp eq i64 %41, %37
br i1 %42, label %43, label %38
43: ; preds = %38, %33, %3
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %5)
ret void
}