#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*/ EVP_PKEY;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
size_t BIO_read(int /*<<< orphan*/ *, unsigned char **, size_t);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ EVP_PKEY_get1_EC_KEY(int /*<<< orphan*/ *);
int i2d_EC_PUBKEY_bio(int /*<<< orphan*/ *, int /*<<< orphan*/);
size_t pkey_to_pub_der(EVP_PKEY *key, unsigned char **string, size_t len) {
  BIO *bo = NULL;
  size_t actual_len = 0;
  if ((bo = BIO_new(BIO_s_mem())) == NULL)
    return 0;
  if (i2d_EC_PUBKEY_bio(bo, EVP_PKEY_get1_EC_KEY(key)) == 1)
    actual_len = BIO_read(bo, string, len);
  BIO_free(bo);
  return actual_len;
}


x86 assembly (clang, -O0):
<pkey_to_pub_der>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x0,%al
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ad <pkey_to_pub_der+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11f8 <pkey_to_pub_der+0x98>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <EVP_PKEY_get1_EC_KEY@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1050 <i2d_EC_PUBKEY_bio@plt>
cmp    $0x1,%eax
jne    11e7 <pkey_to_pub_der+0x87>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <BIO_read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <BIO_free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_to_pub_der>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
xor    %r12d,%r12d
xor    %eax,%eax
call   1080 <BIO_s_mem@plt>
mov    %eax,%edi
call   1060 <BIO_new@plt>
test   %rax,%rax
je     11c0 <pkey_to_pub_der+0x60>
mov    %rax,%rbx
mov    %r13,%rdi
call   1040 <EVP_PKEY_get1_EC_KEY@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <i2d_EC_PUBKEY_bio@plt>
cmp    $0x1,%eax
jne    11b5 <pkey_to_pub_der+0x55>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <BIO_read@plt>
mov    %rax,%r12
jmp    11b8 <pkey_to_pub_der+0x58>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1070 <BIO_free@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @pkey_to_pub_der(i32* %0, i8** %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i32* %0, i32** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
store i32* null, i32** %8
store i64 0, i64* %9
%10 = call i32 (...) @BIO_s_mem()
%11 = call i32* @BIO_new(i32 %10)
store i32* %11, i32** %8
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i64 0, i64* %4
br label %29
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = load i32*, i32** %5
%17 = call i32 @EVP_PKEY_get1_EC_KEY(i32* %16)
%18 = call i32 @i2d_EC_PUBKEY_bio(i32* %15, i32 %17)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i32*, i32** %8
%22 = load i8**, i8*** %6
%23 = load i64, i64* %7
%24 = call i64 @BIO_read(i32* %21, i8** %22, i64 %23)
store i64 %24, i64* %9
br label %25
25: ; preds = %20, %14
%26 = load i32*, i32** %8
%27 = call i32 @BIO_free(i32* %26)
%28 = load i64, i64* %9
store i64 %28, i64* %4
br label %29
29: ; preds = %25, %13
%30 = load i64, i64* %4
ret i64 %30
}

LLVM IR (-O2):
define i64 @pkey_to_pub_der(i32* %0, i8** %1, i64 %2) {
%4 = tail call i32 (...) @BIO_s_mem()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %16, label %7
7: ; preds = %3
%8 = tail call i32 @EVP_PKEY_get1_EC_KEY(i32* %0)
%9 = tail call i32 @i2d_EC_PUBKEY_bio(i32* %5, i32 %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i64 @BIO_read(i32* %5, i8** %1, i64 %2)
br label %13
13: ; preds = %11, %7
%14 = phi i64 [ %12, %11 ], [ 0, %7 ]
%15 = tail call i32 @BIO_free(i32* %5)
br label %16
16: ; preds = %3, %13
%17 = phi i64 [ %14, %13 ], [ 0, %3 ]
ret i64 %17
}