void rs_hexify(char *to_buf, void const *from, int from_len) {
  static const char hex_chars[] = "0123456789abcdef";
  unsigned char const *from_buf = (unsigned char const *)from;
  while (from_len-- > 0) {
    *(to_buf++) = hex_chars[((*from_buf) >> 4) & 0xf];
    *(to_buf++) = hex_chars[(*from_buf) & 0xf];
    from_buf++;
  }
  *to_buf = 0;
}


x86 assembly (clang, -O0):
<rs_hexify>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    118f <rs_hexify+0x8f>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xebe(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe96(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1117 <rs_hexify+0x17>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<rs_hexify>:
test   %edx,%edx
jle    11a4 <rs_hexify+0xa4>
test   $0x1,%dl
jne    1119 <rs_hexify+0x19>
mov    %edx,%eax
cmp    $0x1,%edx
jne    114a <rs_hexify+0x4a>
jmp    11a4 <rs_hexify+0xa4>
lea    -0x1(%rdx),%eax
movzbl (%rsi),%ecx
shr    $0x4,%rcx
lea    0xed6(%rip),%r8
mov    (%rcx,%r8,1),%cl
mov    %cl,(%rdi)
movzbl (%rsi),%ecx
and    $0xf,%ecx
mov    (%rcx,%r8,1),%cl
mov    %cl,0x1(%rdi)
add    $0x2,%rdi
add    $0x1,%rsi
cmp    $0x1,%edx
je     11a4 <rs_hexify+0xa4>
add    $0xffffffff,%eax
lea    0xeac(%rip),%rcx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%rsi),%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rdi)
movzbl (%rsi),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rdi)
movzbl 0x1(%rsi),%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x2(%rdi)
movzbl 0x1(%rsi),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x3(%rdi)
add    $0x4,%rdi
add    $0x2,%rsi
add    $0xfffffffe,%eax
jb     1160 <rs_hexify+0x60>
movb   $0x0,(%rdi)
ret

LLVM IR (-O0):
@rs_hexify.hex_chars = internal constant [17 x i8] c"0123456789abcdef\00"
define void @rs_hexify(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** %5
store i8* %8, i8** %7
br label %9
9: ; preds = %13, %3
%10 = load i32, i32* %6
%11 = add nsw i32 %10, -1
store i32 %11, i32* %6
%12 = icmp sgt i32 %10, 0
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = load i8*, i8** %7
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = ashr i32 %16, 4
%18 = and i32 %17, 15
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %4
store i8 %21, i8* %22
%24 = load i8*, i8** %7
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = and i32 %26, 15
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 %30, i8* %31
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %7
br label %9
35: ; preds = %9
%36 = load i8*, i8** %4
store i8 0, i8* %36
ret void
}

LLVM IR (-O2):
@rs_hexify.hex_chars = internal unnamed_addr constant [17 x i8] c"0123456789abcdef\00"
define void @rs_hexify(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %62
5: ; preds = %3
%6 = and i32 %2, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %23, label %8
8: ; preds = %5
%9 = add nsw i32 %2, -1
%10 = load i8, i8* %1
%11 = lshr i8 %10, 4
%12 = zext i8 %11 to i64
%13 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %14, i8* %0
%16 = load i8, i8* %1
%17 = and i8 %16, 15
%18 = zext i8 %17 to i64
%19 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %20, i8* %15
%22 = getelementptr inbounds i8, i8* %1, i64 1
br label %23
23: ; preds = %8, %5
%24 = phi i8* [ %1, %5 ], [ %22, %8 ]
%25 = phi i32 [ %2, %5 ], [ %9, %8 ]
%26 = phi i8* [ %0, %5 ], [ %21, %8 ]
%27 = phi i8* [ undef, %5 ], [ %21, %8 ]
%28 = icmp eq i32 %2, 1
br i1 %28, label %62, label %29
29: ; preds = %23, %29
%30 = phi i8* [ %59, %29 ], [ %24, %23 ]
%31 = phi i32 [ %46, %29 ], [ %25, %23 ]
%32 = phi i8* [ %58, %29 ], [ %26, %23 ]
%33 = load i8, i8* %30
%34 = lshr i8 %33, 4
%35 = zext i8 %34 to i64
%36 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = getelementptr inbounds i8, i8* %32, i64 1
store i8 %37, i8* %32
%39 = load i8, i8* %30
%40 = and i8 %39, 15
%41 = zext i8 %40 to i64
%42 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %41
%43 = load i8, i8* %42
%44 = getelementptr inbounds i8, i8* %32, i64 2
store i8 %43, i8* %38
%45 = getelementptr inbounds i8, i8* %30, i64 1
%46 = add nsw i32 %31, -2
%47 = load i8, i8* %45
%48 = lshr i8 %47, 4
%49 = zext i8 %48 to i64
%50 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = getelementptr inbounds i8, i8* %32, i64 3
store i8 %51, i8* %44
%53 = load i8, i8* %45
%54 = and i8 %53, 15
%55 = zext i8 %54 to i64
%56 = getelementptr inbounds [17 x i8], [17 x i8]* @rs_hexify.hex_chars, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = getelementptr inbounds i8, i8* %32, i64 4
store i8 %57, i8* %52
%59 = getelementptr inbounds i8, i8* %30, i64 2
%60 = add i32 %31, -3
%61 = icmp ult i32 %60, -2
br i1 %61, label %29, label %62
62: ; preds = %23, %29, %3
%63 = phi i8* [ %0, %3 ], [ %27, %23 ], [ %58, %29 ]
store i8 0, i8* %63
ret void
}