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

  const char cert_filestr[] = "./demo/cert-file.pem";
  ASN1_INTEGER *asn1_serial = NULL;
  BIO              *certbio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;
  const char *neg;
  int  i;
  long l;

  /* ---------------------------------------------------------- *
   * 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 serial number.                   *
   * ---------------------------------------------------------- */
   asn1_serial = X509_get_serialNumber(cert);
   if (asn1_serial == NULL)
     BIO_printf(outbio, "Error getting serial number from certificate");

  /* ---------------------------------------------------------- *
   * Print the serial number value, openssl x509 -serial style  *
   * ---------------------------------------------------------- */
  BIO_puts(outbio,"serial (openssl x509 -serial style): ");
  i2a_ASN1_INTEGER(outbio, asn1_serial);
  BIO_puts(outbio,"\n");

  /* ---------------------------------------------------------- *
   * Print the serial number value, openssl x509 -text style    *
   * ---------------------------------------------------------- */
  if (asn1_serial->length <= (int)sizeof(long)) {
    l=ASN1_INTEGER_get(asn1_serial);
    if (asn1_serial->type == V_ASN1_NEG_INTEGER) {
      l= -l;
      neg="-";
    }
    else neg="";

    if (BIO_printf(outbio," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
      BIO_printf(outbio, "Error during printing the serial.\n");
  } else {
    neg=(asn1_serial->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
    //if (BIO_printf(outbio,"\n%12s%s","",neg) <= 0)
    if (BIO_printf(outbio,"serial (openssl x509 -text   style): %s ",neg) <= 0)
      BIO_printf(outbio, "Error during printing the serial.\n");

    for (i=0; i<asn1_serial->length; i++) {
     if (BIO_printf(outbio,"%02x%c",asn1_serial->data[i],
        ((i+1 == asn1_serial->length)?'\n':':')) <= 0)
      BIO_printf(outbio, "Error during printing the serial.\n");
    }
  }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movq 0xdfa(%rip), %rax       # 0x2010
movq %rax, -0x20(%rbp)
movq 0xdf7(%rip), %rax       # 0x2018
movq %rax, -0x18(%rbp)
movl 0xdf5(%rip), %eax       # 0x2020
movl %eax, -0x10(%rbp)
movb 0xdf0(%rip), %al        # 0x2024
movb %al, -0xc(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
callq 0x1050
movq %rax, %rdi
callq 0x10d0
movq %rax, -0x30(%rbp)
movq 0x2d61(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x1080
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x20(%rbp), %rcx
movl $0x6c, %esi
movl $0x3, %edx
callq 0x1040
movq -0x30(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x10e0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x12d1
movq -0x38(%rbp), %rdi
leaq -0x20(%rbp), %rdx
leaq 0xd65(%rip), %rsi       # 0x2025
movb $0x0, %al
callq 0x10f0
movl $0x1, %edi
callq 0x1090
movq -0x40(%rbp), %rdi
callq 0x10c0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x12f7
movq -0x38(%rbp), %rdi
leaq 0xd59(%rip), %rsi       # 0x2049
movb $0x0, %al
callq 0x10f0
movq -0x38(%rbp), %rdi
leaq 0xd74(%rip), %rsi       # 0x2076
callq 0x10a0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x10b0
movq -0x38(%rbp), %rdi
leaq 0xd8f(%rip), %rsi       # 0x20ae
callq 0x10a0
movq -0x28(%rbp), %rax
cmpl $0x8, (%rax)
jg 0x13a7
movq -0x28(%rbp), %rdi
callq 0x1030
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x102, 0x4(%rax)       # imm = 0x102
jne 0x135e
xorl %eax, %eax
subq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
leaq 0xd44(%rip), %rax       # 0x209c
movq %rax, -0x48(%rbp)
jmp 0x1369
leaq 0xd4a(%rip), %rax       # 0x20af
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x58(%rbp), %r9
leaq 0xd1a(%rip), %rsi       # 0x209e
movb $0x0, %al
callq 0x10f0
cmpl $0x0, %eax
jg 0x13a2
movq -0x38(%rbp), %rdi
leaq 0xd15(%rip), %rsi       # 0x20b0
movb $0x0, %al
callq 0x10f0
jmp 0x146e
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0xcfa(%rip), %rax       # 0x20af
leaq 0xd17(%rip), %rcx       # 0x20d3
cmpl $0x102, %edx            # imm = 0x102
cmoveq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0xd06(%rip), %rsi       # 0x20df
movb $0x0, %al
callq 0x10f0
cmpl $0x0, %eax
jg 0x13f7
movq -0x38(%rbp), %rdi
leaq 0xcc0(%rip), %rsi       # 0x20b0
movb $0x0, %al
callq 0x10f0
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x146c
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x4c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
movl -0x4c(%rbp), %esi
addl $0x1, %esi
movq -0x28(%rbp), %rax
movl (%rax), %r8d
movl $0x3a, %ecx
movl $0xa, %eax
cmpl %r8d, %esi
cmovel %eax, %ecx
leaq 0xcc7(%rip), %rsi       # 0x2108
movb $0x0, %al
callq 0x10f0
cmpl $0x0, %eax
jg 0x145f
movq -0x38(%rbp), %rdi
leaq 0xc58(%rip), %rsi       # 0x20b0
movb $0x0, %al
callq 0x10f0
jmp 0x1461
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x13fe
jmp 0x146e
movq -0x40(%rbp), %rdi
callq 0x1070
movq -0x30(%rbp), %rdi
callq 0x1060
movq -0x38(%rbp), %rdi
callq 0x1060
xorl %edi, %edi
callq 0x1090