int tohex(char *s, const char *t) {
  int i;
  static char hex[] = "0123456789abcdef";
  i = 0;
  while (*t) {
    *s++ = hex[(*t >> 4) & 0x0f];
    *s++ = hex[*t & 0x0f];
    t++;
    i++;
  }
  *s = '\0';
  return i;
}


x86 assembly (clang, -O0):
<tohex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     118d <tohex+0x8d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0x2ef9(%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    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0x2ed1(%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    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1113 <tohex+0x13>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<tohex>:
mov    (%rsi),%dl
test   %dl,%dl
je     1151 <tohex+0x51>
add    $0x1,%rsi
xor    %eax,%eax
lea    0xeed(%rip),%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl %dl,%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rdi)
movzbl -0x1(%rsi),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rdi)
add    $0x2,%rdi
add    $0x1,%eax
movzbl (%rsi),%edx
add    $0x1,%rsi
test   %dl,%dl
jne    1120 <tohex+0x20>
movb   $0x0,(%rdi)
ret
xor    %eax,%eax
movb   $0x0,(%rdi)
ret

LLVM IR (-O0):
@tohex.hex = internal global [17 x i8] c"0123456789abcdef\00"
define i32 @tohex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %6
6: ; preds = %10, %2
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %6
%11 = load i8*, i8** %4
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = ashr i32 %13, 4
%15 = and i32 %14, 15
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [17 x i8], [17 x i8]* @tohex.hex, 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]* @tohex.hex, 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
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %6
34: ; preds = %6
%35 = load i8*, i8** %3
store i8 0, i8* %35
%36 = load i32, i32* %5
ret i32 %36
}

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