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

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_mem_buf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ BIO_reset(int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_copy(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ EC_KEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKCS82PKEY(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_get1_EC_KEY(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_PKCS8(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_PKCS8_PRIV_KEY_INFO(int /*<<< orphan*/ *,
                                                     int /*<<< orphan*/ *,
                                                     int /*<<< orphan*/ *,
                                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ PKCS8_PRIV_KEY_INFO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *PKCS8_decrypt(int /*<<< orphan*/ *, char const *,
                                  int /*<<< orphan*/);
int /*<<< orphan*/ X509_SIG_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char const *);
int vg_pkcs8_decode_privkey(EC_KEY *pkey, const char *pem_in,
                            const char *pass) {
  EC_KEY *pkey_in = NULL;
  EC_KEY *test_key = NULL;
  EVP_PKEY *evp_key = NULL;
  PKCS8_PRIV_KEY_INFO *pkcs8 = NULL;
  X509_SIG *pkcs8_enc = NULL;
  BIO *bio = NULL;
  int res = 0;
  bio = BIO_new_mem_buf((char *)pem_in, strlen(pem_in));
  if (!bio)
    goto out;
  pkcs8_enc = PEM_read_bio_PKCS8(bio, NULL, NULL, NULL);
  if (pkcs8_enc) {
    if (!pass)
      return -1;
    pkcs8 = PKCS8_decrypt(pkcs8_enc, pass, strlen(pass));
  } else {
    (void)BIO_reset(bio);
    pkcs8 = PEM_read_bio_PKCS8_PRIV_KEY_INFO(bio, NULL, NULL, NULL);
  }
  if (!pkcs8)
    goto out;
  evp_key = EVP_PKCS82PKEY(pkcs8);
  if (!evp_key)
    goto out;
  pkey_in = EVP_PKEY_get1_EC_KEY(evp_key);
  if (!pkey_in)
    goto out;
  if (!EC_KEY_copy(pkey, pkey_in))
    goto out;
  res = 1;
out:
  if (bio)
    BIO_free(bio);
  if (test_key)
    EC_KEY_free(pkey_in);
  if (evp_key)
    EVP_PKEY_free(evp_key);
  if (pkcs8)
    PKCS8_PRIV_KEY_INFO_free(pkcs8);
  if (pkcs8_enc)
    X509_SIG_free(pkcs8_enc);
  return res;
}


x86 assembly (clang, -O0):
<vg_pkcs8_decode_privkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1090 <BIO_new_mem_buf@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    125b <vg_pkcs8_decode_privkey+0x7b>
jmp    1351 <vg_pkcs8_decode_privkey+0x171>
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1100 <PEM_read_bio_PKCS8@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12c5 <vg_pkcs8_decode_privkey+0xe5>
cmpq   $0x0,-0x20(%rbp)
jne    1294 <vg_pkcs8_decode_privkey+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bb <vg_pkcs8_decode_privkey+0x1db>
mov    -0x48(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <PKCS8_decrypt@plt>
mov    %rax,-0x40(%rbp)
jmp    12e5 <vg_pkcs8_decode_privkey+0x105>
mov    -0x50(%rbp),%rdi
call   10f0 <BIO_reset@plt>
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10a0 <PEM_read_bio_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12f5 <vg_pkcs8_decode_privkey+0x115>
jmp    1351 <vg_pkcs8_decode_privkey+0x171>
mov    -0x40(%rbp),%rdi
call   10d0 <EVP_PKCS82PKEY@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1312 <vg_pkcs8_decode_privkey+0x132>
jmp    1351 <vg_pkcs8_decode_privkey+0x171>
mov    -0x38(%rbp),%rdi
call   1070 <EVP_PKEY_get1_EC_KEY@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    132f <vg_pkcs8_decode_privkey+0x14f>
jmp    1351 <vg_pkcs8_decode_privkey+0x171>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <EC_KEY_copy@plt>
cmp    $0x0,%eax
jne    134a <vg_pkcs8_decode_privkey+0x16a>
jmp    1351 <vg_pkcs8_decode_privkey+0x171>
movl   $0x1,-0x54(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1365 <vg_pkcs8_decode_privkey+0x185>
mov    -0x50(%rbp),%rdi
call   10b0 <BIO_free@plt>
cmpq   $0x0,-0x30(%rbp)
je     1379 <vg_pkcs8_decode_privkey+0x199>
mov    -0x28(%rbp),%rdi
call   10c0 <EC_KEY_free@plt>
cmpq   $0x0,-0x38(%rbp)
je     138d <vg_pkcs8_decode_privkey+0x1ad>
mov    -0x38(%rbp),%rdi
call   1050 <EVP_PKEY_free@plt>
cmpq   $0x0,-0x40(%rbp)
je     13a1 <vg_pkcs8_decode_privkey+0x1c1>
mov    -0x40(%rbp),%rdi
call   10e0 <PKCS8_PRIV_KEY_INFO_free@plt>
cmpq   $0x0,-0x48(%rbp)
je     13b5 <vg_pkcs8_decode_privkey+0x1d5>
mov    -0x48(%rbp),%rdi
call   1060 <X509_SIG_free@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vg_pkcs8_decode_privkey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <BIO_new_mem_buf@plt>
test   %rax,%rax
je     1230 <vg_pkcs8_decode_privkey+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10f0 <PEM_read_bio_PKCS8@plt>
mov    %rax,%r14
test   %rax,%rax
je     1237 <vg_pkcs8_decode_privkey+0x67>
test   %rbp,%rbp
je     1298 <vg_pkcs8_decode_privkey+0xc8>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <PKCS8_decrypt@plt>
jmp    124d <vg_pkcs8_decode_privkey+0x7d>
xor    %ebp,%ebp
jmp    12d2 <vg_pkcs8_decode_privkey+0x102>
mov    %rbx,%rdi
call   10e0 <BIO_reset@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <PEM_read_bio_PKCS8_PRIV_KEY_INFO@plt>
mov    %rax,%r12
test   %rax,%rax
je     1287 <vg_pkcs8_decode_privkey+0xb7>
mov    %r12,%rdi
call   10c0 <EVP_PKCS82PKEY@plt>
test   %rax,%rax
je     129f <vg_pkcs8_decode_privkey+0xcf>
mov    %rax,%r13
mov    %rax,%rdi
call   1070 <EVP_PKEY_get1_EC_KEY@plt>
test   %rax,%rax
je     12ab <vg_pkcs8_decode_privkey+0xdb>
mov    %r15,%rdi
mov    %rax,%rsi
call   1080 <EC_KEY_copy@plt>
xor    %ebp,%ebp
test   %eax,%eax
setne  %bpl
jmp    12ad <vg_pkcs8_decode_privkey+0xdd>
mov    %rbx,%rdi
call   10b0 <BIO_free@plt>
xor    %ebp,%ebp
test   %r14,%r14
jne    12ca <vg_pkcs8_decode_privkey+0xfa>
jmp    12d2 <vg_pkcs8_decode_privkey+0x102>
mov    $0xffffffff,%ebp
jmp    12d2 <vg_pkcs8_decode_privkey+0x102>
mov    %rbx,%rdi
call   10b0 <BIO_free@plt>
xor    %ebp,%ebp
jmp    12bd <vg_pkcs8_decode_privkey+0xed>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   10b0 <BIO_free@plt>
mov    %r13,%rdi
call   1050 <EVP_PKEY_free@plt>
mov    %r12,%rdi
call   10d0 <PKCS8_PRIV_KEY_INFO_free@plt>
test   %r14,%r14
je     12d2 <vg_pkcs8_decode_privkey+0x102>
mov    %r14,%rdi
call   1060 <X509_SIG_free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vg_pkcs8_decode_privkey(i32* %0, i8* %1, i8* %2) {
%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** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32* null, i32** %8
store i32* null, i32** %9
store i32* null, i32** %10
store i32* null, i32** %11
store i32* null, i32** %12
store i32* null, i32** %13
store i32 0, i32* %14
%15 = load i8*, i8** %6
%16 = load i8*, i8** %6
%17 = call i32 @strlen(i8* %16)
%18 = call i32* @BIO_new_mem_buf(i8* %15, i32 %17)
store i32* %18, i32** %13
%19 = load i32*, i32** %13
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %3
br label %65
22: ; preds = %3
%23 = load i32*, i32** %13
%24 = call i32* @PEM_read_bio_PKCS8(i32* %23, i32* null, i32* null, i32* null)
store i32* %24, i32** %12
%25 = load i32*, i32** %12
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %37
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %27
store i32 -1, i32* %4
br label %97
31: ; preds = %27
%32 = load i32*, i32** %12
%33 = load i8*, i8** %7
%34 = load i8*, i8** %7
%35 = call i32 @strlen(i8* %34)
%36 = call i32* @PKCS8_decrypt(i32* %32, i8* %33, i32 %35)
store i32* %36, i32** %11
br label %42
37: ; preds = %22
%38 = load i32*, i32** %13
%39 = call i32 @BIO_reset(i32* %38)
%40 = load i32*, i32** %13
%41 = call i32* @PEM_read_bio_PKCS8_PRIV_KEY_INFO(i32* %40, i32* null, i32* null, i32* null)
store i32* %41, i32** %11
br label %42
42: ; preds = %37, %31
%43 = load i32*, i32** %11
%44 = icmp ne i32* %43, null
br i1 %44, label %46, label %45
45: ; preds = %42
br label %65
46: ; preds = %42
%47 = load i32*, i32** %11
%48 = call i32* @EVP_PKCS82PKEY(i32* %47)
store i32* %48, i32** %10
%49 = load i32*, i32** %10
%50 = icmp ne i32* %49, null
br i1 %50, label %52, label %51
51: ; preds = %46
br label %65
52: ; preds = %46
%53 = load i32*, i32** %10
%54 = call i32* @EVP_PKEY_get1_EC_KEY(i32* %53)
store i32* %54, i32** %8
%55 = load i32*, i32** %8
%56 = icmp ne i32* %55, null
br i1 %56, label %58, label %57
57: ; preds = %52
br label %65
58: ; preds = %52
%59 = load i32*, i32** %5
%60 = load i32*, i32** %8
%61 = call i32 @EC_KEY_copy(i32* %59, i32* %60)
%62 = icmp ne i32 %61, 0
br i1 %62, label %64, label %63
63: ; preds = %58
br label %65
64: ; preds = %58
store i32 1, i32* %14
br label %65
65: ; preds = %64, %63, %57, %51, %45, %21
%66 = load i32*, i32** %13
%67 = icmp ne i32* %66, null
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32*, i32** %13
%70 = call i32 @BIO_free(i32* %69)
br label %71
71: ; preds = %68, %65
%72 = load i32*, i32** %9
%73 = icmp ne i32* %72, null
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32*, i32** %8
%76 = call i32 @EC_KEY_free(i32* %75)
br label %77
77: ; preds = %74, %71
%78 = load i32*, i32** %10
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %77
%81 = load i32*, i32** %10
%82 = call i32 @EVP_PKEY_free(i32* %81)
br label %83
83: ; preds = %80, %77
%84 = load i32*, i32** %11
%85 = icmp ne i32* %84, null
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32*, i32** %11
%88 = call i32 @PKCS8_PRIV_KEY_INFO_free(i32* %87)
br label %89
89: ; preds = %86, %83
%90 = load i32*, i32** %12
%91 = icmp ne i32* %90, null
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32*, i32** %12
%94 = call i32 @X509_SIG_free(i32* %93)
br label %95
95: ; preds = %92, %89
%96 = load i32, i32* %14
store i32 %96, i32* %4
br label %97
97: ; preds = %95, %30
%98 = load i32, i32* %4
ret i32 %98
}

LLVM IR (-O2):
define i32 @vg_pkcs8_decode_privkey(i32* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32* @BIO_new_mem_buf(i8* %1, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %46, label %7
7: ; preds = %3
%8 = tail call i32* @PEM_read_bio_PKCS8(i32* %5, i32* null, i32* null, i32* null)
%9 = icmp eq i32* %8, null
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = icmp eq i8* %2, null
br i1 %11, label %46, label %12
12: ; preds = %10
%13 = tail call i32 @strlen(i8* %2)
%14 = tail call i32* @PKCS8_decrypt(i32* %8, i8* %2, i32 %13)
br label %18
15: ; preds = %7
%16 = tail call i32 @BIO_reset(i32* %5)
%17 = tail call i32* @PEM_read_bio_PKCS8_PRIV_KEY_INFO(i32* %5, i32* null, i32* null, i32* null)
br label %18
18: ; preds = %15, %12
%19 = phi i32* [ %14, %12 ], [ %17, %15 ]
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @BIO_free(i32* %5)
br label %42
23: ; preds = %18
%24 = tail call i32* @EVP_PKCS82PKEY(i32* %19)
%25 = icmp eq i32* %24, null
br i1 %25, label %37, label %26
26: ; preds = %23
%27 = tail call i32* @EVP_PKEY_get1_EC_KEY(i32* %24)
%28 = icmp eq i32* %27, null
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = tail call i32 @EC_KEY_copy(i32* %0, i32* %27)
%31 = icmp ne i32 %30, 0
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %26, %29
%34 = phi i32 [ %32, %29 ], [ 0, %26 ]
%35 = tail call i32 @BIO_free(i32* %5)
%36 = tail call i32 @EVP_PKEY_free(i32* %24)
br label %39
37: ; preds = %23
%38 = tail call i32 @BIO_free(i32* %5)
br label %39
39: ; preds = %37, %33
%40 = phi i32 [ %34, %33 ], [ 0, %37 ]
%41 = tail call i32 @PKCS8_PRIV_KEY_INFO_free(i32* %19)
br label %42
42: ; preds = %21, %39
%43 = phi i32 [ %40, %39 ], [ 0, %21 ]
br i1 %9, label %46, label %44
44: ; preds = %42
%45 = tail call i32 @X509_SIG_free(i32* %8)
br label %46
46: ; preds = %3, %42, %44, %10
%47 = phi i32 [ -1, %10 ], [ %43, %44 ], [ %43, %42 ], [ 0, %3 ]
ret i32 %47
}