#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  scalar_t__ data;
  int /*<<< orphan*/ length;
};
typedef TYPE_1__ BUF_MEM;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ BN_bn2bin(int /*<<< orphan*/ const *, unsigned char *);
int /*<<< orphan*/ BN_num_bytes(int /*<<< orphan*/ const *);
TYPE_1__ *BUF_MEM_create(int /*<<< orphan*/);
BUF_MEM *BN_bn2buf(const BIGNUM *bn) {
  BUF_MEM *out;
  if (!bn)
    return NULL;
  out = BUF_MEM_create(BN_num_bytes(bn));
  if (!out)
    return NULL;
  out->length = BN_bn2bin(bn, (unsigned char *)out->data);
  return out;
}


x86 assembly (clang, -O0):
<BN_bn2buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <BN_bn2buf+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <BN_bn2buf+0x71>
mov    -0x10(%rbp),%rdi
call   1040 <BN_num_bytes@plt>
mov    %eax,%edi
call   1030 <BUF_MEM_create@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1180 <BN_bn2buf+0x50>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <BN_bn2buf+0x71>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1050 <BN_bn2bin@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BN_bn2buf>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1160 <BN_bn2buf+0x30>
mov    %rdi,%r14
call   1040 <BN_num_bytes@plt>
mov    %eax,%edi
call   1030 <BUF_MEM_create@plt>
test   %rax,%rax
je     1160 <BN_bn2buf+0x30>
mov    %rax,%rbx
mov    (%rax),%rsi
mov    %r14,%rdi
call   1050 <BN_bn2bin@plt>
mov    %eax,0x8(%rbx)
jmp    1162 <BN_bn2buf+0x32>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i64, i32 }
define %struct.TYPE_4__* @BN_bn2buf(i32* %0) {
%2 = alloca %struct.TYPE_4__*
%3 = alloca i32*
%4 = alloca %struct.TYPE_4__*
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store %struct.TYPE_4__* null, %struct.TYPE_4__** %2
br label %25
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = call i32 @BN_num_bytes(i32* %9)
%11 = call %struct.TYPE_4__* @BUF_MEM_create(i32 %10)
store %struct.TYPE_4__* %11, %struct.TYPE_4__** %4
%12 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%13 = icmp ne %struct.TYPE_4__* %12, null
br i1 %13, label %15, label %14
14: ; preds = %8
store %struct.TYPE_4__* null, %struct.TYPE_4__** %2
br label %25
15: ; preds = %8
%16 = load i32*, i32** %3
%17 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%18 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i8*
%21 = call i32 @BN_bn2bin(i32* %16, i8* %20)
%22 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i32 0, i32 1
store i32 %21, i32* %23
%24 = load %struct.TYPE_4__*, %struct.TYPE_4__** %4
store %struct.TYPE_4__* %24, %struct.TYPE_4__** %2
br label %25
25: ; preds = %15, %14, %7
%26 = load %struct.TYPE_4__*, %struct.TYPE_4__** %2
ret %struct.TYPE_4__* %26
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i64, i32 }
define %struct.TYPE_4__* @BN_bn2buf(i32* %0) {
%2 = icmp eq i32* %0, null
br i1 %2, label %13, label %3
3: ; preds = %1
%4 = tail call i32 @BN_num_bytes(i32* %0)
%5 = tail call %struct.TYPE_4__* @BUF_MEM_create(i32 %4)
%6 = icmp eq %struct.TYPE_4__* %5, null
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %5, i64 0, i32 0
%9 = load i64, i64* %8
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @BN_bn2bin(i32* %0, i8* %10)
%12 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %5, i64 0, i32 1
store i32 %11, i32* %12
br label %13
13: ; preds = %3, %1, %7
%14 = phi %struct.TYPE_4__* [ %5, %7 ], [ null, %1 ], [ null, %3 ]
ret %struct.TYPE_4__* %14
}