#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__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
char to_hex_char(int);
void debug_hex_strcat(uint8_t *dst, uint8_t *hextodump, int size) {
  int i;
  int len = 0;
  char *logstr = NULL;
  logstr = (char *)calloc(1, (size * 3) + 2);
  if (logstr == NULL) {
    printf("Couldn't allocate memory to store temporary logging string!\n");
    return;
  }
  memset(logstr, 0x00, ((size * 3) + 2));
  if (hextodump == NULL) {
    free(logstr);
    return;
  }
  for (i = 0; i < size; i++) {
    logstr[len++] = to_hex_char(hextodump[i] >> 4);
    logstr[len++] = to_hex_char(hextodump[i]);
    logstr[len++] = ' ';
  }
  logstr[len] = 0;
  strcat((char *)dst, logstr);
  free(logstr);
}


x86 assembly (clang, -O0):
<debug_hex_strcat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
imul   $0x3,-0x14(%rbp),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b3 <debug_hex_strcat+0x53>
lea    0xe57(%rip),%rdi
call   1040 <printf@plt>
jmp    1288 <debug_hex_strcat+0x128>
mov    -0x28(%rbp),%rdi
imul   $0x3,-0x14(%rbp),%edx
add    $0x2,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11de <debug_hex_strcat+0x7e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    1288 <debug_hex_strcat+0x128>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1266 <debug_hex_strcat+0x106>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
sar    $0x4,%edi
call   1070 <to_hex_char@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1070 <to_hex_char@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movb   $0x20,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e5 <debug_hex_strcat+0x85>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug_hex_strcat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    (%r12,%r12,2),%ebp
add    $0x2,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11e1 <debug_hex_strcat+0x81>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     1207 <debug_hex_strcat+0xa7>
test   %r12d,%r12d
jle    11f5 <debug_hex_strcat+0x95>
mov    %r12d,%eax
lea    (%rax,%rax,2),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%rbx),%edi
sar    $0x4,%edi
call   1070 <to_hex_char@plt>
mov    %al,(%r15,%rbp,1)
mov    (%rbx),%edi
call   1070 <to_hex_char@plt>
mov    %al,0x1(%r15,%rbp,1)
movb   $0x20,0x2(%r15,%rbp,1)
add    $0x3,%rbp
add    $0x4,%rbx
cmp    %rbp,%r12
jne    11b0 <debug_hex_strcat+0x50>
mov    %ebp,%eax
jmp    11f7 <debug_hex_strcat+0x97>
lea    0xe18(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@plt>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [61 x i8] c"Couldn't allocate memory to store temporary logging string!\0A\00"
define void @debug_hex_strcat(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %8
store i8* null, i8** %9
%10 = load i32, i32* %6
%11 = mul nsw i32 %10, 3
%12 = add nsw i32 %11, 2
%13 = call i64 @calloc(i32 1, i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %78
19: ; preds = %3
%20 = load i8*, i8** %9
%21 = load i32, i32* %6
%22 = mul nsw i32 %21, 3
%23 = add nsw i32 %22, 2
%24 = call i32 @memset(i8* %20, i32 0, i32 %23)
%25 = load i32*, i32** %5
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = load i8*, i8** %9
%29 = call i32 @free(i8* %28)
br label %78
30: ; preds = %19
store i32 0, i32* %7
br label %31
31: ; preds = %64, %30
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %67
35: ; preds = %31
%36 = load i32*, i32** %5
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = ashr i32 %40, 4
%42 = call signext i8 @to_hex_char(i32 %41)
%43 = load i8*, i8** %9
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 %42, i8* %47
%48 = load i32*, i32** %5
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i32, i32* %51
%53 = call signext i8 @to_hex_char(i32 %52)
%54 = load i8*, i8** %9
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 %53, i8* %58
%59 = load i8*, i8** %9
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 32, i8* %63
br label %64
64: ; preds = %35
%65 = load i32, i32* %7
%66 = add nsw i32 %65, 1
store i32 %66, i32* %7
br label %31
67: ; preds = %31
%68 = load i8*, i8** %9
%69 = load i32, i32* %8
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
store i8 0, i8* %71
%72 = load i32*, i32** %4
%73 = bitcast i32* %72 to i8*
%74 = load i8*, i8** %9
%75 = call i32 @strcat(i8* %73, i8* %74)
%76 = load i8*, i8** %9
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %67, %27, %17
ret void
}

LLVM IR (-O2):
@str = [60 x i8] c"Couldn't allocate memory to store temporary logging string!\00"
define void @debug_hex_strcat(i32* %0, i32* %1, i32 %2) {
%4 = mul nsw i32 %2, 3
%5 = add nsw i32 %4, 2
%6 = tail call i64 @calloc(i32 1, i32 %5)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @str, i64 0, i64 0))
br label %45
11: ; preds = %3
%12 = tail call i32 @memset(i8* %7, i32 0, i32 %5)
%13 = icmp eq i32* %1, null
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %39
16: ; preds = %14
%17 = zext i32 %2 to i64
br label %20
18: ; preds = %11
%19 = tail call i32 @free(i8* %7)
br label %45
20: ; preds = %16, %20
%21 = phi i64 [ 0, %16 ], [ %35, %20 ]
%22 = phi i64 [ 0, %16 ], [ %33, %20 ]
%23 = getelementptr inbounds i32, i32* %1, i64 %21
%24 = load i32, i32* %23
%25 = ashr i32 %24, 4
%26 = tail call signext i8 @to_hex_char(i32 %25)
%27 = add nuw nsw i64 %22, 1
%28 = getelementptr inbounds i8, i8* %7, i64 %22
store i8 %26, i8* %28
%29 = load i32, i32* %23
%30 = tail call signext i8 @to_hex_char(i32 %29)
%31 = add nuw nsw i64 %22, 2
%32 = getelementptr inbounds i8, i8* %7, i64 %27
store i8 %30, i8* %32
%33 = add nuw nsw i64 %22, 3
%34 = getelementptr inbounds i8, i8* %7, i64 %31
store i8 32, i8* %34
%35 = add nuw nsw i64 %21, 1
%36 = icmp eq i64 %35, %17
br i1 %36, label %37, label %20
37: ; preds = %20
%38 = and i64 %33, 4294967295
br label %39
39: ; preds = %37, %14
%40 = phi i64 [ 0, %14 ], [ %38, %37 ]
%41 = getelementptr inbounds i8, i8* %7, i64 %40
store i8 0, i8* %41
%42 = bitcast i32* %0 to i8*
%43 = tail call i32 @strcat(i8* %42, i8* %7)
%44 = tail call i32 @free(i8* %7)
br label %45
45: ; preds = %39, %18, %9
ret void
}