#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(int);
int /*<<< orphan*/ sprintf(char *, char *, char);
char *strToHexStr(char *str, int len) {
  char *hex = malloc(len * 2 + 1);
  char *rtr = hex;
  while (len--) {
    sprintf(hex, "%.2X", (char)*str++);
    hex += 2;
  }
  *hex = '\0';
  return rtr;
}


x86 assembly (clang, -O0):
<strToHexStr>:
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    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1190 <strToHexStr+0x70>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
lea    0xe89(%rip),%rsi
movsbl (%rax),%edx
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    1149 <strToHexStr+0x29>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strToHexStr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %rdi,%r15
lea    (%r12,%r12,1),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rbx
test   %r12d,%r12d
je     117d <strToHexStr+0x5d>
mov    %r12d,%r13d
xor    %ebp,%ebp
lea    0xeac(%rip),%r12
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %ebp,%r13d
jne    1160 <strToHexStr+0x40>
movb   $0x0,(%rbx)
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* @strToHexStr(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
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 i8* @malloc(i32 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
store i8* %11, i8** %6
br label %12
12: ; preds = %16, %2
%13 = load i32, i32* %4
%14 = add nsw i32 %13, -1
store i32 %14, i32* %4
%15 = icmp ne i32 %13, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
%20 = load i8, i8* %18
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %20)
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 2
store i8* %23, i8** %5
br label %12
24: ; preds = %12
%25 = load i8*, i8** %5
store i8 0, i8* %25
%26 = load i8*, i8** %6
ret i8* %26
}

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