#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 */

/* Variables and functions */
char const buf2char(char const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ stdout;
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void log_data(const char *prefix, const char *buf, size_t nbytes) {
  const int block_size = 16;
  int i, block, nblocks;
  char hex_block[block_size * 10];
  char ascii_block[block_size * 10];
  nblocks = nbytes / block_size;
  if (nbytes % block_size > 0) {
    nblocks++;
  }
  for (block = 0; block < nblocks; block++) {
    hex_block[0] = '\0';
    ascii_block[0] = '\0';
    for (i = 0; i < block_size; i++) {
      int idx = block * block_size + i;
      if (idx >= nbytes) {
        break;
      }
      if (i == 0) {
        sprintf(hex_block, "%02X", (buf[idx] & 0xFF));
        sprintf(ascii_block, "%c", buf2char(buf[idx]));
      } else {
        sprintf(hex_block, "%s %02X", hex_block, (buf[idx] & 0xFF));
        sprintf(ascii_block, "%s%c", ascii_block, buf2char(buf[idx]));
      }
    }
    while (strlen(hex_block) < 3 * block_size) {
      strncat(hex_block, " ", 1);
    }
    fprintf(stdout, "[%s] %s%s\n", prefix, hex_block, ascii_block);
  }
}


x86 assembly (clang, -O0):
<log_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x10,-0x1c(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x4,%rax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
and    $0xf,%rax
cmp    $0x0,%rax
jbe    119b <log_data+0x4b>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1320 <log_data+0x1d0>
movb   $0x0,-0xe0(%rbp)
movb   $0x0,-0x180(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x10,-0x20(%rbp)
jge    12b4 <log_data+0x164>
mov    -0x24(%rbp),%eax
shl    $0x4,%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     11ec <log_data+0x9c>
jmp    12b4 <log_data+0x164>
cmpl   $0x0,-0x20(%rbp)
jne    124b <log_data+0xfb>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
and    $0xff,%edx
lea    -0xe0(%rbp),%rdi
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <buf2char@plt>
movsbl %al,%edx
lea    -0x180(%rbp),%rdi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    12a1 <log_data+0x151>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
and    $0xff,%ecx
lea    -0xe0(%rbp),%rdx
lea    0xd95(%rip),%rsi
mov    %rdx,%rdi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <buf2char@plt>
movsbl %al,%ecx
lea    -0x180(%rbp),%rdx
lea    0xd71(%rip),%rsi
mov    %rdx,%rdi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    12a6 <log_data+0x156>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11c3 <log_data+0x73>
jmp    12b9 <log_data+0x169>
lea    -0xe0(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x30,%eax
jge    12eb <log_data+0x19b>
lea    -0xe0(%rbp),%rdi
lea    0xd31(%rip),%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
jmp    12b9 <log_data+0x169>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd10(%rip),%rsi
lea    -0xe0(%rbp),%rcx
lea    -0x180(%rbp),%r8
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a2 <log_data+0x52>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x168,%rsp
mov    %rdx,%r13
mov    %rsi,0x10(%rsp)
mov    %rdi,0x8(%rsp)
mov    %rdx,%rax
shr    $0x4,%rax
mov    %r13d,%ecx
and    $0xf,%ecx
cmp    $0x1,%rcx
sbb    $0xffffffff,%eax
test   %eax,%eax
jle    12d2 <log_data+0x182>
mov    %eax,%eax
mov    %rax,(%rsp)
lea    0xc0(%rsp),%rbx
lea    0x20(%rsp),%r15
xor    %ebp,%ebp
jmp    11e3 <log_data+0x93>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    0x8(%rsp),%rdx
mov    %rbx,%rcx
mov    %r15,%r8
call   1060 <fprintf@plt>
mov    0x18(%rsp),%rbp
add    $0x1,%rbp
cmp    (%rsp),%rbp
je     12d2 <log_data+0x182>
mov    %rbp,0x18(%rsp)
shl    $0x4,%ebp
mov    0x10(%rsp),%rax
lea    (%rax,%rbp,1),%r12
movb   $0x0,0xc0(%rsp)
movb   $0x0,0x20(%rsp)
xor    %r14d,%r14d
jmp    124c <log_data+0xfc>
cs nopw 0x0(%rax,%rax,1)
lea    0xde9(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
movsbl (%r12,%r14,1),%edi
call   1030 <buf2char@plt>
movsbl %al,%ecx
mov    %r15,%rdi
lea    0xdd0(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
add    $0x1,%r14
cmp    $0x10,%r14
je     1290 <log_data+0x140>
lea    (%r14,%rbp,1),%rax
cmp    %r13,%rax
jae    1290 <log_data+0x140>
movzbl (%r12,%r14,1),%ecx
mov    %rbx,%rdi
test   %r14,%r14
jne    1210 <log_data+0xc0>
lea    0xd9a(%rip),%rsi
mov    %ecx,%edx
xor    %eax,%eax
call   1070 <sprintf@plt>
movsbl (%r12,%r14,1),%edi
call   1030 <buf2char@plt>
mov    %al,0x20(%rsp)
movb   $0x0,0x21(%rsp)
jmp    1242 <log_data+0xf2>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x2f,%eax
lea    0xd6b(%rip),%rbp
jg     11b0 <log_data+0x60>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <strncat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x30,%eax
jl     12b0 <log_data+0x160>
jmp    11b0 <log_data+0x60>
add    $0x168,%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 = [3 x i8] c"%c\00"
@.str.2 = [8 x i8] c"%s %02X\00"
@.str.3 = [5 x i8] c"%s%c\00"
@.str.4 = [2 x i8] c" \00"
@stdout = global i32 0
@.str.5 = [11 x i8] c"[%s] %s%s\0A\00"
define void @log_data(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 16, i32* %7
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %11
%14 = alloca i8, i64 160
%15 = alloca i8, i64 160
%16 = load i64, i64* %6
%17 = udiv i64 %16, 16
%18 = trunc i64 %17 to i32
store i32 %18, i32* %10
%19 = load i64, i64* %6
%20 = urem i64 %19, 16
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %3
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
store i32 %24, i32* %10
br label %25
25: ; preds = %22, %3
store i32 0, i32* %9
br label %26
26: ; preds = %97, %25
%27 = load i32, i32* %9
%28 = load i32, i32* %10
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %100
30: ; preds = %26
%31 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %31
%32 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %32
store i32 0, i32* %8
br label %33
33: ; preds = %84, %30
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 16
br i1 %35, label %36, label %87
36: ; preds = %33
%37 = load i32, i32* %9
%38 = mul nsw i32 %37, 16
%39 = load i32, i32* %8
%40 = add nsw i32 %38, %39
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = load i64, i64* %6
%44 = icmp uge i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %36
br label %87
46: ; preds = %36
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %66
49: ; preds = %46
%50 = load i8*, i8** %5
%51 = load i32, i32* %12
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = and i32 %55, 255
%57 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %56)
%58 = load i8*, i8** %5
%59 = load i32, i32* %12
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = call signext i8 @buf2char(i8 signext %62)
%64 = sext i8 %63 to i32
%65 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %64)
br label %83
66: ; preds = %46
%67 = load i8*, i8** %5
%68 = load i32, i32* %12
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = and i32 %72, 255
%74 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32 %73)
%75 = load i8*, i8** %5
%76 = load i32, i32* %12
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
%79 = load i8, i8* %78
%80 = call signext i8 @buf2char(i8 signext %79)
%81 = sext i8 %80 to i32
%82 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %15, i32 %81)
br label %83
83: ; preds = %66, %49
br label %84
84: ; preds = %83
%85 = load i32, i32* %8
%86 = add nsw i32 %85, 1
store i32 %86, i32* %8
br label %33
87: ; preds = %45, %33
br label %88
88: ; preds = %91, %87
%89 = call i32 @strlen(i8* %14)
%90 = icmp slt i32 %89, 48
br i1 %90, label %91, label %93
91: ; preds = %88
%92 = call i32 @strncat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %88
93: ; preds = %88
%94 = load i32, i32* @stdout
%95 = load i8*, i8** %4
%96 = call i32 @fprintf(i32 %94, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %95, i8* %14, i8* %15)
br label %97
97: ; preds = %93
%98 = load i32, i32* %9
%99 = add nsw i32 %98, 1
store i32 %99, i32* %9
br label %26
100: ; preds = %26
%101 = load i8*, i8** %11
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
@.str.2 = [8 x i8] c"%s %02X\00"
@.str.3 = [5 x i8] c"%s%c\00"
@.str.4 = [2 x i8] c" \00"
@stdout = global i32 0
@.str.5 = [11 x i8] c"[%s] %s%s\0A\00"
define void @log_data(i8* %0, i8* nocapture %1, i64 %2) {
%4 = alloca [160 x i8]
%5 = alloca [160 x i8]
%6 = getelementptr inbounds [160 x i8], [160 x i8]* %5, i64 0, i64 0
%7 = getelementptr inbounds [160 x i8], [160 x i8]* %4, i64 0, i64 0
%8 = lshr i64 %2, 4
%9 = trunc i64 %8 to i32
%10 = and i64 %2, 15
%11 = icmp ne i64 %10, 0
%12 = zext i1 %11 to i32
%13 = add i32 %12, %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %56
15: ; preds = %3
%16 = getelementptr inbounds [160 x i8], [160 x i8]* %5, i64 0, i64 1
%17 = zext i32 %13 to i64
br label %18
18: ; preds = %15, %51
%19 = phi i64 [ 0, %15 ], [ %54, %51 ]
store i8 0, i8* %7
store i8 0, i8* %6
%20 = shl i64 %19, 4
%21 = and i64 %20, 4294967280
br label %22
22: ; preds = %18, %41
%23 = phi i64 [ 0, %18 ], [ %42, %41 ]
%24 = add nuw nsw i64 %23, %21
%25 = icmp ult i64 %24, %2
br i1 %25, label %26, label %44
26: ; preds = %22
%27 = icmp eq i64 %23, 0
%28 = getelementptr inbounds i8, i8* %1, i64 %24
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
br i1 %27, label %31, label %35
31: ; preds = %26
%32 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %30)
%33 = load i8, i8* %28
%34 = call signext i8 @buf2char(i8 signext %33)
store i8 %34, i8* %6
store i8 0, i8* %16
br label %41
35: ; preds = %26
%36 = call i32 (i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %7, i32 %30)
%37 = load i8, i8* %28
%38 = call signext i8 @buf2char(i8 signext %37)
%39 = sext i8 %38 to i32
%40 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %6, i32 %39)
br label %41
41: ; preds = %35, %31
%42 = add nuw nsw i64 %23, 1
%43 = icmp eq i64 %42, 16
br i1 %43, label %44, label %22
44: ; preds = %22, %41
%45 = call i32 @strlen(i8* %7)
%46 = icmp slt i32 %45, 48
br i1 %46, label %47, label %51
47: ; preds = %44, %47
%48 = call i32 @strncat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%49 = call i32 @strlen(i8* %7)
%50 = icmp slt i32 %49, 48
br i1 %50, label %47, label %51
51: ; preds = %47, %44
%52 = load i32, i32* @stdout
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %7, i8* %6)
%54 = add nuw nsw i64 %19, 1
%55 = icmp eq i64 %54, %17
br i1 %55, label %56, label %18
56: ; preds = %51, %3
ret void
}