#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 uint8_t;

/* Variables and functions */
unsigned char _nibble_to_hex_char(int const);
int /*<<< orphan*/ base16_encode_size(size_t);
unsigned char *malloc(int /*<<< orphan*/);
unsigned char *base16_encode(unsigned char *out, size_t *out_len,
                             const unsigned char *in, size_t in_len) {
  int i = 0;
  const uint8_t *work = (const uint8_t *)in;
  if (!out) {
    if (!(out = malloc(base16_encode_size(in_len))))
      return NULL;
  }
  for (i = 0; i < in_len; i++) {
    out[(i * 2)] = _nibble_to_hex_char((work[i] & 0xf0) >> 4);
    out[(i * 2) + 1] = _nibble_to_hex_char(work[i] & 0x0f);
  }
  *out_len = (i * 2) + 1;
  out[*out_len - 1] = 0;
  return out;
}


x86 assembly (clang, -O0):
<base16_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1192 <base16_encode+0x62>
mov    -0x28(%rbp),%rdi
call   1050 <base16_encode_size@plt>
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    118d <base16_encode+0x5d>
movq   $0x0,-0x8(%rbp)
jmp    1236 <base16_encode+0x106>
jmp    1192 <base16_encode+0x62>
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1208 <base16_encode+0xd8>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
and    $0xf0,%edi
sar    $0x4,%edi
call   1030 <_nibble_to_hex_char@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
and    $0xf,%edi
call   1030 <_nibble_to_hex_char@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1199 <base16_encode+0x69>
mov    -0x2c(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base16_encode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%r12
test   %rdi,%rdi
jne    1161 <base16_encode+0x31>
mov    %r15,%rdi
call   1050 <base16_encode_size@plt>
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
je     11c4 <base16_encode+0x94>
test   %r15,%r15
je     11a8 <base16_encode+0x78>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%rbx,4),%edi
shr    $0x4,%edi
and    $0xf,%edi
call   1030 <_nibble_to_hex_char@plt>
mov    %al,(%r12,%rbx,2)
mov    0x0(%r13,%rbx,4),%edi
and    $0xf,%edi
call   1030 <_nibble_to_hex_char@plt>
mov    %al,0x1(%r12,%rbx,2)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1170 <base16_encode+0x40>
add    %r15d,%r15d
or     $0x1,%r15
jmp    11ae <base16_encode+0x7e>
mov    $0x1,%r15d
mov    %r15,(%r14)
movb   $0x0,-0x1(%r15,%r12,1)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
xor    %r12d,%r12d
jmp    11b7 <base16_encode+0x87>

LLVM IR (-O0):
define i8* @base16_encode(i8* %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
store i32 0, i32* %10
%12 = load i8*, i8** %8
%13 = bitcast i8* %12 to i32*
store i32* %13, i32** %11
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %4
%17 = load i64, i64* %9
%18 = call i32 @base16_encode_size(i64 %17)
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %16
store i8* null, i8** %5
br label %71
22: ; preds = %16
br label %23
23: ; preds = %22, %4
store i32 0, i32* %10
br label %24
24: ; preds = %56, %23
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = load i64, i64* %9
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %59
29: ; preds = %24
%30 = load i32*, i32** %11
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %30, i64 %32
%34 = load i32, i32* %33
%35 = and i32 %34, 240
%36 = ashr i32 %35, 4
%37 = call zeroext i8 @_nibble_to_hex_char(i32 %36)
%38 = load i8*, i8** %6
%39 = load i32, i32* %10
%40 = mul nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 %37, i8* %42
%43 = load i32*, i32** %11
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = and i32 %47, 15
%49 = call zeroext i8 @_nibble_to_hex_char(i32 %48)
%50 = load i8*, i8** %6
%51 = load i32, i32* %10
%52 = mul nsw i32 %51, 2
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %50, i64 %54
store i8 %49, i8* %55
br label %56
56: ; preds = %29
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10
br label %24
59: ; preds = %24
%60 = load i32, i32* %10
%61 = mul nsw i32 %60, 2
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = load i64*, i64** %7
store i64 %63, i64* %64
%65 = load i8*, i8** %6
%66 = load i64*, i64** %7
%67 = load i64, i64* %66
%68 = sub i64 %67, 1
%69 = getelementptr inbounds i8, i8* %65, i64 %68
store i8 0, i8* %69
%70 = load i8*, i8** %6
store i8* %70, i8** %5
br label %71
71: ; preds = %59, %21
%72 = load i8*, i8** %5
ret i8* %72
}

LLVM IR (-O2):
define i8* @base16_encode(i8* writeonly %0, i64* nocapture writeonly %1, i8* nocapture %2, i64 %3) {
%5 = bitcast i8* %2 to i32*
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = tail call i32 @base16_encode_size(i64 %3)
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %38, label %11
11: ; preds = %7, %4
%12 = phi i8* [ %0, %4 ], [ %9, %7 ]
%13 = icmp eq i64 %3, 0
br i1 %13, label %34, label %14
14: ; preds = %11, %14
%15 = phi i64 [ %28, %14 ], [ 0, %11 ]
%16 = getelementptr inbounds i32, i32* %5, i64 %15
%17 = load i32, i32* %16
%18 = lshr i32 %17, 4
%19 = and i32 %18, 15
%20 = tail call zeroext i8 @_nibble_to_hex_char(i32 %19)
%21 = shl nuw nsw i64 %15, 1
%22 = getelementptr inbounds i8, i8* %12, i64 %21
store i8 %20, i8* %22
%23 = load i32, i32* %16
%24 = and i32 %23, 15
%25 = tail call zeroext i8 @_nibble_to_hex_char(i32 %24)
%26 = or i64 %21, 1
%27 = getelementptr inbounds i8, i8* %12, i64 %26
store i8 %25, i8* %27
%28 = add nuw i64 %15, 1
%29 = icmp eq i64 %28, %3
br i1 %29, label %30, label %14
30: ; preds = %14
%31 = shl i64 %3, 1
%32 = and i64 %31, 4294967294
%33 = or i64 %32, 1
br label %34
34: ; preds = %30, %11
%35 = phi i64 [ 1, %11 ], [ %33, %30 ]
store i64 %35, i64* %1
%36 = add nsw i64 %35, -1
%37 = getelementptr inbounds i8, i8* %12, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %7, %34
%39 = phi i8* [ %12, %34 ], [ null, %7 ]
ret i8* %39
}