#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 */
typedef int uint8_t;

/* Variables and functions */
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int const);
const char *_hex2str(const uint8_t *data, size_t len) {
  static char *str = NULL;
  size_t i;
  str = (char *)realloc(str, 2 * len + 1);
  *str = 0;
  for (i = 0; i < len; i++) {
    sprintf(str + 2 * i, "%02X", data[i]);
  }
  return str;
}


x86 assembly (clang, -O0):
<_hex2str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2f01(%rip),%rdi
mov    -0x10(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,0x2ee7(%rip)
mov    0x2ee0(%rip),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ab <_hex2str+0x8b>
mov    0x2ec0(%rip),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe6b(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1163 <_hex2str+0x43>
mov    0x2e86(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_hex2str>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2f03(%rip),%rdi
add    %esi,%esi
add    $0x1,%esi
call   1030 <realloc@plt>
mov    %rax,0x2ef2(%rip)
movb   $0x0,(%rax)
test   %r12,%r12
je     1180 <_hex2str+0x60>
xor    %ebx,%ebx
lea    0xea9(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    0x2ed1(%rip),%rdi
add    %rbx,%rdi
mov    (%r14,%rbx,2),%edx
mov    %r15,%rsi
call   1040 <sprintf@plt>
add    $0x2,%rbx
add    $0xffffffffffffffff,%r12
jne    1160 <_hex2str+0x40>
mov    0x2eb1(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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