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

/* Variables and functions */
int /*<<< orphan*/ ASN1_F_D2I_AUTOPRIVATEKEY;
int /*<<< orphan*/ ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE;
int /*<<< orphan*/ ASN1_TYPE;
int /*<<< orphan*/ ASN1_TYPE_free;
int /*<<< orphan*/ ASN1err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *EVP_PKCS82PKEY(int /*<<< orphan*/ *);
int EVP_PKEY_DSA;
int EVP_PKEY_EC;
int EVP_PKEY_RSA;
int /*<<< orphan*/ PKCS8_PRIV_KEY_INFO_free(int /*<<< orphan*/ *);
int STACK_OF(int /*<<< orphan*/);
int d2i_ASN1_SEQUENCE_ANY(int /*<<< orphan*/ *, unsigned char const **, long);
int /*<<< orphan*/ *d2i_PKCS8_PRIV_KEY_INFO(int /*<<< orphan*/ *,
                                            unsigned char const **, long);
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_SEQUENCE_ANY(NULL, &p, length);
  if (sk_ASN1_TYPE_num(inkey) == 6)
    keytype = EVP_PKEY_DSA;
  else if (sk_ASN1_TYPE_num(inkey) == 4)
    keytype = EVP_PKEY_EC;
  else if (sk_ASN1_TYPE_num(inkey) == 3) {
    PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, pp, length);
    EVP_PKEY *ret;
    sk_ASN1_TYPE_pop_free(inkey, ASN1_TYPE_free);
    if (!p8) {
      ASN1err(ASN1_F_D2I_AUTOPRIVATEKEY, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
      return NULL;
    }
    ret = EVP_PKCS82PKEY(p8);
    PKCS8_PRIV_KEY_INFO_free(p8);
    if (a) {
      *a = ret;
    }
    return ret;
  } 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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1030 <STACK_OF@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10a0 <d2i_ASN1_SEQUENCE_ANY@plt>
mov    %eax,%ecx
mov    0x2e01(%rip),%rax
mov    %ecx,(%rax)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    1201 <d2i_AutoPrivateKey+0x71>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12ea <d2i_AutoPrivateKey+0x15a>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    1229 <d2i_AutoPrivateKey+0x99>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12e5 <d2i_AutoPrivateKey+0x155>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x3,%eax
jne    12d4 <d2i_AutoPrivateKey+0x144>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <d2i_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
cmpq   $0x0,-0x38(%rbp)
jne    129b <d2i_AutoPrivateKey+0x10b>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movq   $0x0,-0x8(%rbp)
jmp    1319 <d2i_AutoPrivateKey+0x189>
mov    -0x38(%rbp),%rdi
call   1070 <EVP_PKCS82PKEY@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <PKCS8_PRIV_KEY_INFO_free@plt>
cmpq   $0x0,-0x10(%rbp)
je     12c7 <d2i_AutoPrivateKey+0x137>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1319 <d2i_AutoPrivateKey+0x189>
mov    0x2cf5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12e5 <d2i_AutoPrivateKey+0x155>
jmp    12ea <d2i_AutoPrivateKey+0x15a>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <d2i_PrivateKey@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d2i_AutoPrivateKey>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1030 <STACK_OF@plt>
mov    (%rbx),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   10a0 <d2i_ASN1_SEQUENCE_ANY@plt>
mov    0x2e07(%rip),%r12
mov    %eax,(%r12)
mov    %eax,%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    11ea <d2i_AutoPrivateKey+0x5a>
mov    0x2e10(%rip),%rax
jmp    125a <d2i_AutoPrivateKey+0xca>
mov    (%r12),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    1201 <d2i_AutoPrivateKey+0x71>
mov    0x2dc1(%rip),%rax
jmp    125a <d2i_AutoPrivateKey+0xca>
mov    (%r12),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x3,%eax
jne    1253 <d2i_AutoPrivateKey+0xc3>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10b0 <d2i_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,%r15
mov    (%r12),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
test   %r15,%r15
je     1291 <d2i_AutoPrivateKey+0x101>
mov    %r15,%rdi
call   1070 <EVP_PKCS82PKEY@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <PKCS8_PRIV_KEY_INFO_free@plt>
test   %r14,%r14
je     1281 <d2i_AutoPrivateKey+0xf1>
mov    %rbx,(%r14)
jmp    1281 <d2i_AutoPrivateKey+0xf1>
mov    0x2d76(%rip),%rax
mov    (%rax),%ebp
mov    (%r12),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1060 <d2i_PrivateKey@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
xor    %ebx,%ebx
jmp    1281 <d2i_AutoPrivateKey+0xf1>

LLVM IR (-O0):
@ASN1_TYPE = global i32 0
@inkey = global i32 0
@EVP_PKEY_DSA = global i32 0
@EVP_PKEY_EC = global i32 0
@ASN1_TYPE_free = global i32 0
@ASN1_F_D2I_AUTOPRIVATEKEY = global i32 0
@ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE = global i32 0
@EVP_PKEY_RSA = global i32 0
define i32* @d2i_AutoPrivateKey(i32** %0, i8** %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32**
%6 = alloca i8**
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
store i32** %0, i32*** %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
%12 = load i32, i32* @ASN1_TYPE
%13 = call i32 @STACK_OF(i32 %12)
%14 = load i32, i32* @inkey
%15 = mul nsw i32 %13, %14
%16 = load i8**, i8*** %6
%17 = load i8*, i8** %16
store i8* %17, i8** %8
%18 = load i64, i64* %7
%19 = call i32 @d2i_ASN1_SEQUENCE_ANY(i32* null, i8** %8, i64 %18)
store i32 %19, i32* @inkey
%20 = load i32, i32* @inkey
%21 = call i32 @sk_ASN1_TYPE_num(i32 %20)
%22 = icmp eq i32 %21, 6
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = load i32, i32* @EVP_PKEY_DSA
store i32 %24, i32* %9
br label %64
25: ; preds = %3
%26 = load i32, i32* @inkey
%27 = call i32 @sk_ASN1_TYPE_num(i32 %26)
%28 = icmp eq i32 %27, 4
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* @EVP_PKEY_EC
store i32 %30, i32* %9
br label %63
31: ; preds = %25
%32 = load i32, i32* @inkey
%33 = call i32 @sk_ASN1_TYPE_num(i32 %32)
%34 = icmp eq i32 %33, 3
br i1 %34, label %35, label %60
35: ; preds = %31
%36 = load i8**, i8*** %6
%37 = load i64, i64* %7
%38 = call i32* @d2i_PKCS8_PRIV_KEY_INFO(i32* null, i8** %36, i64 %37)
store i32* %38, i32** %10
%39 = load i32, i32* @inkey
%40 = load i32, i32* @ASN1_TYPE_free
%41 = call i32 @sk_ASN1_TYPE_pop_free(i32 %39, i32 %40)
%42 = load i32*, i32** %10
%43 = icmp ne i32* %42, null
br i1 %43, label %48, label %44
44: ; preds = %35
%45 = load i32, i32* @ASN1_F_D2I_AUTOPRIVATEKEY
%46 = load i32, i32* @ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE
%47 = call i32 @ASN1err(i32 %45, i32 %46)
store i32* null, i32** %4
br label %73
48: ; preds = %35
%49 = load i32*, i32** %10
%50 = call i32* @EVP_PKCS82PKEY(i32* %49)
store i32* %50, i32** %11
%51 = load i32*, i32** %10
%52 = call i32 @PKCS8_PRIV_KEY_INFO_free(i32* %51)
%53 = load i32**, i32*** %5
%54 = icmp ne i32** %53, null
br i1 %54, label %55, label %58
55: ; preds = %48
%56 = load i32*, i32** %11
%57 = load i32**, i32*** %5
store i32* %56, i32** %57
br label %58
58: ; preds = %55, %48
%59 = load i32*, i32** %11
store i32* %59, i32** %4
br label %73
60: ; preds = %31
%61 = load i32, i32* @EVP_PKEY_RSA
store i32 %61, i32* %9
br label %62
62: ; preds = %60
br label %63
63: ; preds = %62, %29
br label %64
64: ; preds = %63, %23
%65 = load i32, i32* @inkey
%66 = load i32, i32* @ASN1_TYPE_free
%67 = call i32 @sk_ASN1_TYPE_pop_free(i32 %65, i32 %66)
%68 = load i32, i32* %9
%69 = load i32**, i32*** %5
%70 = load i8**, i8*** %6
%71 = load i64, i64* %7
%72 = call i32* @d2i_PrivateKey(i32 %68, i32** %69, i8** %70, i64 %71)
store i32* %72, i32** %4
br label %73
73: ; preds = %64, %58, %44
%74 = load i32*, i32** %4
ret i32* %74
}

LLVM IR (-O2):
@ASN1_TYPE = global i32 0
@inkey = global i32 0
@EVP_PKEY_DSA = global i32 0
@EVP_PKEY_EC = global i32 0
@ASN1_TYPE_free = global i32 0
@ASN1_F_D2I_AUTOPRIVATEKEY = global i32 0
@ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE = 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 = call i32 @d2i_ASN1_SEQUENCE_ANY(i32* null, i8** %4, i64 %2)
store i32 %9, i32* @inkey
%10 = call i32 @sk_ASN1_TYPE_num(i32 %9)
%11 = icmp eq i32 %10, 6
br i1 %11, label %35, label %12
12: ; preds = %3
%13 = load i32, i32* @inkey
%14 = call i32 @sk_ASN1_TYPE_num(i32 %13)
%15 = icmp eq i32 %14, 4
br i1 %15, label %35, label %16
16: ; preds = %12
%17 = load i32, i32* @inkey
%18 = call i32 @sk_ASN1_TYPE_num(i32 %17)
%19 = icmp eq i32 %18, 3
br i1 %19, label %20, label %35
20: ; preds = %16
%21 = call i32* @d2i_PKCS8_PRIV_KEY_INFO(i32* null, i8** %1, i64 %2)
%22 = load i32, i32* @inkey
%23 = load i32, i32* @ASN1_TYPE_free
%24 = call i32 @sk_ASN1_TYPE_pop_free(i32 %22, i32 %23)
%25 = icmp eq i32* %21, null
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* @ASN1_F_D2I_AUTOPRIVATEKEY
%28 = load i32, i32* @ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE
%29 = call i32 @ASN1err(i32 %27, i32 %28)
br label %42
30: ; preds = %20
%31 = call i32* @EVP_PKCS82PKEY(i32* %21)
%32 = call i32 @PKCS8_PRIV_KEY_INFO_free(i32* %21)
%33 = icmp eq i32** %0, null
br i1 %33, label %42, label %34
34: ; preds = %30
store i32* %31, i32** %0
br label %42
35: ; preds = %16, %12, %3
%36 = phi i32* [ @EVP_PKEY_DSA, %3 ], [ @EVP_PKEY_EC, %12 ], [ @EVP_PKEY_RSA, %16 ]
%37 = load i32, i32* %36
%38 = load i32, i32* @inkey
%39 = load i32, i32* @ASN1_TYPE_free
%40 = call i32 @sk_ASN1_TYPE_pop_free(i32 %38, i32 %39)
%41 = call i32* @d2i_PrivateKey(i32 %37, i32** %0, i8** %1, i64 %2)
br label %42
42: ; preds = %26, %34, %30, %35
%43 = phi i32* [ %41, %35 ], [ null, %26 ], [ %31, %34 ], [ %31, %30 ]
ret i32* %43
}