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

  const char       cert_filestr[] = "./demo/cert-file.pem";
  const ASN1_BIT_STRING *asn1_sig = NULL;
  const X509_ALGOR      *sig_type = NULL;
  size_t                sig_bytes = 0;
  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 signature data.                  *
   * ---------------------------------------------------------- */
  X509_get0_signature(&asn1_sig, &sig_type, cert);
  //sig_type = cert->sig_alg;
  //asn1_sig = cert->signature;
  sig_bytes = asn1_sig->length;

  /* ---------------------------------------------------------- *
   * Print the signature type here                              *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Algorithm:\n");
  if (i2a_ASN1_OBJECT(outbio, sig_type->algorithm) <= 0)
    BIO_printf(outbio, "Error getting the signature algorithm.\n");
  else BIO_puts(outbio, "\n\n");

  /* ---------------------------------------------------------- *
   * Print the signature length here                            *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Length:\n%ld Bytes\n\n", sig_bytes);

  /* ---------------------------------------------------------- *
   * Print the signature data here                              *
   * ---------------------------------------------------------- */
  BIO_printf(outbio, "Signature Data:");
  if (X509_signature_dump(outbio, asn1_sig, 0) != 1)
    BIO_printf(outbio, "Error printing the signature data\n"); 

  /* ---------------------------------------------------------- *
   * Free up all structures                                     *
   * ---------------------------------------------------------- */
  X509_free(cert);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movaps 0xdf0(%rip), %xmm0      # 0x2010
leaq 0x10(%rsp), %r15
movaps %xmm0, (%r15)
movabsq $0x6d65702e656c69, %rax # imm = 0x6D65702E656C69
movq %rax, 0xd(%r15)
xorl %eax, %eax
movq %rax, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1040
movq %rax, %rdi
callq 0x10d0
movq %rax, %r14
movq 0x2d77(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
movq %rax, %rbx
movl $0x3, %edx
movq %r14, %rdi
movl $0x6c, %esi
movq %r15, %rcx
callq 0x1030
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10e0
testq %rax, %rax
jne 0x12ae
leaq 0xd90(%rip), %rsi       # 0x2025
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
movl $0x1, %edi
callq 0x10a0
movq %rax, %r15
movq %rsp, %r12
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x10c0
movq (%r12), %rax
movslq (%rax), %r12
leaq 0xd74(%rip), %rsi       # 0x2049
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
movq (%r13), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x1070
testl %eax, %eax
jg 0x1305
leaq 0xd66(%rip), %rsi       # 0x205f
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
jmp 0x1314
leaq 0xd96(%rip), %rsi       # 0x20a2
movq %rbx, %rdi
callq 0x10b0
leaq 0xd6c(%rip), %rsi       # 0x2087
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1100
leaq 0xd76(%rip), %rsi       # 0x20a5
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
movq (%rsp), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x137b
testl %eax, %eax
jne 0x135c
leaq 0xd63(%rip), %rsi       # 0x20b5
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1100
movq %r15, %rdi
callq 0x1060
movq %r14, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1050
xorl %edi, %edi
callq 0x10a0