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

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int hex_from_bin(int dst_bytes, char *dst, int src_bytes, unsigned char *src) {
  if (dst_bytes < src_bytes * 2 + 1)
    return -1;
  for (; src_bytes; --src_bytes, ++src, dst += 2)
    sprintf(dst, "%02x", (unsigned char)*src);
  *dst = 0;
  return 0;
}


x86 assembly (clang, -O0):
<hex_from_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1146 <hex_from_bin+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <hex_from_bin+0x90>
jmp    114b <hex_from_bin+0x3b>
cmpl   $0x0,-0x14(%rbp)
je     1192 <hex_from_bin+0x82>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
lea    0xe9c(%rip),%rsi
movzbl (%rax),%edx
call   1030 <sprintf@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    114b <hex_from_bin+0x3b>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_from_bin>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
lea    (%rdx,%rdx,1),%ecx
add    $0x1,%ecx
mov    $0xffffffff,%eax
cmp    %edi,%ecx
jg     1164 <hex_from_bin+0x54>
mov    %rsi,%r13
test   %edx,%edx
je     115d <hex_from_bin+0x4d>
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xec2(%rip),%r15
xchg   %ax,%ax
movzbl (%r14,%rbx,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1030 <sprintf@plt>
add    $0x2,%r13
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1140 <hex_from_bin+0x30>
movb   $0x0,0x0(%r13)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i32 @hex_from_bin(i32 %0, i8* nocapture writeonly %1, i32 %2, i8* nocapture %3) {
%5 = shl nsw i32 %2, 1
%6 = or i32 %5, 1
%7 = icmp sgt i32 %6, %0
br i1 %7, label %22, label %8
8: ; preds = %4
%9 = icmp eq i32 %2, 0
br i1 %9, label %20, label %10
10: ; preds = %8, %10
%11 = phi i8* [ %17, %10 ], [ %3, %8 ]
%12 = phi i32 [ %16, %10 ], [ %2, %8 ]
%13 = phi i8* [ %18, %10 ], [ %1, %8 ]
%14 = load i8, i8* %11
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %14)
%16 = add nsw i32 %12, -1
%17 = getelementptr inbounds i8, i8* %11, i64 1
%18 = getelementptr inbounds i8, i8* %13, i64 2
%19 = icmp eq i32 %16, 0
br i1 %19, label %20, label %10
20: ; preds = %10, %8
%21 = phi i8* [ %1, %8 ], [ %18, %10 ]
store i8 0, i8* %21
br label %22
22: ; preds = %4, %20
%23 = phi i32 [ 0, %20 ], [ -1, %4 ]
ret i32 %23
}