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

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
int _parse_data(uint8_t *data, size_t len, char **out) {
  uint8_t *p;
  char *buffer, *b;
  if (!len)
    return 2;
  buffer = malloc(3 * sizeof(char) * len);
  if (!buffer)
    return 1;
  p = data;
  b = buffer;
  while (len > 0) {
    sprintf(b, "%02hhx", *p);
    b += 2;
    p++;
    len--;
    if (len > 0)
      *b++ = ':';
  }
  *out = buffer;
  return 0;
}


x86 assembly (clang, -O0):
<_parse_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114b <_parse_data+0x2b>
movl   $0x2,-0x4(%rbp)
jmp    11fb <_parse_data+0xdb>
imul   $0x3,-0x18(%rbp),%rax
mov    %eax,%edi
call   1030 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1172 <_parse_data+0x52>
movl   $0x1,-0x4(%rbp)
jmp    11fb <_parse_data+0xdb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11e9 <_parse_data+0xc9>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xe62(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x38(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11e4 <_parse_data+0xc4>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x3a,(%rax)
jmp    1182 <_parse_data+0x62>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_parse_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     11ab <_parse_data+0x8b>
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%r15
lea    (%rsi,%rsi,2),%edi
call   1030 <malloc@plt>
test   %rax,%rax
je     11b2 <_parse_data+0x92>
mov    %rax,%r12
mov    %rbx,(%rsp)
mov    (%r15),%edx
lea    0xea9(%rip),%rsi
mov    %rax,%rdi
call   1040 <sprintf@plt>
cmp    $0x1,%r13
je     11a0 <_parse_data+0x80>
mov    %r12,%rbx
add    $0x3,%rbx
mov    $0x1,%r14d
lea    0xe87(%rip),%rbp
nopl   0x0(%rax)
movb   $0x3a,-0x1(%rbx)
mov    (%r15,%r14,4),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <sprintf@plt>
add    $0x1,%r14
add    $0x3,%rbx
cmp    %r14,%r13
jne    1180 <_parse_data+0x60>
mov    (%rsp),%rax
mov    %r12,(%rax)
xor    %eax,%eax
jmp    11b7 <_parse_data+0x97>
mov    $0x2,%eax
jmp    11b7 <_parse_data+0x97>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%02hhx\00"
define i32 @_parse_data(i32* %0, i64 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
store i32* %0, i32** %5
store i64 %1, i64* %6
store i8** %2, i8*** %7
%11 = load i64, i64* %6
%12 = icmp ne i64 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %3
store i32 2, i32* %4
br label %48
14: ; preds = %3
%15 = load i64, i64* %6
%16 = mul i64 3, %15
%17 = trunc i64 %16 to i32
%18 = call i8* @malloc(i32 %17)
store i8* %18, i8** %9
%19 = load i8*, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %14
store i32 1, i32* %4
br label %48
22: ; preds = %14
%23 = load i32*, i32** %5
store i32* %23, i32** %8
%24 = load i8*, i8** %9
store i8* %24, i8** %10
br label %25
25: ; preds = %44, %22
%26 = load i64, i64* %6
%27 = icmp ugt i64 %26, 0
br i1 %27, label %28, label %45
28: ; preds = %25
%29 = load i8*, i8** %10
%30 = load i32*, i32** %8
%31 = load i32, i32* %30
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i64 2
store i8* %34, i8** %10
%35 = load i32*, i32** %8
%36 = getelementptr inbounds i32, i32* %35, i32 1
store i32* %36, i32** %8
%37 = load i64, i64* %6
%38 = add i64 %37, -1
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = icmp ugt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %10
store i8 58, i8* %42
br label %44
44: ; preds = %41, %28
br label %25
45: ; preds = %25
%46 = load i8*, i8** %9
%47 = load i8**, i8*** %7
store i8* %46, i8** %47
store i32 0, i32* %4
br label %48
48: ; preds = %45, %21, %13
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"%02hhx\00"
define i32 @_parse_data(i32* nocapture %0, i64 %1, i8** nocapture writeonly %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %27, label %5
5: ; preds = %3
%6 = trunc i64 %1 to i32
%7 = mul i32 %6, 3
%8 = tail call i8* @malloc(i32 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %27, label %10
10: ; preds = %5
%11 = load i32, i32* %0
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %8, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = add i64 %1, -1
%14 = icmp eq i64 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %10, %15
%16 = phi i64 [ %24, %15 ], [ %13, %10 ]
%17 = phi i32* [ %19, %15 ], [ %0, %10 ]
%18 = phi i8* [ %21, %15 ], [ %8, %10 ]
%19 = getelementptr inbounds i32, i32* %17, i64 1
%20 = getelementptr inbounds i8, i8* %18, i64 2
%21 = getelementptr inbounds i8, i8* %18, i64 3
store i8 58, i8* %20
%22 = load i32, i32* %19
%23 = tail call i32 @sprintf(i8* dereferenceable(1) %21, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = add i64 %16, -1
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %15
26: ; preds = %15, %10
store i8* %8, i8** %2
br label %27
27: ; preds = %5, %3, %26
%28 = phi i32 [ 0, %26 ], [ 2, %3 ], [ 1, %5 ]
ret i32 %28
}