#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(unsigned int);
void *nibble_to_hex(unsigned char);
char *to_hexstring(unsigned char *bytes, unsigned int len) {
  char *hexstr = malloc(len * 2 + 1);
  if (hexstr == NULL) {
    return NULL;
  }
  unsigned char *src = bytes;
  char *dest = hexstr;
  for (unsigned int i = 0; i < len; ++i) {
    unsigned char val = *src++;
    *dest++ = nibble_to_hex((val >> 4) & 0xF);
    *dest++ = nibble_to_hex(val & 0xF);
  }
  *dest = '\0';
  return hexstr;
}


x86 assembly (clang, -O0):
<to_hexstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
shl    $0x1,%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1159 <to_hexstring+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11f4 <to_hexstring+0xd4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11e5 <to_hexstring+0xc5>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%al
mov    %al,-0x35(%rbp)
movzbl -0x35(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <nibble_to_hex@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
movzbl -0x35(%rbp),%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <nibble_to_hex@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1170 <to_hexstring+0x50>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_hexstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
lea    (%r15,%r15,1),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     119b <to_hexstring+0x7b>
mov    %r12,%rax
test   %r15d,%r15d
je     1198 <to_hexstring+0x78>
mov    %r15d,%r15d
xor    %r13d,%r13d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r14,%rbx,1),%ebp
mov    %ebp,%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <nibble_to_hex@plt>
mov    %al,(%r12,%rbx,2)
and    $0xf,%ebp
mov    %ebp,%edi
call   1030 <nibble_to_hex@plt>
mov    %al,0x1(%r12,%rbx,2)
add    $0x1,%rbx
add    $0xfffffffffffffffe,%r13
cmp    %ebx,%r15d
jne    1160 <to_hexstring+0x40>
mov    %r12,%rax
sub    %r13,%rax
movb   $0x0,(%rax)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @to_hexstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = mul i32 %11, 2
%13 = add i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %2
store i8* null, i8** %3
br label %52
18: ; preds = %2
%19 = load i8*, i8** %4
store i8* %19, i8** %7
%20 = load i8*, i8** %6
store i8* %20, i8** %8
store i32 0, i32* %9
br label %21
21: ; preds = %46, %18
%22 = load i32, i32* %9
%23 = load i32, i32* %5
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %49
25: ; preds = %21
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %7
%28 = load i8, i8* %26
store i8 %28, i8* %10
%29 = load i8, i8* %10
%30 = zext i8 %29 to i32
%31 = ashr i32 %30, 4
%32 = and i32 %31, 15
%33 = trunc i32 %32 to i8
%34 = call i8* @nibble_to_hex(i8 zeroext %33)
%35 = ptrtoint i8* %34 to i8
%36 = load i8*, i8** %8
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %8
store i8 %35, i8* %36
%38 = load i8, i8* %10
%39 = zext i8 %38 to i32
%40 = and i32 %39, 15
%41 = trunc i32 %40 to i8
%42 = call i8* @nibble_to_hex(i8 zeroext %41)
%43 = ptrtoint i8* %42 to i8
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
store i8 %43, i8* %44
br label %46
46: ; preds = %25
%47 = load i32, i32* %9
%48 = add i32 %47, 1
store i32 %48, i32* %9
br label %21
49: ; preds = %21
%50 = load i8*, i8** %8
store i8 0, i8* %50
%51 = load i8*, i8** %6
store i8* %51, i8** %3
br label %52
52: ; preds = %49, %17
%53 = load i8*, i8** %3
ret i8* %53
}

LLVM IR (-O2):
define noalias i8* @to_hexstring(i8* nocapture %0, i32 %1) {
%3 = shl i32 %1, 1
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %29, label %7
7: ; preds = %2
%8 = icmp eq i32 %1, 0
br i1 %8, label %9, label %11
9: ; preds = %11, %7
%10 = phi i8* [ %5, %7 ], [ %26, %11 ]
store i8 0, i8* %10
br label %29
11: ; preds = %7, %11
%12 = phi i32 [ %27, %11 ], [ 0, %7 ]
%13 = phi i8* [ %26, %11 ], [ %5, %7 ]
%14 = phi i8* [ %15, %11 ], [ %0, %7 ]
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %14
%17 = lshr i8 %16, 4
%18 = tail call i8* @nibble_to_hex(i8 zeroext %17)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i8
%21 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %20, i8* %13
%22 = and i8 %16, 15
%23 = tail call i8* @nibble_to_hex(i8 zeroext %22)
%24 = ptrtoint i8* %23 to i64
%25 = trunc i64 %24 to i8
%26 = getelementptr inbounds i8, i8* %13, i64 2
store i8 %25, i8* %21
%27 = add nuw i32 %12, 1
%28 = icmp eq i32 %27, %1
br i1 %28, label %9, label %11
29: ; preds = %2, %9
ret i8* %5
}