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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int, char *,
                           char *);
scalar_t__ isprint(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
void LogBuffer(const uint8_t *p, unsigned n, FILE *out_file) {
  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';
      fprintf(out_file, "[%5u] %s %s\n", at, hex, txt);
      at = idx + 1;
      odx = 0;
    }
  }
  if (odx) {
    txt[odx] = hex[3 * odx] = '\0';
    fprintf(out_file, "[%5u] %-48.48s %s\n", at, hex, txt);
  }
}


x86 assembly (clang, -O0):
<LogBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x74(%rbp)
movl   $0x0,-0x78(%rbp)
movl   $0x0,-0x7c(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    123b <LogBuffer+0x10b>
mov    -0x8(%rbp),%rax
mov    -0x78(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x80(%rbp)
lea    -0x50(%rbp),%rdi
imul   $0x3,-0x74(%rbp),%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x80(%rbp),%edx
lea    0xe6e(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x80(%rbp),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
je     11b7 <LogBuffer+0x87>
mov    -0x80(%rbp),%eax
mov    %eax,-0x84(%rbp)
jmp    11c7 <LogBuffer+0x97>
mov    $0x2e,%eax
mov    %eax,-0x84(%rbp)
jmp    11c7 <LogBuffer+0x97>
mov    -0x84(%rbp),%eax
mov    %al,%cl
mov    -0x74(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x74(%rbp)
mov    %eax,%eax
mov    %cl,-0x70(%rbp,%rax,1)
cmpl   $0x10,-0x74(%rbp)
jne    1228 <LogBuffer+0xf8>
imul   $0x3,-0x74(%rbp),%eax
mov    %eax,%eax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x74(%rbp),%eax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
mov    -0x7c(%rbp),%edx
lea    -0x50(%rbp),%rcx
lea    -0x70(%rbp),%r8
lea    0xdf5(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
movl   $0x0,-0x74(%rbp)
jmp    122d <LogBuffer+0xfd>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1162 <LogBuffer+0x32>
cmpl   $0x0,-0x74(%rbp)
je     1273 <LogBuffer+0x143>
imul   $0x3,-0x74(%rbp),%eax
mov    %eax,%eax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x74(%rbp),%eax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
mov    -0x7c(%rbp),%edx
lea    -0x50(%rbp),%rcx
lea    -0x70(%rbp),%r8
lea    0xda7(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LogBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdx,0x8(%rsp)
test   %esi,%esi
je     1217 <LogBuffer+0xe7>
mov    %rdi,%rbx
mov    %esi,%r12d
xor    %r14d,%r14d
xor    %r15d,%r15d
xor    %ebp,%ebp
jmp    1169 <LogBuffer+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r12,%r14
je     11e4 <LogBuffer+0xb4>
mov    (%rbx,%r14,4),%r13d
lea    (%r15,%r15,2),%eax
lea    (%rsp,%rax,1),%rdi
add    $0x30,%rdi
lea    0xe80(%rip),%rsi
mov    %r13d,%edx
call   1050 <sprintf@plt>
mov    %r13d,%edi
call   1040 <isprint@plt>
test   %rax,%rax
mov    $0x2e,%eax
cmove  %eax,%r13d
mov    %r15d,%eax
add    $0x1,%r15d
mov    %r13b,0x10(%rsp,%rax,1)
cmp    $0x10,%r15d
jne    1160 <LogBuffer+0x30>
movb   $0x0,0x60(%rsp)
movb   $0x0,0x20(%rsp)
mov    0x8(%rsp),%rdi
lea    0xe44(%rip),%rsi
mov    %ebp,%edx
lea    0x30(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1030 <fprintf@plt>
add    $0x1,%r14
xor    %r15d,%r15d
mov    %r14d,%ebp
cmp    %r12,%r14
jne    1169 <LogBuffer+0x39>
test   %r15d,%r15d
je     1217 <LogBuffer+0xe7>
lea    (%r15,%r15,2),%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r15d,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe14(%rip),%rsi
lea    0x30(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    0x8(%rsp),%rdi
mov    %ebp,%edx
call   1030 <fprintf@plt>
add    $0x68,%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, i32* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [49 x i8]
%8 = alloca [17 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %10
br label %13
13: ; preds = %61, %3
%14 = load i32, i32* %10
%15 = load i32, i32* %5
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %64
17: ; preds = %13
%18 = load i32*, i32** %4
%19 = load i32, i32* %10
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
%22 = load i32, i32* %21
store i32 %22, i32* %12
%23 = getelementptr inbounds [49 x i8], [49 x i8]* %7, i64 0, i64 0
%24 = load i32, i32* %9
%25 = mul i32 3, %24
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i32, i32* %12
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %12
%31 = call i64 @isprint(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %17
%34 = load i32, i32* %12
br label %36
35: ; preds = %17
br label %36
36: ; preds = %35, %33
%37 = phi i32 [ %34, %33 ], [ 46, %35 ]
%38 = trunc i32 %37 to i8
%39 = load i32, i32* %9
%40 = add i32 %39, 1
store i32 %40, i32* %9
%41 = zext i32 %39 to i64
%42 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %41
store i8 %38, i8* %42
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, 16
br i1 %44, label %45, label %60
45: ; preds = %36
%46 = load i32, i32* %9
%47 = mul i32 3, %46
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds [49 x i8], [49 x i8]* %7, i64 0, i64 %48
store i8 0, i8* %49
%50 = load i32, i32* %9
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %51
store i8 0, i8* %52
%53 = load i32*, i32** %6
%54 = load i32, i32* %11
%55 = getelementptr inbounds [49 x i8], [49 x i8]* %7, i64 0, i64 0
%56 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%57 = call i32 @fprintf(i32* %53, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %54, i8* %55, i8* %56)
%58 = load i32, i32* %10
%59 = add i32 %58, 1
store i32 %59, i32* %11
store i32 0, i32* %9
br label %60
60: ; preds = %45, %36
br label %61
61: ; preds = %60
%62 = load i32, i32* %10
%63 = add i32 %62, 1
store i32 %63, i32* %10
br label %13
64: ; preds = %13
%65 = load i32, i32* %9
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %80
67: ; preds = %64
%68 = load i32, i32* %9
%69 = mul i32 3, %68
%70 = zext i32 %69 to i64
%71 = getelementptr inbounds [49 x i8], [49 x i8]* %7, i64 0, i64 %70
store i8 0, i8* %71
%72 = load i32, i32* %9
%73 = zext i32 %72 to i64
%74 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 %73
store i8 0, i8* %74
%75 = load i32*, i32** %6
%76 = load i32, i32* %11
%77 = getelementptr inbounds [49 x i8], [49 x i8]* %7, i64 0, i64 0
%78 = getelementptr inbounds [17 x i8], [17 x i8]* %8, i64 0, i64 0
%79 = call i32 @fprintf(i32* %75, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %76, i8* %77, i8* %78)
br label %80
80: ; preds = %67, %64
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, i32* nocapture %2) {
%4 = alloca [49 x i8]
%5 = alloca [17 x i8]
%6 = getelementptr inbounds [49 x i8], [49 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%8 = icmp eq i32 %1, 0
br i1 %8, label %51, label %9
9: ; preds = %3
%10 = getelementptr inbounds [49 x i8], [49 x i8]* %4, i64 0, i64 48
%11 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 16
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %9, %37
%14 = phi i64 [ 0, %9 ], [ %38, %37 ]
%15 = phi i32 [ 0, %9 ], [ %40, %37 ]
%16 = phi i32 [ 0, %9 ], [ %39, %37 ]
%17 = getelementptr inbounds i32, i32* %0, i64 %14
%18 = load i32, i32* %17
%19 = mul i32 %15, 3
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds [49 x i8], [49 x i8]* %4, i64 0, i64 %20
%22 = call i32 @sprintf(i8* %21, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %18)
%23 = call i64 @isprint(i32 %18)
%24 = icmp eq i64 %23, 0
%25 = trunc i32 %18 to i8
%26 = select i1 %24, i8 46, i8 %25
%27 = add i32 %15, 1
%28 = zext i32 %15 to i64
%29 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 %28
store i8 %26, i8* %29
%30 = icmp eq i32 %27, 16
br i1 %30, label %33, label %31
31: ; preds = %13
%32 = add nuw nsw i64 %14, 1
br label %37
33: ; preds = %13
store i8 0, i8* %10
store i8 0, i8* %11
%34 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %16, i8* %6, i8* %7)
%35 = add nuw nsw i64 %14, 1
%36 = trunc i64 %35 to i32
br label %37
37: ; preds = %31, %33
%38 = phi i64 [ %32, %31 ], [ %35, %33 ]
%39 = phi i32 [ %16, %31 ], [ %36, %33 ]
%40 = phi i32 [ %27, %31 ], [ 0, %33 ]
%41 = icmp eq i64 %38, %12
br i1 %41, label %42, label %13
42: ; preds = %37
%43 = icmp eq i32 %40, 0
br i1 %43, label %51, label %44
44: ; preds = %42
%45 = mul i32 %40, 3
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds [49 x i8], [49 x i8]* %4, i64 0, i64 %46
store i8 0, i8* %47
%48 = zext i32 %40 to i64
%49 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 %48
store i8 0, i8* %49
%50 = call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %39, i8* %6, i8* %7)
br label %51
51: ; preds = %3, %44, %42
ret void
}