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


x86 assembly (clang, -O0):
<print_sha1>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x14,-0x1c(%rbp)
jge    1192 <print_sha1+0x92>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%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 <print_sha1+0x1b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
pop    %rbp
ret

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

LLVM IR (-O0):
@print_sha1.hex = internal constant [17 x i8] c"0123456789abcdef\00"
define void @print_sha1(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** %3
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** %4
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %4
%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]* @print_sha1.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]* @print_sha1.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
ret void
}

LLVM IR (-O2):
@print_sha1.hex = internal unnamed_addr constant [17 x i8] c"0123456789abcdef\00"
define void @print_sha1(i8* nocapture writeonly %0, i8* nocapture %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i8* [ %1, %2 ], [ %20, %3 ]
%5 = phi i32 [ 0, %2 ], [ %33, %3 ]
%6 = phi i8* [ %0, %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]* @print_sha1.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]* @print_sha1.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]* @print_sha1.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]* @print_sha1.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 void
}