int hashEncodeHex(unsigned char *hash, int buffer_length) {
  unsigned i;
  unsigned char buffer[(20 * 2) + 1];
  unsigned char *hexdict = (unsigned char *)"0123456789abcdef";
  if (buffer_length < (sizeof(buffer) / sizeof(unsigned char))) {
    (printf("Could not hexencode hash: Insufficient buffer size (Supplied: %d, "
            "Internal: %d)\n",
            buffer_length, (sizeof(buffer) / sizeof(unsigned char))));
    return -1;
  }
  for (i = 0; i < 20; i++) {
    buffer[i * 2] = hexdict[(hash[i]) >> 4];
    buffer[(i * 2) + 1] = hexdict[(hash[i]) & 0xF];
  }
  buffer[i * 2] = 0;
  memcpy(hash, buffer, (20 * 2) + 1);
  return 0;
}


x86 assembly (clang, -O0):
<hashEncodeHex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeca(%rip),%rax
mov    %rax,-0x58(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x29,%rax
jae    116a <hashEncodeHex+0x4a>
mov    -0x14(%rbp),%esi
lea    0xebf(%rip),%rdi
mov    $0x29,%edx
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <hashEncodeHex+0xda>
movl   $0x0,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jae    11d4 <hashEncodeHex+0xb4>
mov    -0x58(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
movzbl (%rcx,%rdx,1),%ecx
sar    $0x4,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x58(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
movzbl (%rcx,%rdx,1),%ecx
and    $0xf,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
mov    %eax,%eax
mov    %cl,-0x50(%rbp,%rax,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1171 <hashEncodeHex+0x51>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x29,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hashEncodeHex>:
sub    $0x38,%rsp
cmp    $0x29,%esi
jae    1136 <hashEncodeHex+0x26>
lea    0xef1(%rip),%rdi
mov    $0x29,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
ret
xor    %eax,%eax
lea    0xec1(%rip),%r8
nop
movzbl (%rdi,%rax,1),%edx
mov    %rdx,%rsi
shr    $0x4,%rsi
movzbl (%rsi,%r8,1),%ecx
mov    %cl,(%rsp,%rax,2)
and    $0xf,%edx
movzbl (%rdx,%r8,1),%ecx
mov    %cl,0x1(%rsp,%rax,2)
add    $0x1,%rax
cmp    $0x14,%rax
jne    1140 <hashEncodeHex+0x30>
movb   $0x0,0x28(%rsp)
movaps (%rsp),%xmm0
movaps 0x10(%rsp),%xmm1
movups %xmm1,0x10(%rdi)
movups %xmm0,(%rdi)
mov    0x19(%rsp),%rax
mov    %rax,0x19(%rdi)
mov    0x21(%rsp),%eax
mov    %eax,0x21(%rdi)
movzwl 0x25(%rsp),%eax
mov    %ax,0x25(%rdi)
mov    0x27(%rsp),%al
mov    %al,0x27(%rdi)
mov    0x28(%rsp),%al
mov    %al,0x28(%rdi)
xor    %eax,%eax
add    $0x38,%rsp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [81 x i8] c"Could not hexencode hash: Insufficient buffer size (Supplied: %d, Internal: %d)\0A\00"
define i32 @hashEncodeHex(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [41 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %8
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, 41
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* %5
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %13, i64 41)
store i32 -1, i32* %3
br label %61
15: ; preds = %2
store i32 0, i32* %6
br label %16
16: ; preds = %51, %15
%17 = load i32, i32* %6
%18 = icmp ult i32 %17, 20
br i1 %18, label %19, label %54
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = load i8*, i8** %4
%22 = load i32, i32* %6
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = ashr i32 %26, 4
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %20, i64 %28
%30 = load i8, i8* %29
%31 = load i32, i32* %6
%32 = mul i32 %31, 2
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %33
store i8 %30, i8* %34
%35 = load i8*, i8** %8
%36 = load i8*, i8** %4
%37 = load i32, i32* %6
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = and i32 %41, 15
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %35, i64 %43
%45 = load i8, i8* %44
%46 = load i32, i32* %6
%47 = mul i32 %46, 2
%48 = add i32 %47, 1
%49 = zext i32 %48 to i64
%50 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %49
store i8 %45, i8* %50
br label %51
51: ; preds = %19
%52 = load i32, i32* %6
%53 = add i32 %52, 1
store i32 %53, i32* %6
br label %16
54: ; preds = %16
%55 = load i32, i32* %6
%56 = mul i32 %55, 2
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %4
%60 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 0
store i32 0, i32* %3
br label %61
61: ; preds = %54, %12
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [17 x i8] c"0123456789abcdef\00"
@.str.1 = [81 x i8] c"Could not hexencode hash: Insufficient buffer size (Supplied: %d, Internal: %d)\0A\00"
define i32 @hashEncodeHex(i8* nocapture %0, i32 %1) {
%3 = alloca [41 x i8]
%4 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%5 = icmp ult i32 %1, 41
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @.str.1, i64 0, i64 0), i32 %1, i64 41)
br label %28
8: ; preds = %2, %8
%9 = phi i64 [ %24, %8 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = lshr i8 %11, 4
%13 = zext i8 %12 to i64
%14 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = shl nuw nsw i64 %9, 1
%17 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 %16
store i8 %15, i8* %17
%18 = and i8 %11, 15
%19 = zext i8 %18 to i64
%20 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = or i64 %16, 1
%23 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 %22
store i8 %21, i8* %23
%24 = add nuw nsw i64 %9, 1
%25 = icmp eq i64 %24, 20
br i1 %25, label %26, label %8
26: ; preds = %8
%27 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 40
store i8 0, i8* %27
br label %28
28: ; preds = %26, %6
%29 = phi i32 [ -1, %6 ], [ 0, %26 ]
ret i32 %29
}