#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 /*<<< orphan*/ uint8_t;
struct mem_pool {
  int dummy;
};

/* Variables and functions */
char *mem_pool_alloc(struct mem_pool *, int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/ const, char);
char *mem_pool_hex(struct mem_pool *pool, const uint8_t *data, size_t size) {
  char *result = mem_pool_alloc(pool, 3 * size);
  for (size_t i = 0; i < size; i++)
    sprintf(result + 3 * i, "%.2hhX%c", data[i], ((i + 1) % 4) ? ':' : ' ');
  result[size ? 3 * size - 1 : 0] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<mem_pool_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
imul   $0x3,-0x18(%rbp),%rax
mov    %eax,%esi
call   1030 <mem_pool_alloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11b2 <mem_pool_hex+0x92>
mov    -0x20(%rbp),%rdi
imul   $0x3,-0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x28(%rbp),%rsi
add    $0x1,%rsi
and    $0x3,%rsi
mov    $0x20,%eax
mov    $0x3a,%ecx
cmp    $0x0,%rsi
cmovne %ecx,%eax
lea    0xe67(%rip),%rsi
movsbl %al,%ecx
call   1040 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1150 <mem_pool_hex+0x30>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d7 <mem_pool_hex+0xb7>
imul   $0x3,-0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11e2 <mem_pool_hex+0xc2>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11e2 <mem_pool_hex+0xc2>
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_pool_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
lea    (%rdx,%rdx,2),%rsi
mov    %rsi,0x10(%rsp)
call   1030 <mem_pool_alloc@plt>
mov    %rax,(%rsp)
mov    %rbp,0x8(%rsp)
test   %rbp,%rbp
je     11a3 <mem_pool_hex+0x83>
mov    0x8(%rsp),%r14
neg    %r14
mov    $0x1,%r12d
mov    $0x20,%r15d
lea    0xe95(%rip),%r13
mov    (%rsp),%rbp
nop
mov    -0x4(%rbx,%r12,4),%edx
test   $0x3,%r12b
mov    $0x3a,%ecx
cmove  %r15d,%ecx
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <sprintf@plt>
lea    (%r14,%r12,1),%rax
add    $0x1,%rax
add    $0x1,%r12
add    $0x3,%rbp
cmp    $0x1,%rax
jne    1170 <mem_pool_hex+0x50>
mov    0x10(%rsp),%rcx
add    $0xffffffffffffffff,%rcx
mov    0x8(%rsp),%rax
test   %rax,%rax
cmove  %rax,%rcx
mov    (%rsp),%rax
movb   $0x0,(%rax,%rcx,1)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mem_pool = type { i32 }
@.str = [9 x i8] c"%.2hhX%c\00"
define i8* @mem_pool_hex(%struct.mem_pool* %0, i32* %1, i64 %2) {
%4 = alloca %struct.mem_pool*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store %struct.mem_pool* %0, %struct.mem_pool** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%9 = load %struct.mem_pool*, %struct.mem_pool** %4
%10 = load i64, i64* %6
%11 = mul i64 3, %10
%12 = trunc i64 %11 to i32
%13 = call i8* @mem_pool_alloc(%struct.mem_pool* %9, i32 %12)
store i8* %13, i8** %7
store i64 0, i64* %8
br label %14
14: ; preds = %35, %3
%15 = load i64, i64* %8
%16 = load i64, i64* %6
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = mul i64 3, %20
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i32*, i32** %5
%24 = load i64, i64* %8
%25 = getelementptr inbounds i32, i32* %23, i64 %24
%26 = load i32, i32* %25
%27 = load i64, i64* %8
%28 = add i64 %27, 1
%29 = urem i64 %28, 4
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 58, i32 32
%33 = trunc i32 %32 to i8
%34 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26, i8 signext %33)
br label %35
35: ; preds = %18
%36 = load i64, i64* %8
%37 = add i64 %36, 1
store i64 %37, i64* %8
br label %14
38: ; preds = %14
%39 = load i8*, i8** %7
%40 = load i64, i64* %6
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i64, i64* %6
%44 = mul i64 3, %43
%45 = sub i64 %44, 1
br label %47
46: ; preds = %38
br label %47
47: ; preds = %46, %42
%48 = phi i64 [ %45, %42 ], [ 0, %46 ]
%49 = getelementptr inbounds i8, i8* %39, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %7
ret i8* %50
}

LLVM IR (-O2):
%struct.mem_pool = type { i32 }
@.str = [9 x i8] c"%.2hhX%c\00"
define i8* @mem_pool_hex(%struct.mem_pool* %0, i32* nocapture %1, i64 %2) {
%4 = mul i64 %2, 3
%5 = trunc i64 %4 to i32
%6 = tail call i8* @mem_pool_alloc(%struct.mem_pool* %0, i32 %5)
%7 = icmp eq i64 %2, 0
br i1 %7, label %8, label %12
8: ; preds = %12, %3
%9 = add i64 %4, -1
%10 = select i1 %7, i64 0, i64 %9
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 0, i8* %11
ret i8* %6
12: ; preds = %3, %12
%13 = phi i64 [ %18, %12 ], [ 0, %3 ]
%14 = mul i64 %13, 3
%15 = getelementptr inbounds i8, i8* %6, i64 %14
%16 = getelementptr inbounds i32, i32* %1, i64 %13
%17 = load i32, i32* %16
%18 = add nuw i64 %13, 1
%19 = and i64 %18, 3
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, i8 32, i8 58
%22 = tail call i32 @sprintf(i8* dereferenceable(1) %15, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17, i8 signext %21)
%23 = icmp eq i64 %18, %2
br i1 %23, label %8, label %12
}