// File: /workspace/github_c_makefile_star2_O2/fm4dd[P]learn-ssl/certpubkey.c
// Function: main
int main() {

  const char cert_filestr[] = "./demo/cert-file.pem";
             EVP_PKEY *pkey = NULL;
  BIO              *certbio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;

  /* ---------------------------------------------------------- *
   * Create the Input/Output BIO's.                             *
   * ---------------------------------------------------------- */
  certbio = BIO_new(BIO_s_file());
  outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);

  /* ---------------------------------------------------------- *
   * Load the certificate from file (PEM).                      *
   * ---------------------------------------------------------- */
  BIO_read_filename(certbio, cert_filestr);
  if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
    BIO_printf(outbio, "Error loading cert into memory: %s\n", cert_filestr);
    exit(1);
  }

  /* ---------------------------------------------------------- *
   * Extract the certificate's public key data.                 *
   * ---------------------------------------------------------- */
  if ((pkey = X509_get_pubkey(cert)) == NULL)
    BIO_printf(outbio, "Error getting public key from certificate");

  /* ---------------------------------------------------------- *
   * Print the public key information and the key in PEM format *
   * ---------------------------------------------------------- */
  /* display the key type and size here */
  if (pkey) {
    BIO_printf(outbio, "Retrieved %d bit %s key", EVP_PKEY_bits(pkey),
                                     EVP_PKEY_get0_type_name(pkey));
    if(EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
        char curvestr[80];
        EVP_PKEY_get_utf8_string_param(pkey,
                            OSSL_PKEY_PARAM_GROUP_NAME,
                            curvestr,
                            sizeof(curvestr),
                            NULL);
        BIO_printf(outbio, ", type %s", curvestr);
#else
        EC_KEY *myecc = NULL;
        myecc = EVP_PKEY_get1_EC_KEY(pkey);
        const EC_GROUP *ecgrp = EC_KEY_get0_group(myecc);
        BIO_printf(outbio, ", type %s",
                OBJ_nid2sn(EC_GROUP_get_curve_name(ecgrp)));
#endif
    }
    BIO_printf(outbio, " from %s\n\n", cert_filestr);
  }
  else {
    BIO_printf(outbio, "Could not get public key from %s\n", cert_filestr);
    exit(1);
  }

  if(!PEM_write_bio_PUBKEY(outbio, pkey))
    BIO_printf(outbio, "Error writing public key data in PEM format");

  X509_free(cert);
  EVP_PKEY_free(pkey);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movaps 0xdcf(%rip), %xmm0      # 0x2010
movq %rsp, %r15
movaps %xmm0, (%r15)
movabsq $0x6d65702e656c69, %rax # imm = 0x6D65702E656C69
movq %rax, 0xd(%r15)
callq 0x1050
movq %rax, %rdi
callq 0x10f0
movq %rax, %r14
movq 0x2d63(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1090
movq %rax, %rbx
pushq $0x6c
popq %rsi
pushq $0x3
popq %rdx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1040
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1110
testq %rax, %rax
jne 0x12a7
leaq 0xd80(%rip), %rsi       # 0x2025
jmp 0x12cf
movq %rax, %r15
movq %rax, %rdi
callq 0x10e0
testq %rax, %rax
jne 0x12e4
leaq 0xd8b(%rip), %rsi       # 0x2049
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
leaq 0xdd7(%rip), %rsi       # 0x20a6
movq %rsp, %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
pushq $0x1
popq %rdi
callq 0x10d0
movq %rax, %r12
movq %rax, %rdi
callq 0x10b0
movl %eax, %ebp
movq %r12, %rdi
callq 0x10a0
leaq 0xd73(%rip), %rsi       # 0x2073
movq %rbx, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1120
movq %r12, %rdi
callq 0x1030
cmpl $0x198, %eax            # imm = 0x198
jne 0x134f
leaq 0xd66(%rip), %rsi       # 0x208b
leaq 0x20(%rsp), %r13
pushq $0x50
popq %rcx
movq %r12, %rdi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x10c0
leaq 0xd4f(%rip), %rsi       # 0x2091
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1120
leaq 0xd45(%rip), %rsi       # 0x209b
movq %rsp, %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1100
testl %eax, %eax
jne 0x1383
leaq 0xd4f(%rip), %rsi       # 0x20c8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
movq %r15, %rdi
callq 0x1070
movq %r12, %rdi
callq 0x1080
movq %r14, %rdi
callq 0x1060
movq %rbx, %rdi
callq 0x1060
xorl %edi, %edi
callq 0x10d0