#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char const);
char *bin2hex(const unsigned char *raw, int len) {
  char *hex = (char *)malloc(len * 2 + 1);
  hex[len * 2] = 0;
  for (int i = 0; i < len; i++) {
    sprintf(hex + i * 2, "%02hhX", raw[i]);
  }
  return hex;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119a <bin2hex+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe7d(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1159 <bin2hex+0x39>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r12
movslq %esi,%rbp
mov    %rbp,%rdi
add    %rbp,%rdi
or     $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax,%rbp,2)
test   %ebp,%ebp
jle    117d <bin2hex+0x5d>
mov    %ebx,%r13d
xor    %ebp,%ebp
lea    0xea8(%rip),%r15
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1160 <bin2hex+0x40>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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