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

/* Variables and functions */
int /*<<< orphan*/ snprintf(char *, int, char *, char *, unsigned char);
int /*<<< orphan*/ strncat(char *, char *, int);
void enhex(unsigned char *hexed, int hex_size, unsigned char *buf,
           int buf_len) {
  int i;
  for (i = 0; i < buf_len; ++i) {
    char tmp[4];
    char *space = (i == 0) ? "" : " ";
    snprintf(tmp, sizeof(tmp), "%s%02x", space, buf[i]);
    strncat((char *)hexed, tmp, hex_size);
  }
}


x86 assembly (clang, -O0):
<enhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11a9 <enhex+0x89>
mov    -0x20(%rbp),%edx
lea    0xead(%rip),%rax
lea    0xea7(%rip),%rcx
cmp    $0x0,%edx
cmove  %rcx,%rax
mov    %rax,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%r8
mov    $0x4,%esi
lea    0xe81(%rip),%rdx
movzbl (%rax,%r8,1),%r8d
call   1030 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x24(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <strncat@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    113d <enhex+0x1d>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enhex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    1195 <enhex+0x75>
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%r13
mov    %ecx,%ebx
xor    %r15d,%r15d
lea    0x4(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %r15,%r15
lea    0xea6(%rip),%rcx
lea    0xea0(%rip),%rax
cmove  %rax,%rcx
movzbl (%r14,%r15,1),%r8d
mov    %rbp,%rdi
mov    $0x4,%esi
lea    0xe89(%rip),%rdx
call   1030 <snprintf@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1040 <strncat@plt>
add    $0x1,%r15
cmp    %r15,%rbx
jne    1150 <enhex+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c" \00"
@.str.2 = [7 x i8] c"%s%02x\00"
define void @enhex(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4 x i8]
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %12
12: ; preds = %33, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load i32, i32* %9
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
store i8* %20, i8** %11
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%22 = load i8*, i8** %11
%23 = load i8*, i8** %7
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @snprintf(i8* %21, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8 zeroext %27)
%29 = load i8*, i8** %5
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%31 = load i32, i32* %6
%32 = call i32 @strncat(i8* %29, i8* %30, i32 %31)
br label %33
33: ; preds = %16
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %12
36: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c" \00"
@.str.2 = [7 x i8] c"%s%02x\00"
define void @enhex(i8* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca [4 x i8]
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %20
7: ; preds = %4
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%9 = zext i32 %3 to i64
br label %10
10: ; preds = %7, %10
%11 = phi i64 [ 0, %7 ], [ %18, %10 ]
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%14 = getelementptr inbounds i8, i8* %2, i64 %11
%15 = load i8, i8* %14
%16 = call i32 @snprintf(i8* %8, i32 4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %13, i8 zeroext %15)
%17 = call i32 @strncat(i8* %0, i8* %8, i32 %1)
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %9
br i1 %19, label %20, label %10
20: ; preds = %10, %4
ret void
}