#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int snprintf(char *, int, char *, int const);
int crypto_binary_to_hex(const unsigned char *binary, const unsigned int length,
                         char *hex) {
  int i;
  assert(hex && binary);
  if (!hex || !binary)
    return 1;
  for (i = 0; i < length; i++) {
    const int tmp = binary[i];
    if (snprintf(hex + i * 2, 3, "%02X", tmp) != 2) {
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<crypto_binary_to_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
je     114e <crypto_binary_to_hex+0x2e>
cmpq   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
je     1171 <crypto_binary_to_hex+0x51>
cmpq   $0x0,-0x10(%rbp)
jne    117d <crypto_binary_to_hex+0x5d>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <crypto_binary_to_hex+0xd1>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11ea <crypto_binary_to_hex+0xca>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%ecx
mov    $0x3,%esi
lea    0xe43(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x2,%eax
je     11d7 <crypto_binary_to_hex+0xb7>
movl   $0x1,-0x4(%rbp)
jmp    11f1 <crypto_binary_to_hex+0xd1>
jmp    11dc <crypto_binary_to_hex+0xbc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1184 <crypto_binary_to_hex+0x64>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_binary_to_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r12
test   %rdx,%rdx
setne  %al
test   %rdi,%rdi
setne  %cl
and    %al,%cl
movzbl %cl,%ebp
mov    %ebp,%edi
call   1040 <assert@plt>
mov    $0x1,%r14d
cmp    $0x1,%bpl
jne    119a <crypto_binary_to_hex+0x7a>
test   %r15d,%r15d
je     1197 <crypto_binary_to_hex+0x77>
mov    %r15d,%r13d
xor    %ebp,%ebp
lea    0xe97(%rip),%r15
nopl   0x0(%rax)
movzbl (%r12,%rbp,1),%ecx
mov    %rbx,%rdi
mov    $0x3,%esi
mov    %r15,%rdx
call   1030 <snprintf@plt>
cmp    $0x2,%eax
jne    119a <crypto_binary_to_hex+0x7a>
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1170 <crypto_binary_to_hex+0x50>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%02X\00"
define i32 @crypto_binary_to_hex(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br label %15
15: ; preds = %12, %3
%16 = phi i1 [ false, %3 ], [ %14, %12 ]
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %21, %15
store i32 1, i32* %4
br label %51
25: ; preds = %21
store i32 0, i32* %8
br label %26
26: ; preds = %47, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %6
%29 = icmp ult i32 %27, %28
br i1 %29, label %30, label %50
30: ; preds = %26
%31 = load i8*, i8** %5
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
store i32 %36, i32* %9
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = mul nsw i32 %38, 2
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i32, i32* %9
%43 = call i32 @snprintf(i8* %41, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = icmp ne i32 %43, 2
br i1 %44, label %45, label %46
45: ; preds = %30
store i32 1, i32* %4
br label %51
46: ; preds = %30
br label %47
47: ; preds = %46
%48 = load i32, i32* %8
%49 = add nsw i32 %48, 1
store i32 %49, i32* %8
br label %26
50: ; preds = %26
store i32 0, i32* %4
br label %51
51: ; preds = %50, %45, %24
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
define i32 @crypto_binary_to_hex(i8* %0, i32 %1, i8* %2) {
%4 = icmp ne i8* %2, null
%5 = icmp ne i8* %0, null
%6 = and i1 %5, %4
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
br i1 %6, label %9, label %25
9: ; preds = %3
%10 = icmp eq i32 %1, 0
br i1 %10, label %25, label %11
11: ; preds = %9
%12 = zext i32 %1 to i64
br label %15
13: ; preds = %15
%14 = icmp eq i64 %24, %12
br i1 %14, label %25, label %15
15: ; preds = %11, %13
%16 = phi i64 [ 0, %11 ], [ %24, %13 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl nuw nsw i64 %16, 1
%21 = getelementptr inbounds i8, i8* %2, i64 %20
%22 = tail call i32 @snprintf(i8* %21, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %19)
%23 = icmp eq i32 %22, 2
%24 = add nuw nsw i64 %16, 1
br i1 %23, label %13, label %25
25: ; preds = %15, %13, %9, %3
%26 = phi i32 [ 1, %3 ], [ 0, %9 ], [ 1, %15 ], [ 0, %13 ]
ret i32 %26
}