#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 *malloc(size_t);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
char *bin2hex(const unsigned char *p, size_t len) {
  char *s = malloc((len * 2) + 1);
  unsigned int i;
  if (!s)
    return NULL;
  for (i = 0; i < len; i++)
    sprintf(s + (i * 2), "%02x", (unsigned int)p[i]);
  return s;
}


x86 assembly (clang, -O0):
<bin2hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x1,%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    115d <bin2hex+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    11ad <bin2hex+0x8d>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11a5 <bin2hex+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
lea    0xe6e(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1164 <bin2hex+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin2hex>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    (%rsi,%rsi,1),%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     117b <bin2hex+0x5b>
test   %r15,%r15
je     117b <bin2hex+0x5b>
xor    %ebx,%ebx
lea    0xeae(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%rbx,%rbx,1),%edi
add    %r13,%rdi
movzbl (%r14,%rbx,1),%edx
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x1,%ebx
cmp    %r15,%rbx
jb     1160 <bin2hex+0x40>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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