#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 */
char nibble_to_hex(int);
void to_hex(uint8_t *input, size_t len, char *output) {
  for (int i = 0; i < len; ++i) {
    uint8_t ch = input[i];
    uint8_t hi = (ch & 0xF0) >> 4;
    uint8_t lo = (ch & 0x0F);
    output[2 * i] = nibble_to_hex(hi);
    output[2 * i + 1] = nibble_to_hex(lo);
  }
  output[2 * len] = '\0';
}


x86 assembly (clang, -O0):
<to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a3 <to_hex+0x93>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xf0,%eax
sar    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <nibble_to_hex@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%edi
call   1030 <nibble_to_hex@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <to_hex+0x1b>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
shl    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
test   %rsi,%rsi
je     115d <to_hex+0x4d>
mov    %rdi,%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%ebp
mov    %ebp,%edi
shr    $0x4,%edi
and    $0xf,%edi
and    $0xf,%ebp
call   1030 <nibble_to_hex@plt>
mov    %al,(%r12,%rbx,2)
mov    %ebp,%edi
call   1030 <nibble_to_hex@plt>
mov    %al,0x1(%r12,%rbx,2)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1130 <to_hex+0x20>
movb   $0x0,(%r12,%r14,2)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @to_hex(i32* nocapture %0, i64 %1, i8* nocapture writeonly %2) {
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %8
5: ; preds = %8, %3
%6 = shl i64 %1, 1
%7 = getelementptr inbounds i8, i8* %2, i64 %6
store i8 0, i8* %7
ret void
8: ; preds = %3, %8
%9 = phi i64 [ %21, %8 ], [ 0, %3 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = lshr i32 %11, 4
%13 = and i32 %12, 15
%14 = and i32 %11, 15
%15 = tail call signext i8 @nibble_to_hex(i32 %13)
%16 = shl nuw nsw i64 %9, 1
%17 = getelementptr inbounds i8, i8* %2, i64 %16
store i8 %15, i8* %17
%18 = tail call signext i8 @nibble_to_hex(i32 %14)
%19 = or i64 %16, 1
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 %18, i8* %20
%21 = add nuw i64 %9, 1
%22 = icmp eq i64 %21, %1
br i1 %22, label %5, label %8
}