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


x86 assembly (clang, -O0):
<sha1_to_hex>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
lea    0x2f21(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jge    1191 <sha1_to_hex+0x91>
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    0xeb6(%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    0xe91(%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    111a <sha1_to_hex+0x1a>
lea    0x2e98(%rip),%rax
pop    %rbp
ret

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

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

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