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))) {
    ERROR(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    0xeba(%rip),%rax
mov    %rax,-0x58(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x29,%rax
jae    1183 <hashEncodeHex+0x53>
mov    -0x14(%rbp),%esi
lea    0xeaf(%rip),%rdi
mov    $0x29,%edx
mov    $0x0,%al
call   1030 <printf@plt>
mov    %eax,%edi
mov    $0x0,%al
call   1050 <ERROR@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <hashEncodeHex+0xe3>
movl   $0x0,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jae    11ed <hashEncodeHex+0xbd>
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    118a <hashEncodeHex+0x5a>
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    114f <hashEncodeHex+0x2f>
lea    0xee1(%rip),%rdi
mov    $0x29,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %eax,%edi
xor    %eax,%eax
call   1040 <ERROR@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
ret
xor    %eax,%eax
lea    0xea8(%rip),%r8
nopl   0x0(%rax,%rax,1)
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    1160 <hashEncodeHex+0x40>
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 %16
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)
%15 = call i32 (i32, ...) bitcast (i32 (...)* @ERROR to i32 (i32, ...)*)(i32 %14)
store i32 -1, i32* %3
br label %62
16: ; preds = %2
store i32 0, i32* %6
br label %17
17: ; preds = %52, %16
%18 = load i32, i32* %6
%19 = icmp ult i32 %18, 20
br i1 %19, label %20, label %55
20: ; preds = %17
%21 = load i8*, i8** %8
%22 = load i8*, i8** %4
%23 = load i32, i32* %6
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = ashr i32 %27, 4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %21, i64 %29
%31 = load i8, i8* %30
%32 = load i32, i32* %6
%33 = mul i32 %32, 2
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %34
store i8 %31, i8* %35
%36 = load i8*, i8** %8
%37 = load i8*, i8** %4
%38 = load i32, i32* %6
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = and i32 %42, 15
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %36, i64 %44
%46 = load i8, i8* %45
%47 = load i32, i32* %6
%48 = mul i32 %47, 2
%49 = add i32 %48, 1
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %50
store i8 %46, i8* %51
br label %52
52: ; preds = %20
%53 = load i32, i32* %6
%54 = add i32 %53, 1
store i32 %54, i32* %6
br label %17
55: ; preds = %17
%56 = load i32, i32* %6
%57 = mul i32 %56, 2
%58 = zext i32 %57 to i64
%59 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %4
%61 = getelementptr inbounds [41 x i8], [41 x i8]* %7, i64 0, i64 0
store i32 0, i32* %3
br label %62
62: ; preds = %55, %12
%63 = load i32, i32* %3
ret i32 %63
}

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 %9
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)
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @ERROR to i32 (i32, ...)*)(i32 %7)
br label %29
9: ; preds = %2, %9
%10 = phi i64 [ %25, %9 ], [ 0, %2 ]
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = lshr i8 %12, 4
%14 = zext i8 %13 to i64
%15 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = shl nuw nsw i64 %10, 1
%18 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 %17
store i8 %16, i8* %18
%19 = and i8 %12, 15
%20 = zext i8 %19 to i64
%21 = getelementptr inbounds [17 x i8], [17 x i8]* @.str, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = or i64 %17, 1
%24 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 %23
store i8 %22, i8* %24
%25 = add nuw nsw i64 %10, 1
%26 = icmp eq i64 %25, 20
br i1 %26, label %27, label %9
27: ; preds = %9
%28 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 40
store i8 0, i8* %28
br label %29
29: ; preds = %27, %6
%30 = phi i32 [ -1, %6 ], [ 0, %27 ]
ret i32 %30
}