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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new(int /*<<< orphan*/);
int /*<<< orphan*/ BIO_s_mem();
scalar_t__ PEM_read_bio_RSAPublicKey(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ **,
                                     int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
scalar_t__ PEM_write_bio_RSAPublicKey(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *RSA_new();
RSA *rsa_extract_public_key(RSA *privkey) {
  BIO *rsa_pub_bio = NULL;
  RSA *pubkey = NULL;
  rsa_pub_bio = BIO_new(BIO_s_mem());
  if (PEM_write_bio_RSAPublicKey(rsa_pub_bio, privkey) < 0) {
    BIO_free(rsa_pub_bio);
    return NULL;
  }
  pubkey = RSA_new();
  if (PEM_read_bio_RSAPublicKey(rsa_pub_bio, &pubkey, NULL, NULL) < 0) {
    BIO_free(rsa_pub_bio);
    RSA_free(pubkey);
    return NULL;
  }
  BIO_free(rsa_pub_bio);
  return pubkey;
}


x86 assembly (clang, -O0):
<rsa_extract_public_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1090 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <PEM_write_bio_RSAPublicKey@plt>
cmp    $0x0,%rax
jge    11cb <rsa_extract_public_key+0x5b>
mov    -0x18(%rbp),%rdi
call   1060 <BIO_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1224 <rsa_extract_public_key+0xb4>
mov    $0x0,%al
call   1070 <RSA_new@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <PEM_read_bio_RSAPublicKey@plt>
cmp    $0x0,%rax
jge    1213 <rsa_extract_public_key+0xa3>
mov    -0x18(%rbp),%rdi
call   1060 <BIO_free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <RSA_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1224 <rsa_extract_public_key+0xb4>
mov    -0x18(%rbp),%rdi
call   1060 <BIO_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_extract_public_key>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1090 <BIO_s_mem@plt>
mov    %eax,%edi
call   1050 <BIO_new@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <PEM_write_bio_RSAPublicKey@plt>
test   %rax,%rax
js     11d0 <rsa_extract_public_key+0x60>
xor    %eax,%eax
call   1070 <RSA_new@plt>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <PEM_read_bio_RSAPublicKey@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1060 <BIO_free@plt>
mov    (%rsp),%rax
test   %r14,%r14
jns    11da <rsa_extract_public_key+0x6a>
mov    %rax,%rdi
call   1040 <RSA_free@plt>
jmp    11d8 <rsa_extract_public_key+0x68>
mov    %rbx,%rdi
call   1060 <BIO_free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32* @rsa_extract_public_key(i32* %0) {
%2 = alloca i32*
%3 = bitcast i32** %2 to i8*
%4 = tail call i32 (...) @BIO_s_mem()
%5 = tail call i32* @BIO_new(i32 %4)
%6 = tail call i64 @PEM_write_bio_RSAPublicKey(i32* %5, i32* %0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @BIO_free(i32* %5)
br label %18
10: ; preds = %1
%11 = tail call i32* (...) @RSA_new()
store i32* %11, i32** %2
%12 = call i64 @PEM_read_bio_RSAPublicKey(i32* %5, i32** %2, i32* null, i32* null)
%13 = icmp slt i64 %12, 0
%14 = call i32 @BIO_free(i32* %5)
%15 = load i32*, i32** %2
br i1 %13, label %16, label %18
16: ; preds = %10
%17 = call i32 @RSA_free(i32* %15)
br label %18
18: ; preds = %10, %16, %8
%19 = phi i32* [ null, %8 ], [ null, %16 ], [ %15, %10 ]
ret i32* %19
}