void binary_to_hex(const unsigned char *bin, char *hex, unsigned int length) {
  static const char trans[] = "0123456789ABCDEF";
  for (const unsigned char *const end = bin + length; bin < end; ++bin) {
    *hex++ = trans[*bin >> 4];
    *hex++ = trans[*bin & 0xf];
  }
  *hex = '\0';
}


x86 assembly (clang, -O0):
<binary_to_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    118c <binary_to_hex+0x8c>
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0xec1(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe99(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    111d <binary_to_hex+0x1d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_to_hex>:
test   %edx,%edx
je     1137 <binary_to_hex+0x37>
mov    %edx,%eax
add    %rdi,%rax
lea    0xef0(%rip),%rcx
movzbl (%rdi),%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rsi)
movzbl (%rdi),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rsi)
add    $0x2,%rsi
add    $0x1,%rdi
cmp    %rax,%rdi
jb     1110 <binary_to_hex+0x10>
movb   $0x0,(%rsi)
ret

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

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