#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 */
char *calloc(int, int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *fcla_bin_to_hex(unsigned char *data, size_t len) {
  char *r = calloc(2 * len + 1, sizeof(char));
  for (size_t i = 0; i < len; ++i)
    sprintf(r + 2 * i, "%02x", data[i]);
  return r;
}


x86 assembly (clang, -O0):
<fcla_bin_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    119a <fcla_bin_to_hex+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
lea    0xe80(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1154 <fcla_bin_to_hex+0x34>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcla_bin_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r13
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%r15
test   %r13,%r13
je     117d <fcla_bin_to_hex+0x5d>
xor    %ebp,%ebp
lea    0xeae(%rip),%r12
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1160 <fcla_bin_to_hex+0x40>
mov    %r15,%rax
add    $0x8,%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* @fcla_bin_to_hex(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = mul i64 2, %7
%9 = add i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i8* @calloc(i32 %10, i32 1)
store i8* %11, i8** %5
store i64 0, i64* %6
br label %12
12: ; preds = %26, %2
%13 = load i64, i64* %6
%14 = load i64, i64* %4
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = mul i64 2, %18
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8*, i8** %3
%22 = load i64, i64* %6
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i8, i8* %23
%25 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %24)
br label %26
26: ; preds = %16
%27 = load i64, i64* %6
%28 = add i64 %27, 1
store i64 %28, i64* %6
br label %12
29: ; preds = %12
%30 = load i8*, i8** %5
ret i8* %30
}

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