int _sha1_formatter(unsigned char *data, char formatted[40]) {
  static const char *hex_lookup = "0123456789ABCDEF";
  int i;
  for (i = 0; i < 20; i++) {
    formatted[i * 2] = hex_lookup[data[i] & 0x0F];
    formatted[i * 2 + 1] = hex_lookup[(data[i] / 0x0F) & 0x0F];
  }
  return 0;
}


x86 assembly (clang, -O0):
<_sha1_formatter>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jge    1198 <_sha1_formatter+0x98>
mov    0x2efc(%rip),%rax
mov    -0x8(%rbp),%rcx
movslq -0x14(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    0x2ed0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    $0xf,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1113 <_sha1_formatter+0x13>
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sha1_formatter>:
xor    %eax,%eax
lea    0xef7(%rip),%rcx
nopl   0x0(%rax)
movzbl (%rdi,%rax,1),%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,(%rsi,%rax,2)
movzbl (%rdi,%rax,1),%edx
imul   $0x89,%edx,%edx
shr    $0xb,%edx
and    $0xf,%edx
movzbl (%rdx,%rcx,1),%edx
mov    %dl,0x1(%rsi,%rax,2)
add    $0x1,%rax
cmp    $0x14,%rax
jne    1110 <_sha1_formatter+0x10>
xor    %eax,%eax
ret

LLVM IR (-O0):
@_sha1_formatter.hex_lookup = internal global i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i32 0, i32 0)
@.str = [17 x i8] c"0123456789ABCDEF\00"
define i32 @_sha1_formatter(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 = %44, %2
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 20
br i1 %8, label %9, label %47
9: ; preds = %6
%10 = load i8*, i8** @_sha1_formatter.hex_lookup
%11 = load i8*, i8** %3
%12 = load i32, i32* %5
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = and i32 %16, 15
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %10, i64 %18
%20 = load i8, i8* %19
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = mul nsw i32 %22, 2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
%26 = load i8*, i8** @_sha1_formatter.hex_lookup
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = sdiv i32 %32, 15
%34 = and i32 %33, 15
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %26, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %4
%39 = load i32, i32* %5
%40 = mul nsw i32 %39, 2
%41 = add nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
store i8 %37, i8* %43
br label %44
44: ; preds = %9
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %6
47: ; preds = %6
ret i32 0
}

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