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

  STACK_OF(X509_INFO) *certstack;
  const char   ca_filestr[] = "./demo/ca-bundle.pem";
  X509_INFO *stack_item     = NULL;
  X509_NAME    *certsubject = NULL;
  BIO             *stackbio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;
  int i;

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

  /* ---------------------------------------------------------- *
   * Load the file with the list of certificates in PEM format  *
   * ---------------------------------------------------------- */
  if (BIO_read_filename(stackbio, ca_filestr) <= 0) {
    BIO_printf(outbio, "Error loading cert bundle into memory: %s\n", ca_filestr);
    exit(1);
  }

  certstack = PEM_X509_INFO_read_bio(stackbio, NULL, NULL, NULL);

  /* ---------------------------------------------------------- *
   * Count the number of certs that are now on the stack        *
   * ---------------------------------------------------------- */
   BIO_printf(outbio, "# of stack certs: %d\n",
                      sk_X509_INFO_num(certstack));

  /* ---------------------------------------------------------- *
   * Cycle through the stack to display various cert data       *
   * ---------------------------------------------------------- */
  for (i = 0; i < sk_X509_INFO_num(certstack); i++) {
    char subject_cn[256] = "** n/a **";
    long cert_version;

    stack_item = sk_X509_INFO_value(certstack, i);

    certsubject = X509_get_subject_name(stack_item->x509);
    X509_NAME_get_text_by_NID(certsubject, NID_commonName,
                                           subject_cn, 256);
    cert_version = (X509_get_version(stack_item->x509)+1);

    BIO_printf(outbio, "Cert #%.2d v%ld CN: %.70s\n", i, 
                                  cert_version, subject_cn);
  }

  /* ---------------------------------------------------------- *
   * Free up the resources                                      *
   * ---------------------------------------------------------- */
  sk_X509_INFO_pop_free(certstack, X509_INFO_free);
  X509_free(cert);
  BIO_free_all(stackbio);
  BIO_free_all(outbio);
  exit(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movl $0x0, -0x4(%rbp)
movq 0xdc7(%rip), %rax       # 0x2010
movq %rax, -0x30(%rbp)
movq 0xdc4(%rip), %rax       # 0x2018
movq %rax, -0x28(%rbp)
movl 0xdc2(%rip), %eax       # 0x2020
movl %eax, -0x20(%rbp)
movb 0xdbd(%rip), %al        # 0x2024
movb %al, -0x1c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
callq 0x1040
movq %rax, %rdi
callq 0x1100
movq %rax, -0x48(%rbp)
movq 0x2d1e(%rip), %rax      # 0x3fc8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1090
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rcx
movl $0x6c, %esi
movl $0x3, %edx
callq 0x1030
cmpl $0x0, %eax
jg 0x12f4
movq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
leaq 0xd42(%rip), %rsi       # 0x2025
movb $0x0, %al
callq 0x1120
movl $0x1, %edi
callq 0x10e0
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x1050
movq %rax, -0x10(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1460
movq %rax, %rdi
callq 0x1070
movq -0x170(%rbp), %rdi
movl %eax, %edx
leaq 0xd19(%rip), %rsi       # 0x2050
movb $0x0, %al
callq 0x1120
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x174(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1460
movq %rax, %rdi
callq 0x1070
movl %eax, %ecx
movl -0x174(%rbp), %eax
cmpl %ecx, %eax
jge 0x1411
leaq -0x160(%rbp), %rdi
leaq 0xd13(%rip), %rsi       # 0x2090
movl $0x100, %edx            # imm = 0x100
callq 0x10a0
movq -0x10(%rbp), %rdi
callq 0x1460
movq %rax, %rdi
movl -0x5c(%rbp), %esi
callq 0x10f0
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x10b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq -0x160(%rbp), %rdx
movl $0xd, %esi
movl $0x100, %ecx            # imm = 0x100
callq 0x10c0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x10d0
addq $0x1, %rax
movq %rax, -0x168(%rbp)
movq -0x50(%rbp), %rdi
movl -0x5c(%rbp), %edx
movq -0x168(%rbp), %rcx
leaq -0x160(%rbp), %r8
leaq 0xc6a(%rip), %rsi       # 0x2066
movb $0x0, %al
callq 0x1120
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1345
movq -0x10(%rbp), %rdi
callq 0x1470
movq %rax, -0x180(%rbp)
movq 0x2ba8(%rip), %rdi      # 0x3fd0
callq 0x1480
movq -0x180(%rbp), %rdi
movq %rax, %rsi
callq 0x1110
movq -0x58(%rbp), %rdi
callq 0x1080
movq -0x48(%rbp), %rdi
callq 0x1060
movq -0x50(%rbp), %rdi
callq 0x1060
xorl %edi, %edi
callq 0x10e0
nop