#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int ssize_t;

/* Variables and functions */
ssize_t chars8bit_to_hexstr(const char *in, size_t in_length, char *out,
                            size_t out_size) {
  static const char hex_table[] = "0123456789ABCDEF";
  const unsigned char *in2 = (const unsigned char *)in;
  if (out_size - 1 < in_length * 2) {
    return -1;
  }
  out_size = in_length * 2;
  for (; in_length; --in_length, ++in2) {
    *out++ = hex_table[*in2 >> 4];
    *out++ = hex_table[*in2 & 0xF];
  }
  *out = 0;
  return out_size;
}


x86 assembly (clang, -O0):
<chars8bit_to_hexstr>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    -0x18(%rbp),%rcx
shl    $0x1,%rcx
cmp    %rcx,%rax
jae    1141 <chars8bit_to_hexstr+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <chars8bit_to_hexstr+0xd3>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c5 <chars8bit_to_hexstr+0xc5>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
sar    $0x4,%eax
movslq %eax,%rcx
lea    0xe94(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
and    $0xf,%eax
movslq %eax,%rcx
lea    0xe6c(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    114d <chars8bit_to_hexstr+0x4d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<chars8bit_to_hexstr>:
add    $0xffffffffffffffff,%rcx
lea    (%rsi,%rsi,1),%r8
mov    $0xffffffff,%eax
cmp    %r8,%rcx
jb     11be <chars8bit_to_hexstr+0xbe>
test   %rsi,%rsi
je     11b8 <chars8bit_to_hexstr+0xb8>
test   $0x1,%sil
jne    1133 <chars8bit_to_hexstr+0x33>
mov    %rsi,%r9
cmp    $0x1,%rsi
jne    1164 <chars8bit_to_hexstr+0x64>
jmp    11b8 <chars8bit_to_hexstr+0xb8>
movzbl (%rdi),%eax
shr    $0x4,%rax
lea    0xebf(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,(%rdx)
movzbl (%rdi),%eax
and    $0xf,%eax
mov    (%rax,%rcx,1),%al
mov    %al,0x1(%rdx)
add    $0x2,%rdx
lea    -0x1(%rsi),%r9
add    $0x1,%rdi
cmp    $0x1,%rsi
je     11b8 <chars8bit_to_hexstr+0xb8>
xor    %ecx,%ecx
lea    0xe93(%rip),%rsi
nopl   (%rax)
movzbl (%rdi,%rcx,1),%eax
shr    $0x4,%rax
movzbl (%rax,%rsi,1),%eax
mov    %al,(%rdx)
movzbl (%rdi,%rcx,1),%eax
and    $0xf,%eax
movzbl (%rax,%rsi,1),%eax
mov    %al,0x1(%rdx)
movzbl 0x1(%rdi,%rcx,1),%eax
shr    $0x4,%rax
movzbl (%rax,%rsi,1),%eax
mov    %al,0x2(%rdx)
movzbl 0x1(%rdi,%rcx,1),%eax
and    $0xf,%eax
movzbl (%rax,%rsi,1),%eax
mov    %al,0x3(%rdx)
add    $0x4,%rdx
add    $0x2,%rcx
cmp    %rcx,%r9
jne    1170 <chars8bit_to_hexstr+0x70>
movb   $0x0,(%rdx)
mov    %r8d,%eax
ret

LLVM IR (-O0):
@chars8bit_to_hexstr.hex_table = internal constant [17 x i8] c"0123456789ABCDEF\00"
define i32 @chars8bit_to_hexstr(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i64, i64* %9
%13 = sub i64 %12, 1
%14 = load i64, i64* %7
%15 = mul i64 %14, 2
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %52
18: ; preds = %4
%19 = load i64, i64* %7
%20 = mul i64 %19, 2
store i64 %20, i64* %9
br label %21
21: ; preds = %43, %18
%22 = load i64, i64* %7
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %48
24: ; preds = %21
%25 = load i8*, i8** %10
%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 [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %8
store i8 %31, i8* %32
%34 = load i8*, i8** %10
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = and i32 %36, 15
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %8
store i8 %40, i8* %41
br label %43
43: ; preds = %24
%44 = load i64, i64* %7
%45 = add i64 %44, -1
store i64 %45, i64* %7
%46 = load i8*, i8** %10
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %10
br label %21
48: ; preds = %21
%49 = load i8*, i8** %8
store i8 0, i8* %49
%50 = load i64, i64* %9
%51 = trunc i64 %50 to i32
store i32 %51, i32* %5
br label %52
52: ; preds = %48, %17
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
@chars8bit_to_hexstr.hex_table = internal unnamed_addr constant [17 x i8] c"0123456789ABCDEF\00"
define i32 @chars8bit_to_hexstr(i8* nocapture %0, i64 %1, i8* nocapture writeonly %2, i64 %3) {
%5 = add i64 %3, -1
%6 = shl i64 %1, 1
%7 = icmp ult i64 %5, %6
br i1 %7, label %69, label %8
8: ; preds = %4
%9 = icmp eq i64 %1, 0
br i1 %9, label %66, label %10
10: ; preds = %8
%11 = and i64 %1, 1
%12 = icmp eq i64 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %10
%14 = load i8, i8* %0
%15 = lshr i8 %14, 4
%16 = zext i8 %15 to i64
%17 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %2, i64 1
store i8 %18, i8* %2
%20 = load i8, i8* %0
%21 = and i8 %20, 15
%22 = zext i8 %21 to i64
%23 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = getelementptr inbounds i8, i8* %2, i64 2
store i8 %24, i8* %19
%26 = add i64 %1, -1
%27 = getelementptr inbounds i8, i8* %0, i64 1
br label %28
28: ; preds = %13, %10
%29 = phi i8* [ undef, %10 ], [ %25, %13 ]
%30 = phi i8* [ %0, %10 ], [ %27, %13 ]
%31 = phi i8* [ %2, %10 ], [ %25, %13 ]
%32 = phi i64 [ %1, %10 ], [ %26, %13 ]
%33 = icmp eq i64 %1, 1
br i1 %33, label %66, label %34
34: ; preds = %28, %34
%35 = phi i8* [ %64, %34 ], [ %30, %28 ]
%36 = phi i8* [ %62, %34 ], [ %31, %28 ]
%37 = phi i64 [ %63, %34 ], [ %32, %28 ]
%38 = load i8, i8* %35
%39 = lshr i8 %38, 4
%40 = zext i8 %39 to i64
%41 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = getelementptr inbounds i8, i8* %36, i64 1
store i8 %42, i8* %36
%44 = load i8, i8* %35
%45 = and i8 %44, 15
%46 = zext i8 %45 to i64
%47 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %46
%48 = load i8, i8* %47
%49 = getelementptr inbounds i8, i8* %36, i64 2
store i8 %48, i8* %43
%50 = getelementptr inbounds i8, i8* %35, i64 1
%51 = load i8, i8* %50
%52 = lshr i8 %51, 4
%53 = zext i8 %52 to i64
%54 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %53
%55 = load i8, i8* %54
%56 = getelementptr inbounds i8, i8* %36, i64 3
store i8 %55, i8* %49
%57 = load i8, i8* %50
%58 = and i8 %57, 15
%59 = zext i8 %58 to i64
%60 = getelementptr inbounds [17 x i8], [17 x i8]* @chars8bit_to_hexstr.hex_table, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = getelementptr inbounds i8, i8* %36, i64 4
store i8 %61, i8* %56
%63 = add i64 %37, -2
%64 = getelementptr inbounds i8, i8* %35, i64 2
%65 = icmp eq i64 %63, 0
br i1 %65, label %66, label %34
66: ; preds = %28, %34, %8
%67 = phi i8* [ %2, %8 ], [ %29, %28 ], [ %62, %34 ]
store i8 0, i8* %67
%68 = trunc i64 %6 to i32
br label %69
69: ; preds = %4, %66
%70 = phi i32 [ %68, %66 ], [ -1, %4 ]
ret i32 %70
}