#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 uint8_t;
typedef int /*<<< orphan*/ out;

/* Variables and functions */
int /*<<< orphan*/ isgraph(char);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void hexdump(uint8_t *buf, unsigned len) {
  const unsigned bpl = 8;
  const unsigned spc = 2;
  char out[bpl * 4 + spc + 1];
  int outpos = 0;
  memset(out, ' ', sizeof(out));
  out[sizeof(out) - 1] = 0;
  for (unsigned i = 0; i < len; i++) {
    sprintf(out + outpos, "%02x ", buf[i]);
    char c = buf[i];
    if (!isgraph(c))
      c = '.';
    out[(bpl * 3) + spc + (outpos / 3)] = c;
    outpos += 3;
    if (i % bpl == (bpl - 1) || i + 1 == len) {
      out[outpos] = ' ';
      ;
      memset(out, ' ', sizeof(out) - 1);
      outpos = 0;
    }
  }
}


x86 assembly (clang, -O0):
<hexdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x8,-0x10(%rbp)
movl   $0x2,-0x14(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x20,%esi
mov    $0x23,%edx
call   1030 <memset@plt>
movb   $0x0,-0x2e(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    123d <hexdump+0x10d>
movslq -0x24(%rbp),%rax
lea    -0x50(%rbp),%rdi
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    (%rax,%rcx,4),%edx
lea    0xe5f(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %al,-0x29(%rbp)
movsbl -0x29(%rbp),%edi
call   1050 <isgraph@plt>
cmp    $0x0,%eax
jne    11c9 <hexdump+0x99>
movb   $0x2e,-0x29(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x24(%rbp),%eax
mov    $0x3,%esi
cltd
idiv   %esi
add    $0x1a,%eax
mov    %eax,%eax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x24(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
and    $0x7,%eax
cmp    $0x7,%eax
je     1207 <hexdump+0xd7>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cmp    -0xc(%rbp),%eax
jne    122a <hexdump+0xfa>
movslq -0x24(%rbp),%rax
movb   $0x20,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rdi
mov    $0x20,%esi
mov    $0x22,%edx
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
jmp    122f <hexdump+0xff>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1179 <hexdump+0x49>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
add    $0x50,%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,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x20,%esi
mov    $0x23,%edx
call   1030 <memset@plt>
movb   $0x0,0x22(%rsp)
test   %ebp,%ebp
je     1205 <hexdump+0xd5>
mov    %ebp,%r12d
lea    -0x1(%r12),%r13
xor    %r15d,%r15d
xor    %r14d,%r14d
jmp    11a2 <hexdump+0x72>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x20,(%rsp,%r14,1)
mov    %rsp,%rdi
mov    $0x20,%esi
mov    $0x22,%edx
call   1030 <memset@plt>
xor    %r14d,%r14d
mov    %rbp,%r15
cmp    %rbp,%r12
je     1205 <hexdump+0xd5>
movslq %r14d,%r14
lea    (%rsp,%r14,1),%rdi
mov    (%rbx,%r15,4),%edx
lea    0xe4c(%rip),%rsi
call   1040 <sprintf@plt>
movsbl (%rbx,%r15,4),%ebp
mov    %ebp,%edi
call   1050 <isgraph@plt>
test   %eax,%eax
mov    $0x2e,%eax
cmove  %eax,%ebp
imul   $0x55555556,%r14,%rax
mov    %rax,%rcx
shr    $0x20,%rcx
shr    $0x3f,%rax
add    %ecx,%eax
add    $0x1a,%eax
mov    %bpl,(%rsp,%rax,1)
add    $0x3,%r14
mov    %r15d,%eax
not    %eax
lea    0x1(%r15),%rbp
test   $0x7,%al
je     1180 <hexdump+0x50>
cmp    %r15,%r13
jne    119a <hexdump+0x6a>
jmp    1180 <hexdump+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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