#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 *, unsigned char);
int /*<<< orphan*/ strcat(char *, char *);
char *hexToString(unsigned char *hex, int nbOfBytes) {
  int i;
  char *string = malloc((nbOfBytes * 2 + 1) * sizeof(char));
  string[0] = '\0';
  char buffer[3];
  for (i = 0; i < nbOfBytes; i++) {
    sprintf(buffer, "%02x", hex[i]);
    strcat(string, buffer);
  }
  return string;
}


x86 assembly (clang, -O0):
<hexToString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11aa <hexToString+0x7a>
lea    -0x1b(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
lea    0xe7a(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x1b(%rbp),%rsi
call   1040 <strcat@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1167 <hexToString+0x37>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexToString>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
lea    (%rbx,%rbx,1),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
test   %ebx,%ebx
jle    1194 <hexToString+0x64>
mov    %ebx,%r13d
xor    %ebp,%ebp
lea    0xe9f(%rip),%r15
lea    0x5(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <sprintf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1170 <hexToString+0x40>
mov    %r12,%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* @hexToString(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [3 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = mul nsw i32 %8, 2
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 1
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 0
store i8 0, i8* %16
store i32 0, i32* %5
br label %17
17: ; preds = %32, %2
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %35
21: ; preds = %17
%22 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%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 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%31 = call i32 @strcat(i8* %29, i8* %30)
br label %32
32: ; preds = %21
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %17
35: ; preds = %17
%36 = load i8*, i8** %6
ret i8* %36
}

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