#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_NOCLOSE;
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
long BIO_get_mem_data(int /*<<< orphan*/ *, char **);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_mem();
int /*<<< orphan*/ BIO_set_close(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/
PEM_write_bio_PrivateKey(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                         int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/ *);
void pki_write_privatekey_in_mem(EVP_PKEY *privatekey, char **privatekey_ptr,
                                 long *size) {
  BIO *bio_mem = NULL;
  bio_mem = BIO_new(BIO_s_mem());
  PEM_write_bio_PrivateKey(bio_mem, privatekey, NULL, NULL, 0, 0, NULL);
  *size = BIO_get_mem_data(bio_mem, privatekey_ptr);
  *(*privatekey_ptr + *size) = '\0';
  BIO_set_close(bio_mem, BIO_NOCLOSE);
  BIO_free(bio_mem);
}


x86 assembly (clang, -O0):
<pki_write_privatekey_in_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
xor    %r9d,%r9d
mov    %rcx,%rdx
mov    %r9d,%r8d
movq   $0x0,(%rsp)
call   1060 <PEM_write_bio_PrivateKey@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <BIO_get_mem_data@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
call   1030 <BIO_set_close@plt>
mov    -0x20(%rbp),%rdi
call   1050 <BIO_free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pki_write_privatekey_in_mem>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
xor    %eax,%eax
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,%rbx
movq   $0x0,(%rsp)
mov    %rax,%rdi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <PEM_write_bio_PrivateKey@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <BIO_get_mem_data@plt>
mov    %rax,(%r14)
mov    (%r12),%rcx
movb   $0x0,(%rcx,%rax,1)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <BIO_set_close@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <BIO_free@plt>

LLVM IR (-O0):
@BIO_NOCLOSE = global i32 0
define void @pki_write_privatekey_in_mem(i32* %0, i8** %1, i64* %2) {
%4 = alloca i32*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32*
store i32* %0, i32** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
store i32* null, i32** %7
%8 = call i32 (...) @BIO_s_mem()
%9 = call i32* @BIO_new(i32 %8)
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = load i32*, i32** %4
%12 = call i32 @PEM_write_bio_PrivateKey(i32* %10, i32* %11, i32* null, i32* null, i32 0, i32 0, i32* null)
%13 = load i32*, i32** %7
%14 = load i8**, i8*** %5
%15 = call i64 @BIO_get_mem_data(i32* %13, i8** %14)
%16 = load i64*, i64** %6
store i64 %15, i64* %16
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = load i64*, i64** %6
%20 = load i64, i64* %19
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
%22 = load i32*, i32** %7
%23 = load i32, i32* @BIO_NOCLOSE
%24 = call i32 @BIO_set_close(i32* %22, i32 %23)
%25 = load i32*, i32** %7
%26 = call i32 @BIO_free(i32* %25)
ret void
}

LLVM IR (-O2):
@BIO_NOCLOSE = global i32 0
define void @pki_write_privatekey_in_mem(i32* %0, i8** %1, i64* nocapture writeonly %2) {
%4 = tail call i32 (...) @BIO_s_mem()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = tail call i32 @PEM_write_bio_PrivateKey(i32* %5, i32* %0, i32* null, i32* null, i32 0, i32 0, i32* null)
%7 = tail call i64 @BIO_get_mem_data(i32* %5, i8** %1)
store i64 %7, i64* %2
%8 = load i8*, i8** %1
%9 = getelementptr inbounds i8, i8* %8, i64 %7
store i8 0, i8* %9
%10 = load i32, i32* @BIO_NOCLOSE
%11 = tail call i32 @BIO_set_close(i32* %5, i32 %10)
%12 = tail call i32 @BIO_free(i32* %5)
ret void
}