#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*/ linebuf;
typedef int /*<<< orphan*/ hex;

/* Variables and functions */
scalar_t__ isprint(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ tds_debug(int, char *, char *);
void dump_hex(int lvl, void *vp, size_t len) {
  char linebuf[80];
  int i;
  int linebuf_dirty = 0;
  unsigned char *p = (unsigned char *)vp;
  memset(linebuf, ' ', sizeof(linebuf));
  linebuf[70] = '\0';
  for (i = 0; i < len; i++) {
    int x = i % 16;
    int ch = (unsigned)p[i];
    char hex[20];
    if (x >= 8)
      x = x * 3 + 1;
    else
      x = x * 3;
    snprintf(hex, sizeof(hex), "%02x", ch);
    linebuf[x] = hex[0];
    linebuf[x + 1] = hex[1];
    if (isprint(ch))
      linebuf[52 + (i % 16)] = ch;
    else
      linebuf[52 + (i % 16)] = '.';
    linebuf_dirty = 1;
    if (!((i + 1) % 16)) {
      tds_debug(lvl, "%s\n", linebuf);
      memset(linebuf, ' ', sizeof(linebuf));
      linebuf[70] = '\0';
      linebuf_dirty = 0;
    }
  }
  if (linebuf_dirty == 1)
    tds_debug(lvl, "%s\n", linebuf);
}


x86 assembly (clang, -O0):
<dump_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x20,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movb   $0x0,-0x2a(%rbp)
movl   $0x0,-0x74(%rbp)
movslq -0x74(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12d6 <dump_hex+0x196>
mov    -0x74(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %edx,-0x84(%rbp)
mov    -0x80(%rbp),%rax
movslq -0x74(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x88(%rbp)
cmpl   $0x8,-0x84(%rbp)
jl     11d6 <dump_hex+0x96>
imul   $0x3,-0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11e3 <dump_hex+0xa3>
imul   $0x3,-0x84(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x88(%rbp),%ecx
mov    $0x14,%esi
lea    0xe04(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0xa0(%rbp),%cl
movslq -0x84(%rbp),%rax
mov    %cl,-0x70(%rbp,%rax,1)
mov    -0x9f(%rbp),%cl
mov    -0x84(%rbp),%eax
add    $0x1,%eax
cltq
mov    %cl,-0x70(%rbp,%rax,1)
mov    -0x88(%rbp),%edi
call   1060 <isprint@plt>
cmp    $0x0,%rax
je     125e <dump_hex+0x11e>
mov    -0x88(%rbp),%eax
mov    %al,%cl
mov    -0x74(%rbp),%eax
mov    $0x10,%esi
cltd
idiv   %esi
add    $0x34,%edx
movslq %edx,%rax
mov    %cl,-0x70(%rbp,%rax,1)
jmp    1274 <dump_hex+0x134>
mov    -0x74(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
add    $0x34,%edx
movslq %edx,%rax
movb   $0x2e,-0x70(%rbp,%rax,1)
movl   $0x1,-0x78(%rbp)
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12c3 <dump_hex+0x183>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rdx
lea    0xd65(%rip),%rsi
call   1030 <tds_debug@plt>
lea    -0x70(%rbp),%rdi
mov    $0x20,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movb   $0x0,-0x2a(%rbp)
movl   $0x0,-0x78(%rbp)
jmp    12c8 <dump_hex+0x188>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1183 <dump_hex+0x43>
cmpl   $0x1,-0x78(%rbp)
jne    12f3 <dump_hex+0x1b3>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rdx
lea    0xd17(%rip),%rsi
call   1030 <tds_debug@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,0xc(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x20,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movb   $0x0,0x76(%rsp)
test   %rbx,%rbx
je     1259 <dump_hex+0x119>
xor    %r13d,%r13d
jmp    1199 <dump_hex+0x59>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    %r13,%rbx
je     123e <dump_hex+0xfe>
mov    %r13d,%r12d
and    $0xf,%r12d
movzbl 0x0(%rbp,%r13,1),%r14d
cmp    $0x8,%r12d
lea    (%r12,%r12,2),%r15d
sbb    $0xffffffffffffffff,%r15
lea    0x10(%rsp),%rdi
mov    $0x14,%esi
lea    0xe3d(%rip),%rdx
mov    %r14d,%ecx
call   1040 <snprintf@plt>
movzwl 0x10(%rsp),%eax
mov    %ax,0x30(%rsp,%r15,1)
mov    %r14d,%edi
call   1060 <isprint@plt>
test   %rax,%rax
mov    $0x2e,%eax
cmove  %eax,%r14d
lea    0x30(%rsp),%rax
mov    %r14b,0x34(%r12,%rax,1)
add    $0x1,%r13
test   $0xf,%r13b
jne    1190 <dump_hex+0x50>
mov    0xc(%rsp),%edi
lea    0xdfc(%rip),%rsi
mov    %rbx,%r14
mov    %rbp,%rbx
lea    0x30(%rsp),%rbp
mov    %rbp,%rdx
call   1030 <tds_debug@plt>
mov    %rbp,%rdi
mov    %rbx,%rbp
mov    %r14,%rbx
mov    $0x20,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movb   $0x0,0x76(%rsp)
jmp    1190 <dump_hex+0x50>
test   $0xf,%r13b
je     1259 <dump_hex+0x119>
lea    0xdba(%rip),%rsi
lea    0x30(%rsp),%rdx
mov    0xc(%rsp),%edi
call   1030 <tds_debug@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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