#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 PEM_write_bio_PUBKEY(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
size_t pkey_to_pub_string(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 (PEM_write_bio_PUBKEY(bo, key) == 1)
    actual_len = BIO_read(bo, string, len);
  BIO_free(bo);
  return actual_len;
}


x86 assembly (clang, -O0):
<pkey_to_pub_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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   1060 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    119d <pkey_to_pub_string+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <pkey_to_pub_string+0x89>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <PEM_write_bio_PUBKEY@plt>
cmp    $0x1,%eax
jne    11c8 <pkey_to_pub_string+0x78>
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   1050 <BIO_free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkey_to_pub_string>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
xor    %r15d,%r15d
xor    %eax,%eax
call   1060 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
test   %rax,%rax
je     11a9 <pkey_to_pub_string+0x59>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
call   1070 <PEM_write_bio_PUBKEY@plt>
cmp    $0x1,%eax
jne    119e <pkey_to_pub_string+0x4e>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1030 <BIO_read@plt>
mov    %rax,%r15
jmp    11a1 <pkey_to_pub_string+0x51>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <BIO_free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @pkey_to_pub_string(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 %28
14: ; preds = %3
%15 = load i32*, i32** %8
%16 = load i32*, i32** %5
%17 = call i32 @PEM_write_bio_PUBKEY(i32* %15, i32* %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32*, i32** %8
%21 = load i8**, i8*** %6
%22 = load i64, i64* %7
%23 = call i64 @BIO_read(i32* %20, i8** %21, i64 %22)
store i64 %23, i64* %9
br label %24
24: ; preds = %19, %14
%25 = load i32*, i32** %8
%26 = call i32 @BIO_free(i32* %25)
%27 = load i64, i64* %9
store i64 %27, i64* %4
br label %28
28: ; preds = %24, %13
%29 = load i64, i64* %4
ret i64 %29
}

LLVM IR (-O2):
define i64 @pkey_to_pub_string(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 %15, label %7
7: ; preds = %3
%8 = tail call i32 @PEM_write_bio_PUBKEY(i32* %5, i32* %0)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i64 @BIO_read(i32* %5, i8** %1, i64 %2)
br label %12
12: ; preds = %10, %7
%13 = phi i64 [ %11, %10 ], [ 0, %7 ]
%14 = tail call i32 @BIO_free(i32* %5)
br label %15
15: ; preds = %3, %12
%16 = phi i64 [ %13, %12 ], [ 0, %3 ]
ret i64 %16
}