#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ *, unsigned char *);
int /*<<< orphan*/ BN_free(int /*<<< orphan*/ *);
int BN_hex2bn(int /*<<< orphan*/ **, char *);
scalar_t__ BN_num_bytes(int /*<<< orphan*/ *);
unsigned char *hex_to_bin(unsigned char *bin_out, char *hex_in, ssize_t size) {
  BIGNUM *bn = NULL;
  int error = 0;
  error = BN_hex2bn(&bn, hex_in);
  if (error == 0) {
    bin_out = 0;
    goto errors;
  }
  if (BN_num_bytes(bn) > size) {
    bin_out = 0;
    goto errors;
  }
  BN_bn2bin(bn, bin_out);
errors:
  BN_free(bn);
  return bin_out;
}


x86 assembly (clang, -O0):
<hex_to_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1030 <BN_hex2bn@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    118a <hex_to_bin+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <hex_to_bin+0x77>
mov    -0x20(%rbp),%rdi
call   1050 <BN_num_bytes@plt>
cmp    -0x18(%rbp),%rax
jle    11aa <hex_to_bin+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <hex_to_bin+0x77>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <BN_bn2bin@plt>
mov    -0x20(%rbp),%rdi
call   1040 <BN_free@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_to_bin>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rdi
call   1030 <BN_hex2bn@plt>
test   %eax,%eax
je     116c <hex_to_bin+0x2c>
mov    (%rsp),%rdi
call   1050 <BN_num_bytes@plt>
cmp    %r14,%rax
jle    1170 <hex_to_bin+0x30>
xor    %ebx,%ebx
jmp    117c <hex_to_bin+0x3c>
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1060 <BN_bn2bin@plt>
mov    (%rsp),%rdi
call   1040 <BN_free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @hex_to_bin(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32* null, i32** %7
store i32 0, i32* %8
%9 = load i8*, i8** %5
%10 = call i32 @BN_hex2bn(i32** %7, i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i8* null, i8** %4
br label %24
14: ; preds = %3
%15 = load i32*, i32** %7
%16 = call i64 @BN_num_bytes(i32* %15)
%17 = load i64, i64* %6
%18 = icmp sgt i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %14
store i8* null, i8** %4
br label %24
20: ; preds = %14
%21 = load i32*, i32** %7
%22 = load i8*, i8** %4
%23 = call i32 @BN_bn2bin(i32* %21, i8* %22)
br label %24
24: ; preds = %20, %19, %13
%25 = load i32*, i32** %7
%26 = call i32 @BN_free(i32* %25)
%27 = load i8*, i8** %4
ret i8* %27
}

LLVM IR (-O2):
define i8* @hex_to_bin(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32*
%5 = bitcast i32** %4 to i8*
store i32* null, i32** %4
%6 = call i32 @BN_hex2bn(i32** %4, i8* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = load i32*, i32** %4
%10 = call i64 @BN_num_bytes(i32* %9)
%11 = icmp sgt i64 %10, %2
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i32*, i32** %4
%14 = call i32 @BN_bn2bin(i32* %13, i8* %0)
br label %15
15: ; preds = %8, %3, %12
%16 = phi i8* [ %0, %12 ], [ null, %3 ], [ null, %8 ]
%17 = load i32*, i32** %4
%18 = call i32 @BN_free(i32* %17)
ret i8* %16
}