#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
ssize_t bin2hex(const void *bin, char *hex, size_t n) {
  for (size_t i = 0; i < n; i++)
    sprintf(&hex[i * 2], "%.2x", ((unsigned char *)bin)[i]);
  return n * 2;
}


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)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1172 <bin2hex+0x62>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
lea    0xea8(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1030 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    112c <bin2hex+0x1c>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x20,%rsp
pop    %rbp
ret

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

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

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