#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 int);
void bin2hex(const unsigned char *source, size_t length,
             unsigned char *target) {
  unsigned int i;
  for (i = 0; i < length; i++) {
    sprintf((char *)(target + (i * 2)), "%02x", (unsigned int)source[i]);
  }
  target[length * 2] = 0;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    116c <bin2hex+0x5c>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xea7(%rip),%rsi
call   1030 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <bin2hex+0x1b>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shl    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r13
test   %rsi,%rsi
je     114b <bin2hex+0x3b>
mov    %rdi,%r15
xor    %ebx,%ebx
lea    0xed0(%rip),%r12
lea    (%rbx,%rbx,1),%edi
add    %r14,%rdi
movzbl (%r15,%rbx,1),%edx
mov    %r12,%rsi
call   1030 <sprintf@plt>
add    $0x1,%ebx
cmp    %r13,%rbx
jb     1130 <bin2hex+0x20>
movb   $0x0,(%r14,%r13,2)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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