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


x86 assembly (clang, -O0):
<sha1_to_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    1192 <sha1_to_hex+0x92>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
shr    $0x4,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xeb5(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe90(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    111b <sha1_to_hex+0x1b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
pop    %rbp
ret

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

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

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