void sha1_to_hex(const unsigned char *sha1, char *buf) {
  static const char hex[] = "0123456789abcdef";
  for (int i = 0; i < 20; ++i) {
    char *pos = buf + i * 2;
    *pos++ = hex[sha1[i] >> 4];
    *pos = hex[sha1[i] & 0xf];
  }
  buf[40] = '\0';
}


x86 assembly (clang, -O0):
<sha1_to_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jge    118e <sha1_to_hex+0x8e>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0xeb6(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe89(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1113 <sha1_to_hex+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,0x28(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha1_to_hex>:
mov    $0x1,%eax
lea    0xef4(%rip),%rcx
nopl   0x0(%rax)
movzbl -0x1(%rdi,%rax,1),%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,-0x2(%rsi,%rax,2)
movzbl -0x1(%rdi,%rax,1),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,-0x1(%rsi,%rax,2)
movzbl (%rdi,%rax,1),%edx
shr    $0x4,%rdx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rsi,%rax,2)
movzbl (%rdi,%rax,1),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rsi,%rax,2)
add    $0x2,%rax
cmp    $0x15,%rax
jne    1110 <sha1_to_hex+0x10>
movb   $0x0,0x28(%rsi)
ret

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

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