#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;

/* Variables and functions */
int /*<<< orphan*/ ASN1_TYPE;
int /*<<< orphan*/ ASN1_TYPE_free;
int EVP_PKEY_DSA;
int EVP_PKEY_EC;
int EVP_PKEY_RSA;
int STACK_OF(int /*<<< orphan*/);
int /*<<< orphan*/ V_ASN1_SEQUENCE;
int /*<<< orphan*/ V_ASN1_UNIVERSAL;
int d2i_ASN1_SET_OF_ASN1_TYPE(int /*<<< orphan*/ *, unsigned char const **,
                              long, int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ d2i_ASN1_TYPE;
int /*<<< orphan*/ *d2i_PrivateKey(int, int /*<<< orphan*/ **,
                                   unsigned char const **, long);
int inkey;
int sk_ASN1_TYPE_num(int);
int /*<<< orphan*/ sk_ASN1_TYPE_pop_free(int, int /*<<< orphan*/);
EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **a, const unsigned char **pp,
                             long length) {
  STACK_OF(ASN1_TYPE) * inkey;
  const unsigned char *p;
  int keytype;
  p = *pp;
  inkey =
      d2i_ASN1_SET_OF_ASN1_TYPE(NULL, &p, length, d2i_ASN1_TYPE, ASN1_TYPE_free,
                                V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
  if (sk_ASN1_TYPE_num(inkey) == 6)
    keytype = EVP_PKEY_DSA;
  else if (sk_ASN1_TYPE_num(inkey) == 4)
    keytype = EVP_PKEY_EC;
  else
    keytype = EVP_PKEY_RSA;
  sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
  return d2i_PrivateKey(keytype, a, pp, length);
}


x86 assembly (clang, -O0):
<d2i_AutoPrivateKey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1030 <STACK_OF@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
mov    0x2e17(%rip),%rax
mov    (%rax),%r8d
mov    0x2dfd(%rip),%rax
mov    (%rax),%r9d
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
xor    %esi,%esi
mov    %esi,%edi
lea    -0x20(%rbp),%rsi
mov    %eax,(%rsp)
call   1070 <d2i_ASN1_SET_OF_ASN1_TYPE@plt>
mov    %eax,%ecx
mov    0x2e10(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1040 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    11ea <d2i_AutoPrivateKey+0x9a>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1223 <d2i_AutoPrivateKey+0xd3>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1040 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    1212 <d2i_AutoPrivateKey+0xc2>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    121e <d2i_AutoPrivateKey+0xce>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1223 <d2i_AutoPrivateKey+0xd3>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
call   1060 <sk_ASN1_TYPE_pop_free@plt>
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1050 <d2i_PrivateKey@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d2i_AutoPrivateKey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1030 <STACK_OF@plt>
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1b(%rip),%r12
mov    (%r12),%r8d
mov    0x2e00(%rip),%rax
mov    (%rax),%r9d
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    %r14,%rdx
call   1070 <d2i_ASN1_SET_OF_ASN1_TYPE@plt>
mov    0x2e13(%rip),%r13
mov    %eax,0x0(%r13)
mov    %eax,%edi
call   1040 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    11d6 <d2i_AutoPrivateKey+0x86>
mov    0x2e24(%rip),%rax
jmp    11f4 <d2i_AutoPrivateKey+0xa4>
mov    0x0(%r13),%edi
call   1040 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    11ed <d2i_AutoPrivateKey+0x9d>
mov    0x2dd5(%rip),%rax
jmp    11f4 <d2i_AutoPrivateKey+0xa4>
mov    0x2dd4(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%edi
mov    (%r12),%esi
call   1060 <sk_ASN1_TYPE_pop_free@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <d2i_PrivateKey@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ASN1_TYPE = global i32 0
@inkey = global i32 0
@d2i_ASN1_TYPE = global i32 0
@ASN1_TYPE_free = global i32 0
@V_ASN1_SEQUENCE = global i32 0
@V_ASN1_UNIVERSAL = global i32 0
@EVP_PKEY_DSA = global i32 0
@EVP_PKEY_EC = global i32 0
@EVP_PKEY_RSA = global i32 0
define i32* @d2i_AutoPrivateKey(i32** %0, i8** %1, i64 %2) {
%4 = alloca i32**
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32** %0, i32*** %4
store i8** %1, i8*** %5
store i64 %2, i64* %6
%9 = load i32, i32* @ASN1_TYPE
%10 = call i32 @STACK_OF(i32 %9)
%11 = load i32, i32* @inkey
%12 = mul nsw i32 %10, %11
%13 = load i8**, i8*** %5
%14 = load i8*, i8** %13
store i8* %14, i8** %7
%15 = load i64, i64* %6
%16 = load i32, i32* @d2i_ASN1_TYPE
%17 = load i32, i32* @ASN1_TYPE_free
%18 = load i32, i32* @V_ASN1_SEQUENCE
%19 = load i32, i32* @V_ASN1_UNIVERSAL
%20 = call i32 @d2i_ASN1_SET_OF_ASN1_TYPE(i32* null, i8** %7, i64 %15, i32 %16, i32 %17, i32 %18, i32 %19)
store i32 %20, i32* @inkey
%21 = load i32, i32* @inkey
%22 = call i32 @sk_ASN1_TYPE_num(i32 %21)
%23 = icmp eq i32 %22, 6
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = load i32, i32* @EVP_PKEY_DSA
store i32 %25, i32* %8
br label %35
26: ; preds = %3
%27 = load i32, i32* @inkey
%28 = call i32 @sk_ASN1_TYPE_num(i32 %27)
%29 = icmp eq i32 %28, 4
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i32, i32* @EVP_PKEY_EC
store i32 %31, i32* %8
br label %34
32: ; preds = %26
%33 = load i32, i32* @EVP_PKEY_RSA
store i32 %33, i32* %8
br label %34
34: ; preds = %32, %30
br label %35
35: ; preds = %34, %24
%36 = load i32, i32* @inkey
%37 = load i32, i32* @ASN1_TYPE_free
%38 = call i32 @sk_ASN1_TYPE_pop_free(i32 %36, i32 %37)
%39 = load i32, i32* %8
%40 = load i32**, i32*** %4
%41 = load i8**, i8*** %5
%42 = load i64, i64* %6
%43 = call i32* @d2i_PrivateKey(i32 %39, i32** %40, i8** %41, i64 %42)
ret i32* %43
}

LLVM IR (-O2):
@ASN1_TYPE = global i32 0
@inkey = global i32 0
@d2i_ASN1_TYPE = global i32 0
@ASN1_TYPE_free = global i32 0
@V_ASN1_SEQUENCE = global i32 0
@V_ASN1_UNIVERSAL = global i32 0
@EVP_PKEY_DSA = global i32 0
@EVP_PKEY_EC = global i32 0
@EVP_PKEY_RSA = global i32 0
define i32* @d2i_AutoPrivateKey(i32** %0, i8** %1, i64 %2) {
%4 = alloca i8*
%5 = load i32, i32* @ASN1_TYPE
%6 = tail call i32 @STACK_OF(i32 %5)
%7 = bitcast i8** %4 to i8*
%8 = load i8*, i8** %1
store i8* %8, i8** %4
%9 = load i32, i32* @d2i_ASN1_TYPE
%10 = load i32, i32* @ASN1_TYPE_free
%11 = load i32, i32* @V_ASN1_SEQUENCE
%12 = load i32, i32* @V_ASN1_UNIVERSAL
%13 = call i32 @d2i_ASN1_SET_OF_ASN1_TYPE(i32* null, i8** %4, i64 %2, i32 %9, i32 %10, i32 %11, i32 %12)
store i32 %13, i32* @inkey
%14 = call i32 @sk_ASN1_TYPE_num(i32 %13)
%15 = icmp eq i32 %14, 6
br i1 %15, label %21, label %16
16: ; preds = %3
%17 = load i32, i32* @inkey
%18 = call i32 @sk_ASN1_TYPE_num(i32 %17)
%19 = icmp eq i32 %18, 4
%20 = select i1 %19, i32* @EVP_PKEY_EC, i32* @EVP_PKEY_RSA
br label %21
21: ; preds = %16, %3
%22 = phi i32* [ @EVP_PKEY_DSA, %3 ], [ %20, %16 ]
%23 = load i32, i32* %22
%24 = load i32, i32* @inkey
%25 = load i32, i32* @ASN1_TYPE_free
%26 = call i32 @sk_ASN1_TYPE_pop_free(i32 %24, i32 %25)
%27 = call i32* @d2i_PrivateKey(i32 %23, i32** %0, i8** %1, i64 %2)
ret i32* %27
}