#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 */
void tohex(char *const hex, uint8_t const *const bin, size_t const binlen) {
  char const *const map = "0123456789abcdef";
  for (size_t i = 0; i < binlen; i++) {
    hex[i * 2 + 0] = map[0xf & (bin[i] >> 4)];
    hex[i * 2 + 1] = map[0xf & (bin[i] >> 0)];
  }
}


x86 assembly (clang, -O0):
<tohex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xee9(%rip),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119d <tohex+0x9d>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x4,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xeb4(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x1,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x0,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe87(%rip),%rax
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
shl    $0x1,%rcx
mov    %dl,0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1123 <tohex+0x23>
pop    %rbp
ret

x86 assembly (clang, -O2):
<tohex>:
test   %rdx,%rdx
je     1139 <tohex+0x39>
xor    %eax,%eax
lea    0xef2(%rip),%r8
xchg   %ax,%ax
mov    (%rsi,%rax,4),%ecx
shr    $0x4,%ecx
and    $0xf,%ecx
movzbl (%rcx,%r8,1),%ecx
mov    %cl,(%rdi,%rax,2)
mov    (%rsi,%rax,4),%ecx
and    $0xf,%ecx
movzbl (%rcx,%r8,1),%ecx
mov    %cl,0x1(%rdi,%rax,2)
add    $0x1,%rax
cmp    %rax,%rdx
jne    1110 <tohex+0x10>
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @tohex(i8* %0, i32* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %7
store i64 0, i64* %8
br label %9
9: ; preds = %42, %3
%10 = load i64, i64* %8
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %45
13: ; preds = %9
%14 = load i32*, i32** %5
%15 = load i64, i64* %8
%16 = getelementptr inbounds i32, i32* %14, i64 %15
%17 = load i32, i32* %16
%18 = ashr i32 %17, 4
%19 = and i32 15, %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0), i64 %20
%22 = load i8, i8* %21
%23 = load i8*, i8** %4
%24 = load i64, i64* %8
%25 = mul i64 %24, 2
%26 = add i64 %25, 0
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
%28 = load i32*, i32** %5
%29 = load i64, i64* %8
%30 = getelementptr inbounds i32, i32* %28, i64 %29
%31 = load i32, i32* %30
%32 = ashr i32 %31, 0
%33 = and i32 15, %32
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0), i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %4
%38 = load i64, i64* %8
%39 = mul i64 %38, 2
%40 = add i64 %39, 1
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 %36, i8* %41
br label %42
42: ; preds = %13
%43 = load i64, i64* %8
%44 = add i64 %43, 1
store i64 %44, i64* %8
br label %9
45: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
define void @tohex(i8* nocapture writeonly %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %5, label %6
5: ; preds = %6, %3
ret void
6: ; preds = %3, %6
%7 = phi i64 [ %24, %6 ], [ 0, %3 ]
%8 = getelementptr inbounds i32, i32* %1, i64 %7
%9 = load i32, i32* %8
%10 = lshr i32 %9, 4
%11 = and i32 %10, 15
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = shl i64 %7, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 %14, i8* %16
%17 = load i32, i32* %8
%18 = and i32 %17, 15
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = or i64 %15, 1
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 %21, i8* %23
%24 = add nuw i64 %7, 1
%25 = icmp eq i64 %24, %2
br i1 %25, label %5, label %6
}