#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 */
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ prnlog(char *);
int snprintf(char *, size_t, char *, ...);
int strlen(char *);
void printvar(char *name, uint8_t *arr, int len) {
  int cx;
  size_t outsize = 40 + strlen(name) + len * 2;
  char *output = malloc(outsize);
  memset(output, 0, outsize);
  int i;
  cx = snprintf(output, outsize, "%s = ", name);
  for (i = 0; i < len; i++) {
    cx += snprintf(output + cx, outsize - cx, "%02x", *(arr + i));
  }
  prnlog(output);
}


x86 assembly (clang, -O0):
<printvar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x28,%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rcx
lea    0xe54(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    120c <printvar+0xbc>
mov    -0x28(%rbp),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x18(%rbp),%rax
sub    %rax,%rsi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xe15(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bd <printvar+0x6d>
mov    -0x28(%rbp),%rdi
call   1030 <prnlog@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printvar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
lea    (%rax,%r12,2),%eax
add    $0x28,%eax
movslq %eax,%r15
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1060 <memset@plt>
lea    0xe6e(%rip),%rdx
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
test   %r12d,%r12d
jle    11da <printvar+0x8a>
mov    %eax,%ebx
mov    %r12d,%ebp
xor    %r12d,%r12d
nop
movslq %ebx,%rbx
lea    (%rbx,%r13,1),%rdi
mov    %r15,%rsi
sub    %rbx,%rsi
mov    (%r14,%r12,4),%ecx
lea    0xe3e(%rip),%rdx
xor    %eax,%eax
call   1050 <snprintf@plt>
add    %eax,%ebx
add    $0x1,%r12
cmp    %r12,%rbp
jne    11b0 <printvar+0x60>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <prnlog@plt>

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s = \00"
@.str.1 = [5 x i8] c"%02x\00"
define void @printvar(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = shl nsw i32 %2, 1
%6 = add i32 %5, 40
%7 = add i32 %6, %4
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i32 @memset(i8* %9, i32 0, i64 %8)
%11 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %9, i64 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %27
13: ; preds = %3
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %25, %15 ]
%17 = phi i32 [ %11, %13 ], [ %24, %15 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %9, i64 %18
%20 = sub nsw i64 %8, %18
%21 = getelementptr inbounds i32, i32* %1, i64 %16
%22 = load i32, i32* %21
%23 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %19, i64 %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = add nsw i32 %23, %17
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, %14
br i1 %26, label %27, label %15
27: ; preds = %15, %3
%28 = tail call i32 @prnlog(i8* %9)
ret void
}