#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__ calloc(int, size_t);
size_t data2string_length(void const *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
char *data2string(const void *data, size_t size) {
  char *string;
  size_t length;
  char *p;
  size_t i;
  if (data == NULL || size == 0)
    return NULL;
  length = data2string_length(data, size);
  if (length == 0)
    return NULL;
  string = (char *)calloc(1, length);
  p = string;
  for (i = 0; i < size; i++) {
    sprintf(p, "%02x", *((unsigned char *)data + i));
    p += 2 * sizeof(char);
  }
  return string;
}


x86 assembly (clang, -O0):
<data2string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <data2string+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <data2string+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11fd <data2string+0xcd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <data2string_length@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118c <data2string+0x5c>
movq   $0x0,-0x8(%rbp)
jmp    11fd <data2string+0xcd>
mov    -0x28(%rbp),%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f5 <data2string+0xc5>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
lea    0xe31(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1050 <sprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11ae <data2string+0x7e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<data2string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     11a3 <data2string+0x73>
mov    %rsi,%r14
test   %rsi,%rsi
je     11a3 <data2string+0x73>
mov    %rdi,%r13
mov    %r14,%rsi
call   1030 <data2string_length@plt>
test   %rax,%rax
je     11a0 <data2string+0x70>
mov    $0x1,%edi
mov    %rax,%rsi
call   1040 <calloc@plt>
mov    %rax,%r15
xor    %ebp,%ebp
lea    0xe8c(%rip),%r12
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
movzbl 0x0(%r13,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <sprintf@plt>
add    $0x2,%rbx
add    $0x1,%rbp
cmp    %rbp,%r14
jne    1180 <data2string+0x50>
jmp    11a3 <data2string+0x73>
xor    %r15d,%r15d
mov    %r15,%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* @data2string(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12, %2
store i8* null, i8** %3
br label %46
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i64, i64* %5
%19 = call i64 @data2string_length(i8* %17, i64 %18)
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i8* null, i8** %3
br label %46
23: ; preds = %16
%24 = load i64, i64* %7
%25 = call i64 @calloc(i32 1, i64 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %6
%27 = load i8*, i8** %6
store i8* %27, i8** %8
store i64 0, i64* %9
br label %28
28: ; preds = %41, %23
%29 = load i64, i64* %9
%30 = load i64, i64* %5
%31 = icmp ult i64 %29, %30
br i1 %31, label %32, label %44
32: ; preds = %28
%33 = load i8*, i8** %8
%34 = load i8*, i8** %4
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = load i8, i8* %36
%38 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 zeroext %37)
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i64 2
store i8* %40, i8** %8
br label %41
41: ; preds = %32
%42 = load i64, i64* %9
%43 = add i64 %42, 1
store i64 %43, i64* %9
br label %28
44: ; preds = %28
%45 = load i8*, i8** %6
store i8* %45, i8** %3
br label %46
46: ; preds = %44, %22, %15
%47 = load i8*, i8** %3
ret i8* %47
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02x\00"
define i8* @data2string(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = tail call i64 @data2string_length(i8* %0, i64 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = tail call i64 @calloc(i32 1, i64 %7)
%11 = inttoptr i64 %10 to i8*
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ %19, %12 ], [ 0, %9 ]
%14 = phi i8* [ %18, %12 ], [ %11, %9 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %13
%16 = load i8, i8* %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), i8 zeroext %16)
%18 = getelementptr inbounds i8, i8* %14, i64 2
%19 = add nuw i64 %13, 1
%20 = icmp eq i64 %19, %1
br i1 %20, label %21, label %12
21: ; preds = %12, %6, %2
%22 = phi i8* [ null, %2 ], [ null, %6 ], [ %11, %12 ]
ret i8* %22
}