#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*/);
int /*<<< orphan*/ BIO_s_mem();
size_t BIO_write(int /*<<< orphan*/ *, unsigned char const *, size_t);
int /*<<< orphan*/ PEM_read_bio_PUBKEY(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ **,
                                       int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
EVP_PKEY *pub_string_to_pkey(const unsigned char *string, size_t len) {
  EVP_PKEY *pub_key = NULL;
  BIO *bo = NULL;
  if ((bo = BIO_new(BIO_s_mem())) == NULL)
    return NULL;
  if (BIO_write(bo, string, len) == len)
    PEM_read_bio_PUBKEY(bo, &pub_key, NULL, NULL);
  BIO_free(bo);
  return pub_key;
}


x86 assembly (clang, -O0):
<pub_string_to_pkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1199 <pub_string_to_pkey+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <pub_string_to_pkey+0x89>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <BIO_write@plt>
cmp    -0x18(%rbp),%rax
jne    11c8 <pub_string_to_pkey+0x78>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <PEM_read_bio_PUBKEY@plt>
mov    -0x28(%rbp),%rdi
call   1050 <BIO_free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pub_string_to_pkey>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
movq   $0x0,(%rsp)
xor    %r15d,%r15d
xor    %eax,%eax
call   1070 <BIO_s_mem@plt>
mov    %eax,%edi
call   1040 <BIO_new@plt>
test   %rax,%rax
je     11ad <pub_string_to_pkey+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <BIO_write@plt>
cmp    %r14,%rax
jne    11a1 <pub_string_to_pkey+0x51>
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <PEM_read_bio_PUBKEY@plt>
mov    %rbx,%rdi
call   1050 <BIO_free@plt>
mov    (%rsp),%r15
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @pub_string_to_pkey(i8* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* null, i32** %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 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32* null, i32** %3
br label %26
12: ; preds = %2
%13 = load i32*, i32** %7
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = call i64 @BIO_write(i32* %13, i8* %14, i64 %15)
%17 = load i64, i64* %5
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32*, i32** %7
%21 = call i32 @PEM_read_bio_PUBKEY(i32* %20, i32** %6, i32* null, i32* null)
br label %22
22: ; preds = %19, %12
%23 = load i32*, i32** %7
%24 = call i32 @BIO_free(i32* %23)
%25 = load i32*, i32** %6
store i32* %25, i32** %3
br label %26
26: ; preds = %22, %11
%27 = load i32*, i32** %3
ret i32* %27
}

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