#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(unsigned char *, char *, unsigned char);
char *byte_to_hex(unsigned char *bytes, int len) {
  unsigned char *res = (char *)malloc(sizeof(char) * len * 2 + 1);
  unsigned char *ptr = res;
  int i = 0;
  for (i = 0; i < len; i++) {
    ptr += sprintf(ptr, "%.2X", bytes[i]);
  }
  res[len * 2] = '\0';
  return res;
}


x86 assembly (clang, -O0):
<byte_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a6 <byte_to_hex+0x86>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
lea    0xe81(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <sprintf@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1160 <byte_to_hex+0x40>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<byte_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r12
lea    (%rbx,%rbx,1),%eax
mov    %eax,0x4(%rsp)
lea    (%rbx,%rbx,1),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
test   %ebx,%ebx
jle    117e <byte_to_hex+0x5e>
mov    %ebx,%ebp
xor    %r13d,%r13d
lea    0xeab(%rip),%r15
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
movzbl (%r12,%r13,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0x1,%r13
cmp    %r13,%rbp
jne    1160 <byte_to_hex+0x40>
movslq 0x4(%rsp),%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%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"%.2X\00"
define i8* @byte_to_hex(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = sext i32 %8 to i64
%10 = mul i64 1, %9
%11 = mul i64 %10, 2
%12 = add i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
%16 = load i8*, i8** %5
store i8* %16, i8** %6
store i32 0, i32* %7
store i32 0, i32* %7
br label %17
17: ; preds = %32, %2
%18 = load i32, i32* %7
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = load i8*, i8** %3
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %27)
%29 = load i8*, i8** %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %6
br label %32
32: ; preds = %21
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %17
35: ; preds = %17
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = mul nsw i32 %37, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %5
ret i8* %41
}

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