#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_ALGOR;
typedef int /*<<< orphan*/ PKCS8_PRIV_KEY_INFO;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ EC_POINT;
typedef int /*<<< orphan*/ EC_KEY;
typedef int /*<<< orphan*/ EC_GROUP;
typedef int /*<<< orphan*/ BIGNUM;

/* Variables and functions */
int /*<<< orphan*/ EC_GROUP_get0_generator(int /*<<< orphan*/ const *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_group(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_private_key(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_KEY_get0_public_key(int /*<<< orphan*/ *);
scalar_t__ EC_KEY_set_public_key(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_copy(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EC_POINT_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_POINT_mul(int /*<<< orphan*/ const *,
                                int /*<<< orphan*/ *,
                                int /*<<< orphan*/ const *,
                                int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                int /*<<< orphan*/ *);
int /*<<< orphan*/ *EC_POINT_new(int /*<<< orphan*/ const *);
int /*<<< orphan*/ EC_R_DECODE_ERROR;
int /*<<< orphan*/ ECerror(int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_EC_LIB;
int /*<<< orphan*/ EVP_PKEY_assign_EC_KEY(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS8_pkey_get0(int /*<<< orphan*/ *, unsigned char const **,
                                   int *, int /*<<< orphan*/ **,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_ALGOR_get0(int /*<<< orphan*/ *, int *, void **,
                                   int /*<<< orphan*/ *);
int /*<<< orphan*/ d2i_ECPrivateKey(int /*<<< orphan*/ **,
                                    unsigned char const **, int);
int /*<<< orphan*/ *eckey_type2param(int, void *);
int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) {
  const unsigned char *p = NULL;
  void *pval;
  int ptype, pklen;
  EC_KEY *eckey = NULL;
  X509_ALGOR *palg;
  if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
    return 0;
  X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  eckey = eckey_type2param(ptype, pval);
  if (!eckey)
    goto ecliberr;
  if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
    ECerror(EC_R_DECODE_ERROR);
    goto ecerr;
  }
  if (EC_KEY_get0_public_key(eckey) == NULL) {
    const BIGNUM *priv_key;
    const EC_GROUP *group;
    EC_POINT *pub_key;
    group = EC_KEY_get0_group(eckey);
    pub_key = EC_POINT_new(group);
    if (pub_key == NULL) {
      ECerror(ERR_R_EC_LIB);
      goto ecliberr;
    }
    if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
      EC_POINT_free(pub_key);
      ECerror(ERR_R_EC_LIB);
      goto ecliberr;
    }
    priv_key = EC_KEY_get0_private_key(eckey);
    if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) {
      EC_POINT_free(pub_key);
      ECerror(ERR_R_EC_LIB);
      goto ecliberr;
    }
    if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
      EC_POINT_free(pub_key);
      ECerror(ERR_R_EC_LIB);
      goto ecliberr;
    }
    EC_POINT_free(pub_key);
  }
  EVP_PKEY_assign_EC_KEY(pkey, eckey);
  return 1;
ecliberr:
  ECerror(ERR_R_EC_LIB);
ecerr:
  if (eckey)
    EC_KEY_free(eckey);
  return 0;
}


x86 assembly (clang, -O0):
<eckey_priv_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%r8
xor    %eax,%eax
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1090 <PKCS8_pkey_get0@plt>
cmp    $0x0,%eax
jne    124e <eckey_priv_decode+0x4e>
movl   $0x0,-0x4(%rbp)
jmp    1407 <eckey_priv_decode+0x207>
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x2c(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1120 <X509_ALGOR_get0@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   10c0 <eckey_type2param@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1283 <eckey_priv_decode+0x83>
jmp    13de <eckey_priv_decode+0x1de>
mov    -0x30(%rbp),%edx
lea    -0x38(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10f0 <d2i_ECPrivateKey@plt>
cmp    $0x0,%eax
jne    12af <eckey_priv_decode+0xaf>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
jmp    13ec <eckey_priv_decode+0x1ec>
mov    -0x38(%rbp),%rdi
call   1070 <EC_KEY_get0_public_key@plt>
cmp    $0x0,%rax
jne    13c5 <eckey_priv_decode+0x1c5>
mov    -0x38(%rbp),%rdi
call   10a0 <EC_KEY_get0_group@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1110 <EC_POINT_new@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    12fa <eckey_priv_decode+0xfa>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
jmp    13de <eckey_priv_decode+0x1de>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   10d0 <EC_GROUP_get0_generator@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1030 <EC_POINT_copy@plt>
cmp    $0x0,%eax
jne    133b <eckey_priv_decode+0x13b>
mov    -0x58(%rbp),%rdi
call   1100 <EC_POINT_free@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
jmp    13de <eckey_priv_decode+0x1de>
mov    -0x38(%rbp),%rdi
call   1080 <EC_KEY_get0_private_key@plt>
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x48(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%rcx
mov    %r9,%r8
call   1060 <EC_POINT_mul@plt>
cmp    $0x0,%eax
jne    1389 <eckey_priv_decode+0x189>
mov    -0x58(%rbp),%rdi
call   1100 <EC_POINT_free@plt>
mov    0x2c63(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
jmp    13de <eckey_priv_decode+0x1de>
mov    -0x38(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1040 <EC_KEY_set_public_key@plt>
cmp    $0x0,%rax
jne    13bc <eckey_priv_decode+0x1bc>
mov    -0x58(%rbp),%rdi
call   1100 <EC_POINT_free@plt>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
jmp    13de <eckey_priv_decode+0x1de>
mov    -0x58(%rbp),%rdi
call   1100 <EC_POINT_free@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10e0 <EVP_PKEY_assign_EC_KEY@plt>
movl   $0x1,-0x4(%rbp)
jmp    1407 <eckey_priv_decode+0x207>
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   1050 <ECerror@plt>
cmpq   $0x0,-0x38(%rbp)
je     1400 <eckey_priv_decode+0x200>
mov    -0x38(%rbp),%rdi
call   10b0 <EC_KEY_free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eckey_priv_decode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r8
mov    %rdi,%r14
movq   $0x0,0x18(%rsp)
xor    %r13d,%r13d
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x20(%rsp),%rcx
xor    %edi,%edi
call   1090 <PKCS8_pkey_get0@plt>
test   %eax,%eax
je     136a <eckey_priv_decode+0x16a>
mov    0x20(%rsp),%rcx
xor    %r13d,%r13d
lea    0x14(%rsp),%rsi
lea    0x28(%rsp),%rdx
xor    %edi,%edi
call   1120 <X509_ALGOR_get0@plt>
mov    0x14(%rsp),%edi
mov    0x28(%rsp),%rsi
call   10c0 <eckey_type2param@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     12ac <eckey_priv_decode+0xac>
mov    0x10(%rsp),%edx
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   10f0 <d2i_ECPrivateKey@plt>
test   %eax,%eax
je     12b8 <eckey_priv_decode+0xb8>
mov    0x8(%rsp),%rdi
call   1070 <EC_KEY_get0_public_key@plt>
test   %rax,%rax
je     12c4 <eckey_priv_decode+0xc4>
mov    0x8(%rsp),%rsi
mov    %r14,%rdi
call   10e0 <EVP_PKEY_assign_EC_KEY@plt>
mov    $0x1,%r13d
jmp    136a <eckey_priv_decode+0x16a>
mov    0x2d2d(%rip),%rbx
jmp    1354 <eckey_priv_decode+0x154>
mov    0x2d29(%rip),%rbx
jmp    1354 <eckey_priv_decode+0x154>
mov    0x8(%rsp),%rdi
call   10a0 <EC_KEY_get0_group@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1110 <EC_POINT_new@plt>
test   %rax,%rax
je     1346 <eckey_priv_decode+0x146>
mov    %rax,%r15
mov    %r12,%rdi
call   10d0 <EC_GROUP_get0_generator@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <EC_POINT_copy@plt>
test   %eax,%eax
je     133e <eckey_priv_decode+0x13e>
mov    0x8(%rsp),%rdi
call   1080 <EC_KEY_get0_private_key@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <EC_POINT_mul@plt>
test   %eax,%eax
je     133e <eckey_priv_decode+0x13e>
mov    0x8(%rsp),%rdi
mov    %r15,%rsi
call   1040 <EC_KEY_set_public_key@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1100 <EC_POINT_free@plt>
test   %r12,%r12
jne    1294 <eckey_priv_decode+0x94>
jmp    1346 <eckey_priv_decode+0x146>
mov    %r15,%rdi
call   1100 <EC_POINT_free@plt>
mov    0x2c93(%rip),%rbx
mov    (%rbx),%edi
call   1050 <ECerror@plt>
mov    (%rbx),%edi
call   1050 <ECerror@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     136a <eckey_priv_decode+0x16a>
call   10b0 <EC_KEY_free@plt>
mov    %r13d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EC_R_DECODE_ERROR = global i32 0
@ERR_R_EC_LIB = global i32 0
define i32 @eckey_priv_decode(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i8* null, i8** %6
store i32* null, i32** %10
%15 = load i32*, i32** %5
%16 = call i32 @PKCS8_pkey_get0(i32* null, i8** %6, i32* %9, i32** %11, i32* %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %2
store i32 0, i32* %3
br label %100
19: ; preds = %2
%20 = load i32*, i32** %11
%21 = call i32 @X509_ALGOR_get0(i32* null, i32* %8, i8** %7, i32* %20)
%22 = load i32, i32* %8
%23 = load i8*, i8** %7
%24 = call i32* @eckey_type2param(i32 %22, i8* %23)
store i32* %24, i32** %10
%25 = load i32*, i32** %10
%26 = icmp ne i32* %25, null
br i1 %26, label %28, label %27
27: ; preds = %19
br label %90
28: ; preds = %19
%29 = load i32, i32* %9
%30 = call i32 @d2i_ECPrivateKey(i32** %10, i8** %6, i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i32, i32* @EC_R_DECODE_ERROR
%34 = call i32 @ECerror(i32 %33)
br label %93
35: ; preds = %28
%36 = load i32*, i32** %10
%37 = call i32* @EC_KEY_get0_public_key(i32* %36)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %86
39: ; preds = %35
%40 = load i32*, i32** %10
%41 = call i32* @EC_KEY_get0_group(i32* %40)
store i32* %41, i32** %13
%42 = load i32*, i32** %13
%43 = call i32* @EC_POINT_new(i32* %42)
store i32* %43, i32** %14
%44 = load i32*, i32** %14
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = load i32, i32* @ERR_R_EC_LIB
%48 = call i32 @ECerror(i32 %47)
br label %90
49: ; preds = %39
%50 = load i32*, i32** %14
%51 = load i32*, i32** %13
%52 = call i32 @EC_GROUP_get0_generator(i32* %51)
%53 = call i32 @EC_POINT_copy(i32* %50, i32 %52)
%54 = icmp ne i32 %53, 0
br i1 %54, label %60, label %55
55: ; preds = %49
%56 = load i32*, i32** %14
%57 = call i32 @EC_POINT_free(i32* %56)
%58 = load i32, i32* @ERR_R_EC_LIB
%59 = call i32 @ECerror(i32 %58)
br label %90
60: ; preds = %49
%61 = load i32*, i32** %10
%62 = call i32* @EC_KEY_get0_private_key(i32* %61)
store i32* %62, i32** %12
%63 = load i32*, i32** %13
%64 = load i32*, i32** %14
%65 = load i32*, i32** %12
%66 = call i32 @EC_POINT_mul(i32* %63, i32* %64, i32* %65, i32* null, i32* null, i32* null)
%67 = icmp ne i32 %66, 0
br i1 %67, label %73, label %68
68: ; preds = %60
%69 = load i32*, i32** %14
%70 = call i32 @EC_POINT_free(i32* %69)
%71 = load i32, i32* @ERR_R_EC_LIB
%72 = call i32 @ECerror(i32 %71)
br label %90
73: ; preds = %60
%74 = load i32*, i32** %10
%75 = load i32*, i32** %14
%76 = call i64 @EC_KEY_set_public_key(i32* %74, i32* %75)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %73
%79 = load i32*, i32** %14
%80 = call i32 @EC_POINT_free(i32* %79)
%81 = load i32, i32* @ERR_R_EC_LIB
%82 = call i32 @ECerror(i32 %81)
br label %90
83: ; preds = %73
%84 = load i32*, i32** %14
%85 = call i32 @EC_POINT_free(i32* %84)
br label %86
86: ; preds = %83, %35
%87 = load i32*, i32** %4
%88 = load i32*, i32** %10
%89 = call i32 @EVP_PKEY_assign_EC_KEY(i32* %87, i32* %88)
store i32 1, i32* %3
br label %100
90: ; preds = %78, %68, %55, %46, %27
%91 = load i32, i32* @ERR_R_EC_LIB
%92 = call i32 @ECerror(i32 %91)
br label %93
93: ; preds = %90, %32
%94 = load i32*, i32** %10
%95 = icmp ne i32* %94, null
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32*, i32** %10
%98 = call i32 @EC_KEY_free(i32* %97)
br label %99
99: ; preds = %96, %93
store i32 0, i32* %3
br label %100
100: ; preds = %99, %86, %18
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@EC_R_DECODE_ERROR = global i32 0
@ERR_R_EC_LIB = global i32 0
define i32 @eckey_priv_decode(i32* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%10 = bitcast i8** %4 to i8*
%11 = bitcast i32* %5 to i8*
%12 = bitcast i32* %6 to i8*
%13 = bitcast i32** %7 to i8*
%14 = bitcast i32** %8 to i8*
%15 = call i32 @PKCS8_pkey_get0(i32* null, i8** %3, i32* %6, i32** %8, i32* %1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %67, label %17
17: ; preds = %2
%18 = load i32*, i32** %8
%19 = call i32 @X509_ALGOR_get0(i32* null, i32* %5, i8** %4, i32* %18)
%20 = load i32, i32* %5
%21 = load i8*, i8** %4
%22 = call i32* @eckey_type2param(i32 %20, i8* %21)
store i32* %22, i32** %7
%23 = icmp eq i32* %22, null
br i1 %23, label %59, label %24
24: ; preds = %17
%25 = load i32, i32* %6
%26 = call i32 @d2i_ECPrivateKey(i32** %7, i8** %3, i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %59, label %28
28: ; preds = %24
%29 = load i32*, i32** %7
%30 = call i32* @EC_KEY_get0_public_key(i32* %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = load i32*, i32** %7
%34 = call i32* @EC_KEY_get0_group(i32* %33)
%35 = call i32* @EC_POINT_new(i32* %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %56, label %37
37: ; preds = %32
%38 = call i32 @EC_GROUP_get0_generator(i32* %34)
%39 = call i32 @EC_POINT_copy(i32* %35, i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %54, label %41
41: ; preds = %37
%42 = load i32*, i32** %7
%43 = call i32* @EC_KEY_get0_private_key(i32* %42)
%44 = call i32 @EC_POINT_mul(i32* %34, i32* %35, i32* %43, i32* null, i32* null, i32* null)
%45 = icmp eq i32 %44, 0
br i1 %45, label %54, label %46
46: ; preds = %41
%47 = load i32*, i32** %7
%48 = call i64 @EC_KEY_set_public_key(i32* %47, i32* %35)
%49 = icmp eq i64 %48, 0
%50 = call i32 @EC_POINT_free(i32* %35)
br i1 %49, label %56, label %51
51: ; preds = %46, %28
%52 = load i32*, i32** %7
%53 = call i32 @EVP_PKEY_assign_EC_KEY(i32* %0, i32* %52)
br label %67
54: ; preds = %41, %37
%55 = call i32 @EC_POINT_free(i32* %35)
br label %56
56: ; preds = %54, %46, %32
%57 = load i32, i32* @ERR_R_EC_LIB
%58 = call i32 @ECerror(i32 %57)
br label %59
59: ; preds = %17, %56, %24
%60 = phi i32* [ @EC_R_DECODE_ERROR, %24 ], [ @ERR_R_EC_LIB, %56 ], [ @ERR_R_EC_LIB, %17 ]
%61 = load i32, i32* %60
%62 = call i32 @ECerror(i32 %61)
%63 = load i32*, i32** %7
%64 = icmp eq i32* %63, null
br i1 %64, label %67, label %65
65: ; preds = %59
%66 = call i32 @EC_KEY_free(i32* %63)
br label %67
67: ; preds = %59, %65, %2, %51
%68 = phi i32 [ 1, %51 ], [ 0, %2 ], [ 0, %65 ], [ 0, %59 ]
ret i32 %68
}