#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 /*<<< orphan*/ EC_KEY;

/* Variables and functions */
int /*<<< orphan*/ EC_F_EC_KEY_PRIV2BUF;
size_t EC_KEY_priv2oct(int /*<<< orphan*/ const *, unsigned char *, size_t);
int /*<<< orphan*/ ECerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
unsigned char *OPENSSL_malloc(size_t);
size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf) {
  size_t len;
  unsigned char *buf;
  len = EC_KEY_priv2oct(eckey, NULL, 0);
  if (len == 0)
    return 0;
  if ((buf = OPENSSL_malloc(len)) == NULL) {
    ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
    return 0;
  }
  len = EC_KEY_priv2oct(eckey, buf, len);
  if (len == 0) {
    OPENSSL_free(buf);
    return 0;
  }
  *pbuf = buf;
  return len;
}


x86 assembly (clang, -O0):
<EC_KEY_priv2buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <EC_KEY_priv2oct@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117c <EC_KEY_priv2buf+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    1200 <EC_KEY_priv2buf+0xc0>
mov    -0x20(%rbp),%rdi
call   1030 <OPENSSL_malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11b7 <EC_KEY_priv2buf+0x77>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1040 <ECerr@plt>
movq   $0x0,-0x8(%rbp)
jmp    1200 <EC_KEY_priv2buf+0xc0>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <EC_KEY_priv2oct@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ed <EC_KEY_priv2buf+0xad>
mov    -0x28(%rbp),%rdi
call   1050 <OPENSSL_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1200 <EC_KEY_priv2buf+0xc0>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<EC_KEY_priv2buf>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %r13d,%r13d
xor    %esi,%esi
xor    %edx,%edx
call   1060 <EC_KEY_priv2oct@plt>
test   %rax,%rax
je     11b2 <EC_KEY_priv2buf+0x72>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <OPENSSL_malloc@plt>
test   %rax,%rax
je     118e <EC_KEY_priv2buf+0x4e>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1060 <EC_KEY_priv2oct@plt>
test   %rax,%rax
je     11a7 <EC_KEY_priv2buf+0x67>
mov    %rax,%r13
mov    %r12,(%r14)
jmp    11b2 <EC_KEY_priv2buf+0x72>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1040 <ECerr@plt>
jmp    11af <EC_KEY_priv2buf+0x6f>
mov    %r12,%rdi
call   1050 <OPENSSL_free@plt>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EC_F_EC_KEY_PRIV2BUF = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i64 @EC_KEY_priv2buf(i32* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32* %0, i32** %4
store i8** %1, i8*** %5
%8 = load i32*, i32** %4
%9 = call i64 @EC_KEY_priv2oct(i32* %8, i8* null, i64 0)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i64 0, i64* %3
br label %35
13: ; preds = %2
%14 = load i64, i64* %6
%15 = call i8* @OPENSSL_malloc(i64 %14)
store i8* %15, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* @EC_F_EC_KEY_PRIV2BUF
%19 = load i32, i32* @ERR_R_MALLOC_FAILURE
%20 = call i32 @ECerr(i32 %18, i32 %19)
store i64 0, i64* %3
br label %35
21: ; preds = %13
%22 = load i32*, i32** %4
%23 = load i8*, i8** %7
%24 = load i64, i64* %6
%25 = call i64 @EC_KEY_priv2oct(i32* %22, i8* %23, i64 %24)
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i8*, i8** %7
%30 = call i32 @OPENSSL_free(i8* %29)
store i64 0, i64* %3
br label %35
31: ; preds = %21
%32 = load i8*, i8** %7
%33 = load i8**, i8*** %5
store i8* %32, i8** %33
%34 = load i64, i64* %6
store i64 %34, i64* %3
br label %35
35: ; preds = %31, %28, %17, %12
%36 = load i64, i64* %3
ret i64 %36
}

LLVM IR (-O2):
@EC_F_EC_KEY_PRIV2BUF = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
define i64 @EC_KEY_priv2buf(i32* %0, i8** nocapture writeonly %1) {
%3 = tail call i64 @EC_KEY_priv2oct(i32* %0, i8* null, i64 0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = tail call i8* @OPENSSL_malloc(i64 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @EC_F_EC_KEY_PRIV2BUF
%10 = load i32, i32* @ERR_R_MALLOC_FAILURE
%11 = tail call i32 @ECerr(i32 %9, i32 %10)
br label %18
12: ; preds = %5
%13 = tail call i64 @EC_KEY_priv2oct(i32* %0, i8* %6, i64 %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @OPENSSL_free(i8* %6)
br label %18
17: ; preds = %12
store i8* %6, i8** %1
br label %18
18: ; preds = %2, %17, %15, %8
%19 = phi i64 [ 0, %8 ], [ 0, %15 ], [ %13, %17 ], [ 0, %2 ]
ret i64 %19
}