int sha1_to_hex(const unsigned char *sha1, char *hexbuf) {
  static const char hex[] = "0123456789abcdef";
  int i;
  for (i = 0; i < 20; i++) {
    unsigned int val = *sha1++;
    *hexbuf++ = hex[val >> 4];
    *hexbuf++ = hex[val & 0xf];
  }
  *hexbuf = '\0';
  return 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    118a <sha1_to_hex+0x8a>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shr    $0x4,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xebd(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%eax
and    $0xf,%eax
mov    %eax,%eax
mov    %eax,%ecx
lea    0xe98(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
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,(%rax)
xor    %eax,%eax
pop    %rbp
ret

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

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

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