#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);
  p = *pp;
  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, &p, 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 (ret == NULL)
      return NULL;
    *pp = p;
    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    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    120c <d2i_AutoPrivateKey+0x7c>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1318 <d2i_AutoPrivateKey+0x188>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    1234 <d2i_AutoPrivateKey+0xa4>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1313 <d2i_AutoPrivateKey+0x183>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x3,%eax
jne    1302 <d2i_AutoPrivateKey+0x172>
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rsi
call   10b0 <d2i_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,-0x38(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
cmpq   $0x0,-0x38(%rbp)
jne    12a6 <d2i_AutoPrivateKey+0x116>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movq   $0x0,-0x8(%rbp)
jmp    1347 <d2i_AutoPrivateKey+0x1b7>
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,-0x40(%rbp)
jne    12d4 <d2i_AutoPrivateKey+0x144>
movq   $0x0,-0x8(%rbp)
jmp    1347 <d2i_AutoPrivateKey+0x1b7>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     12f5 <d2i_AutoPrivateKey+0x165>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1347 <d2i_AutoPrivateKey+0x1b7>
mov    0x2cc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1313 <d2i_AutoPrivateKey+0x183>
jmp    1318 <d2i_AutoPrivateKey+0x188>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
mov    0x2c80(%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   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1030 <STACK_OF@plt>
mov    (%r12),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   10a0 <d2i_ASN1_SEQUENCE_ANY@plt>
mov    0x2e05(%rip),%r13
mov    %eax,0x0(%r13)
mov    (%r12),%rcx
mov    %rcx,0x8(%rsp)
mov    %eax,%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x6,%eax
jne    11f8 <d2i_AutoPrivateKey+0x68>
mov    0x2e05(%rip),%rax
jmp    1278 <d2i_AutoPrivateKey+0xe8>
mov    0x0(%r13),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x4,%eax
jne    120f <d2i_AutoPrivateKey+0x7f>
mov    0x2db3(%rip),%rax
jmp    1278 <d2i_AutoPrivateKey+0xe8>
mov    0x0(%r13),%edi
call   1050 <sk_ASN1_TYPE_num@plt>
cmp    $0x3,%eax
jne    1271 <d2i_AutoPrivateKey+0xe1>
lea    0x8(%rsp),%rsi
xor    %edi,%edi
mov    %r15,%rdx
call   10b0 <d2i_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,%r15
mov    0x0(%r13),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
test   %r15,%r15
je     12b0 <d2i_AutoPrivateKey+0x120>
mov    %r15,%rdi
call   1070 <EVP_PKCS82PKEY@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <PKCS8_PRIV_KEY_INFO_free@plt>
test   %rbx,%rbx
je     12c7 <d2i_AutoPrivateKey+0x137>
mov    0x8(%rsp),%rax
mov    %rax,(%r12)
test   %r14,%r14
je     129f <d2i_AutoPrivateKey+0x10f>
mov    %rbx,(%r14)
jmp    129f <d2i_AutoPrivateKey+0x10f>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x0(%r13),%edi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
call   1080 <sk_ASN1_TYPE_pop_free@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1060 <d2i_PrivateKey@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
xor    %ebx,%ebx
jmp    129f <d2i_AutoPrivateKey+0x10f>

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 i8**, i8*** %6
%21 = load i8*, i8** %20
store i8* %21, i8** %8
%22 = load i32, i32* @inkey
%23 = call i32 @sk_ASN1_TYPE_num(i32 %22)
%24 = icmp eq i32 %23, 6
br i1 %24, label %25, label %27
25: ; preds = %3
%26 = load i32, i32* @EVP_PKEY_DSA
store i32 %26, i32* %9
br label %71
27: ; preds = %3
%28 = load i32, i32* @inkey
%29 = call i32 @sk_ASN1_TYPE_num(i32 %28)
%30 = icmp eq i32 %29, 4
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* @EVP_PKEY_EC
store i32 %32, i32* %9
br label %70
33: ; preds = %27
%34 = load i32, i32* @inkey
%35 = call i32 @sk_ASN1_TYPE_num(i32 %34)
%36 = icmp eq i32 %35, 3
br i1 %36, label %37, label %67
37: ; preds = %33
%38 = load i64, i64* %7
%39 = call i32* @d2i_PKCS8_PRIV_KEY_INFO(i32* null, i8** %8, i64 %38)
store i32* %39, i32** %10
%40 = load i32, i32* @inkey
%41 = load i32, i32* @ASN1_TYPE_free
%42 = call i32 @sk_ASN1_TYPE_pop_free(i32 %40, i32 %41)
%43 = load i32*, i32** %10
%44 = icmp ne i32* %43, null
br i1 %44, label %49, label %45
45: ; preds = %37
%46 = load i32, i32* @ASN1_F_D2I_AUTOPRIVATEKEY
%47 = load i32, i32* @ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE
%48 = call i32 @ASN1err(i32 %46, i32 %47)
store i32* null, i32** %4
br label %80
49: ; preds = %37
%50 = load i32*, i32** %10
%51 = call i32* @EVP_PKCS82PKEY(i32* %50)
store i32* %51, i32** %11
%52 = load i32*, i32** %10
%53 = call i32 @PKCS8_PRIV_KEY_INFO_free(i32* %52)
%54 = load i32*, i32** %11
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %57
56: ; preds = %49
store i32* null, i32** %4
br label %80
57: ; preds = %49
%58 = load i8*, i8** %8
%59 = load i8**, i8*** %6
store i8* %58, i8** %59
%60 = load i32**, i32*** %5
%61 = icmp ne i32** %60, null
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = load i32*, i32** %11
%64 = load i32**, i32*** %5
store i32* %63, i32** %64
br label %65
65: ; preds = %62, %57
%66 = load i32*, i32** %11
store i32* %66, i32** %4
br label %80
67: ; preds = %33
%68 = load i32, i32* @EVP_PKEY_RSA
store i32 %68, i32* %9
br label %69
69: ; preds = %67
br label %70
70: ; preds = %69, %31
br label %71
71: ; preds = %70, %25
%72 = load i32, i32* @inkey
%73 = load i32, i32* @ASN1_TYPE_free
%74 = call i32 @sk_ASN1_TYPE_pop_free(i32 %72, i32 %73)
%75 = load i32, i32* %9
%76 = load i32**, i32*** %5
%77 = load i8**, i8*** %6
%78 = load i64, i64* %7
%79 = call i32* @d2i_PrivateKey(i32 %75, i32** %76, i8** %77, i64 %78)
store i32* %79, i32** %4
br label %80
80: ; preds = %71, %65, %56, %45
%81 = load i32*, i32** %4
ret i32* %81
}

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