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

/* Variables and functions */
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_PUBKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *X509_PUBKEY_get(int /*<<< orphan*/ *);
int /*<<< orphan*/ *d2i_X509_PUBKEY(int /*<<< orphan*/ *,
                                    unsigned char const **, long);
EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) {
  X509_PUBKEY *xpk;
  EVP_PKEY *pktmp;
  xpk = d2i_X509_PUBKEY(NULL, pp, length);
  if (!xpk)
    return NULL;
  pktmp = X509_PUBKEY_get(xpk);
  X509_PUBKEY_free(xpk);
  if (!pktmp)
    return NULL;
  if (a) {
    EVP_PKEY_free(*a);
    *a = pktmp;
  }
  return pktmp;
}


x86 assembly (clang, -O0):
<d2i_PUBKEY>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   1060 <d2i_X509_PUBKEY@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <d2i_PUBKEY+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <d2i_PUBKEY+0x99>
mov    -0x28(%rbp),%rdi
call   1030 <X509_PUBKEY_get@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <X509_PUBKEY_free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11af <d2i_PUBKEY+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <d2i_PUBKEY+0x99>
cmpq   $0x0,-0x10(%rbp)
je     11d1 <d2i_PUBKEY+0x91>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <EVP_PKEY_free@plt>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d2i_PUBKEY>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %r15d,%r15d
xor    %edi,%edi
call   1060 <d2i_X509_PUBKEY@plt>
test   %rax,%rax
je     1187 <d2i_PUBKEY+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <X509_PUBKEY_get@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <X509_PUBKEY_free@plt>
test   %r15,%r15
je     1184 <d2i_PUBKEY+0x44>
test   %r14,%r14
je     1187 <d2i_PUBKEY+0x47>
mov    (%r14),%rdi
call   1040 <EVP_PKEY_free@plt>
mov    %r15,(%r14)
jmp    1187 <d2i_PUBKEY+0x47>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @d2i_PUBKEY(i32** %0, i8** %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32**
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
store i32** %0, i32*** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
%10 = load i8**, i8*** %6
%11 = load i64, i64* %7
%12 = call i32* @d2i_X509_PUBKEY(i32* null, i8** %10, i64 %11)
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32* null, i32** %4
br label %35
16: ; preds = %3
%17 = load i32*, i32** %8
%18 = call i32* @X509_PUBKEY_get(i32* %17)
store i32* %18, i32** %9
%19 = load i32*, i32** %8
%20 = call i32 @X509_PUBKEY_free(i32* %19)
%21 = load i32*, i32** %9
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %16
store i32* null, i32** %4
br label %35
24: ; preds = %16
%25 = load i32**, i32*** %5
%26 = icmp ne i32** %25, null
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32**, i32*** %5
%29 = load i32*, i32** %28
%30 = call i32 @EVP_PKEY_free(i32* %29)
%31 = load i32*, i32** %9
%32 = load i32**, i32*** %5
store i32* %31, i32** %32
br label %33
33: ; preds = %27, %24
%34 = load i32*, i32** %9
store i32* %34, i32** %4
br label %35
35: ; preds = %33, %23, %15
%36 = load i32*, i32** %4
ret i32* %36
}

LLVM IR (-O2):
define i32* @d2i_PUBKEY(i32** %0, i8** %1, i64 %2) {
%4 = tail call i32* @d2i_X509_PUBKEY(i32* null, i8** %1, i64 %2)
%5 = icmp eq i32* %4, null
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = tail call i32* @X509_PUBKEY_get(i32* %4)
%8 = tail call i32 @X509_PUBKEY_free(i32* %4)
%9 = icmp eq i32* %7, null
br i1 %9, label %15, label %10
10: ; preds = %6
%11 = icmp eq i32** %0, null
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = load i32*, i32** %0
%14 = tail call i32 @EVP_PKEY_free(i32* %13)
store i32* %7, i32** %0
br label %15
15: ; preds = %10, %12, %6, %3
%16 = phi i32* [ null, %3 ], [ null, %6 ], [ %7, %12 ], [ %7, %10 ]
ret i32* %16
}