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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void ts_hex_dump_buf(char *buf, int bufsz, uint8_t *d, int size, int col) {
  int i;
  if (bufsz < size * 6)
    return;
  memset(buf, 0, bufsz);
  for (i = 0; i < size; i++) {
    if (col && (i % col == col - 1))
      sprintf(buf + (i * 3), "%02x\n", d[i]);
    else
      sprintf(buf + (i * 3), "%02x ", d[i]);
  }
}


x86 assembly (clang, -O0):
<ts_hex_dump_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0xc(%rbp),%eax
imul   $0x6,-0x1c(%rbp),%ecx
cmp    %ecx,%eax
jge    114e <ts_hex_dump_buf+0x2e>
jmp    11ee <ts_hex_dump_buf+0xce>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11ee <ts_hex_dump_buf+0xce>
cmpl   $0x0,-0x20(%rbp)
je     11b7 <ts_hex_dump_buf+0x97>
mov    -0x24(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
cmp    %eax,%edx
jne    11b7 <ts_hex_dump_buf+0x97>
mov    -0x8(%rbp),%rdi
imul   $0x3,-0x24(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe53(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11db <ts_hex_dump_buf+0xbb>
mov    -0x8(%rbp),%rdi
imul   $0x3,-0x24(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe30(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11e0 <ts_hex_dump_buf+0xc0>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1163 <ts_hex_dump_buf+0x43>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_hex_dump_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
lea    0x0(%rbp,%rbp,1),%eax
lea    (%rax,%rax,2),%eax
cmp    %esi,%eax
jg     11b0 <ts_hex_dump_buf+0x90>
mov    %r8d,%r14d
mov    %rdx,%r12
mov    %esi,%edx
mov    %rdi,%r15
xor    %ebx,%ebx
xor    %esi,%esi
call   1030 <memset@plt>
test   %ebp,%ebp
jle    11b0 <ts_hex_dump_buf+0x90>
lea    -0x1(%r14),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%eax
lea    (%rax,%rax,2),%rbp
xor    %r13d,%r13d
jmp    1196 <ts_hex_dump_buf+0x76>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe8f(%rip),%rsi
mov    %r13d,%edi
add    %r15,%rdi
mov    (%r12),%edx
call   1040 <sprintf@plt>
add    $0x3,%r13
add    $0x4,%r12
add    $0x1,%ebx
cmp    %r13,%rbp
je     11b0 <ts_hex_dump_buf+0x90>
test   %r14d,%r14d
je     1170 <ts_hex_dump_buf+0x50>
mov    %ebx,%eax
cltd
idiv   %r14d
lea    0xe58(%rip),%rsi
cmp    0x4(%rsp),%edx
jne    1170 <ts_hex_dump_buf+0x50>
jmp    1177 <ts_hex_dump_buf+0x57>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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