void str2hex(char *out, char *in) {
  const char hextab[17] = "0123456789ABCDEF";
  while (*in) {
    *out++ = hextab[*in >> 4];
    *out++ = hextab[*in & 0x0F];
    in++;
  }
}


x86 assembly (clang, -O0):
<str2hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xeed(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeea(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xee8(%rip),%al
mov    %al,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     118b <str2hex+0x8b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
cltq
mov    -0x30(%rbp,%rax,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
cltq
mov    -0x30(%rbp,%rax,1),%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    112b <str2hex+0x2b>
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2hex>:
mov    (%rsi),%dl
test   %dl,%dl
je     113a <str2hex+0x3a>
xor    %eax,%eax
lea    0xef1(%rip),%rcx
nop
sar    $0x4,%dl
movsbq %dl,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rdi,%rax,2)
movzbl (%rsi,%rax,1),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rdi,%rax,2)
movzbl 0x1(%rsi,%rax,1),%edx
add    $0x1,%rax
test   %dl,%dl
jne    1110 <str2hex+0x10>
ret

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

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