#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 */
scalar_t__ isprint(int);
int /*<<< orphan*/ printf(char *, unsigned int, char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void LogBuffer(const uint8_t *p, unsigned n) {
  char hex[(3 * 16) + 1];
  char txt[16 + 1];
  unsigned odx = 0, idx = 0, at = 0;
  for (idx = 0; idx < n; idx++) {
    uint8_t byte = p[idx];
    sprintf(hex + 3 * odx, "%2.02X ", byte);
    txt[odx++] = isprint(byte) ? byte : '.';
    if (odx == 16) {
      txt[odx] = hex[3 * odx] = '\0';
      printf("[%5u] %s %s\n", at, hex, txt);
      at = idx + 1;
      odx = 0;
    }
  }
  if (odx) {
    txt[odx] = hex[3 * odx] = '\0';
    printf("[%5u] %-48.48s %s\n", at, hex, txt);
  }
}


x86 assembly (clang, -O0):
<LogBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x64(%rbp)
movl   $0x0,-0x68(%rbp)
movl   $0x0,-0x6c(%rbp)
movl   $0x0,-0x68(%rbp)
mov    -0x68(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    122a <LogBuffer+0xfa>
mov    -0x8(%rbp),%rax
mov    -0x68(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x70(%rbp)
lea    -0x40(%rbp),%rdi
imul   $0x3,-0x64(%rbp),%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x70(%rbp),%edx
lea    0xe72(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x70(%rbp),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
je     11b0 <LogBuffer+0x80>
mov    -0x70(%rbp),%eax
mov    %eax,-0x74(%rbp)
jmp    11bd <LogBuffer+0x8d>
mov    $0x2e,%eax
mov    %eax,-0x74(%rbp)
jmp    11bd <LogBuffer+0x8d>
mov    -0x74(%rbp),%eax
mov    %al,%cl
mov    -0x64(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x64(%rbp)
mov    %eax,%eax
mov    %cl,-0x60(%rbp,%rax,1)
cmpl   $0x10,-0x64(%rbp)
jne    1217 <LogBuffer+0xe7>
imul   $0x3,-0x64(%rbp),%eax
mov    %eax,%eax
movb   $0x0,-0x40(%rbp,%rax,1)
mov    -0x64(%rbp),%eax
movb   $0x0,-0x60(%rbp,%rax,1)
mov    -0x6c(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    0xe06(%rip),%rdi
call   1030 <printf@plt>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
movl   $0x0,-0x64(%rbp)
jmp    121c <LogBuffer+0xec>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    115e <LogBuffer+0x2e>
cmpl   $0x0,-0x64(%rbp)
je     125e <LogBuffer+0x12e>
imul   $0x3,-0x64(%rbp),%eax
mov    %eax,%eax
movb   $0x0,-0x40(%rbp,%rax,1)
mov    -0x64(%rbp),%eax
movb   $0x0,-0x60(%rbp,%rax,1)
mov    -0x6c(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%rcx
lea    0xdbc(%rip),%rdi
call   1030 <printf@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LogBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
test   %esi,%esi
je     1207 <LogBuffer+0xd7>
mov    %rdi,%rbx
mov    %esi,%r12d
xor    %r13d,%r13d
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    1169 <LogBuffer+0x39>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    %r12,%r13
je     11dc <LogBuffer+0xac>
mov    (%rbx,%r13,4),%r15d
lea    (%r14,%r14,2),%eax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
lea    0xe80(%rip),%rsi
mov    %r15d,%edx
call   1050 <sprintf@plt>
mov    %r15d,%edi
call   1040 <isprint@plt>
test   %rax,%rax
mov    $0x2e,%eax
cmove  %eax,%r15d
mov    %r14d,%eax
add    $0x1,%r14d
mov    %r15b,(%rsp,%rax,1)
cmp    $0x10,%r14d
jne    1160 <LogBuffer+0x30>
movb   $0x0,0x50(%rsp)
movb   $0x0,0x10(%rsp)
lea    0xe4a(%rip),%rdi
mov    %ebp,%esi
lea    0x20(%rsp),%rdx
mov    %rsp,%rcx
call   1030 <printf@plt>
add    $0x1,%r13
xor    %r14d,%r14d
mov    %r13d,%ebp
cmp    %r12,%r13
jne    1169 <LogBuffer+0x39>
test   %r14d,%r14d
je     1207 <LogBuffer+0xd7>
lea    (%r14,%r14,2),%eax
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r14d,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe1d(%rip),%rdi
lea    0x20(%rsp),%rdx
mov    %rsp,%rcx
mov    %ebp,%esi
call   1030 <printf@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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