#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 __u8;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, int const);
char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) {
  char *ptr = buf;
  int i;
  for (i = 0; i < len; i++) {
    if (blen < 3)
      break;
    sprintf(ptr, "%02x", str[i]);
    ptr += 2;
    blen -= 2;
    if (i != len - 1 && blen > 1) {
      *ptr++ = ':';
      blen--;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<hexstring_n2a>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c9 <hexstring_n2a+0xb9>
cmpl   $0x3,-0x1c(%rbp)
jge    1150 <hexstring_n2a+0x40>
jmp    11c9 <hexstring_n2a+0xb9>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe9a(%rip),%rsi
call   1030 <sprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     11b6 <hexstring_n2a+0xa6>
cmpl   $0x1,-0x1c(%rbp)
jle    11b6 <hexstring_n2a+0xa6>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movb   $0x3a,(%rax)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bb <hexstring_n2a+0xab>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1135 <hexstring_n2a+0x25>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexstring_n2a>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,0x8(%rsp)
mov    %rdi,0x10(%rsp)
test   %esi,%esi
jle    11a7 <hexstring_n2a+0x97>
mov    %ecx,%r13d
cmp    $0x3,%ecx
jl     11a7 <hexstring_n2a+0x97>
lea    -0x1(%rsi),%ebp
mov    %esi,%r14d
neg    %rbp
mov    $0x1,%r12d
mov    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
lea    (%r12,%rbp,1),%r15
mov    0x10(%rsp),%rax
mov    -0x4(%rax,%r12,4),%edx
mov    %rbx,%rdi
lea    0xe98(%rip),%rsi
call   1030 <sprintf@plt>
lea    0x2(%rbx),%rcx
lea    -0x2(%r13),%eax
cmp    $0x1,%r15
je     1193 <hexstring_n2a+0x83>
cmp    $0x4,%r13d
jb     1193 <hexstring_n2a+0x83>
movb   $0x3a,0x2(%rbx)
add    $0x3,%rbx
add    $0xfffffffd,%r13d
mov    %r13d,%eax
mov    %rbx,%rcx
cmp    %r14,%r12
jae    11a7 <hexstring_n2a+0x97>
add    $0x1,%r12
mov    %rcx,%rbx
mov    %eax,%r13d
cmp    $0x2,%eax
jg     1150 <hexstring_n2a+0x40>
mov    0x8(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @hexstring_n2a(i32* nocapture %0, i32 %1, i8* returned writeonly %2, i32 %3) {
%5 = icmp slt i32 %1, 1
%6 = icmp slt i32 %3, 3
%7 = or i1 %5, %6
br i1 %7, label %34, label %8
8: ; preds = %4
%9 = add nsw i32 %1, -1
%10 = zext i32 %9 to i64
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %8, %27
%13 = phi i64 [ 0, %8 ], [ %30, %27 ]
%14 = phi i8* [ %2, %8 ], [ %29, %27 ]
%15 = phi i32 [ %3, %8 ], [ %28, %27 ]
%16 = getelementptr inbounds i32, i32* %0, i64 %13
%17 = load i32, i32* %16
%18 = tail call i32 @sprintf(i8* dereferenceable(1) %14, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds i8, i8* %14, i64 2
%20 = add nsw i32 %15, -2
%21 = icmp ne i64 %13, %10
%22 = icmp ugt i32 %15, 3
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %27
24: ; preds = %12
%25 = getelementptr inbounds i8, i8* %14, i64 3
store i8 58, i8* %19
%26 = add nsw i32 %15, -3
br label %27
27: ; preds = %12, %24
%28 = phi i32 [ %26, %24 ], [ %20, %12 ]
%29 = phi i8* [ %25, %24 ], [ %19, %12 ]
%30 = add nuw nsw i64 %13, 1
%31 = icmp uge i64 %30, %11
%32 = icmp slt i32 %28, 3
%33 = select i1 %31, i1 true, i1 %32
br i1 %33, label %34, label %12
34: ; preds = %27, %4
ret i8* %2
}