// File: /workspace/github_c_makefile_star2_O1/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
movl $0x3, %edx
movq %r14, %rdi
movl $0x6c, %esi
movq %r15, %rcx
callq 0x1040
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1110
testq %rax, %rax
jne 0x12ab
leaq 0xd7c(%rip), %rsi       # 0x2025
jmp 0x12d3
movq %rax, %r15
movq %rax, %rdi
callq 0x10e0
testq %rax, %rax
jne 0x12ea
leaq 0xd87(%rip), %rsi       # 0x2049
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
leaq 0xdd3(%rip), %rsi       # 0x20a6
movq %rsp, %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1120
movl $0x1, %edi
callq 0x10d0
movq %rax, %r12
movq %rax, %rdi
callq 0x10b0
movl %eax, %ebp
movq %r12, %rdi
callq 0x10a0
leaq 0xd6d(%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 0x1357
leaq 0xd60(%rip), %rsi       # 0x208b
leaq 0x20(%rsp), %r13
movl $0x50, %ecx
movq %r12, %rdi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x10c0
leaq 0xd47(%rip), %rsi       # 0x2091
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x1120
leaq 0xd3d(%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 0x138b
leaq 0xd47(%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