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

  const char cert_filestr[] = "./demo/cert-file.pem";
  const char pkey_filestr[] = "./demo/cert-file.key";
  BIO              *certbio = NULL;
  BIO              *pkeybio = NULL;
  BIO               *outbio = NULL;
  X509                *cert = NULL;
  X509_REQ         *certreq = NULL;
  EVP_PKEY            *pkey = NULL;
  EVP_MD      const *digest = EVP_sha1();
  int ret;

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

  /* ---------------------------------------------------------- *
   * Load the old certificate from file (PEM).                  *
   * ---------------------------------------------------------- */
  ret = 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);
  }

  /* ---------------------------------------------------------- *
   * Load the original private key from file (PEM).             *
   * ---------------------------------------------------------- */
  ret = BIO_read_filename(pkeybio, pkey_filestr);
  if(ret == 0) {
    BIO_printf(outbio, "Error loading private key into BIO: %s\n", pkey_filestr);
    exit(1);
  }

  if (! (pkey = PEM_read_bio_PrivateKey(pkeybio, NULL, 0, NULL))) {
    BIO_printf(outbio, "Error loading private key into memory: %s\n", pkey_filestr);
    exit(1);
  }

  /* ---------------------------------------------------------- *
   * Convert the old certificate into a new request             *
   * Returns NULL on error                                      *
   * ---------------------------------------------------------- */
  if ((certreq = X509_to_X509_REQ(cert, pkey, digest)) == NULL) {
    BIO_printf(outbio, "Error converting certificate into request.\n");
    exit(1);
  }

  /* ---------------------------------------------------------- *
   * Print the new certificate request (PEM)                    *
   * ---------------------------------------------------------- */
  PEM_write_bio_X509_REQ(outbio, certreq);

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movaps 0xddf(%rip), %xmm0      # 0x2010
leaq 0x20(%rsp), %r13
movaps %xmm0, (%r13)
movabsq $0x6d65702e656c69, %rax # imm = 0x6D65702E656C69
movq %rax, 0xd(%r13)
movaps 0xde0(%rip), %xmm0      # 0x2030
movaps %xmm0, (%rsp)
movabsq $0x79656b2e656c69, %rax # imm = 0x79656B2E656C69
movq %rax, 0xd(%rsp)
callq 0x10b0
movq %rax, %r12
callq 0x1050
movq %rax, %rdi
callq 0x10f0
movq %rax, %r14
callq 0x1050
movq %rax, %rdi
callq 0x10f0
movq %rax, %r15
movq 0x2d3e(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
xorl %esi, %esi
callq 0x10a0
movq %rax, %rbx
movl $0x3, %edx
movq %r14, %rdi
movl $0x6c, %esi
movq %r13, %rcx
callq 0x1030
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1100
testq %rax, %rax
jne 0x12d5
leaq 0xd77(%rip), %rsi       # 0x2045
leaq 0x20(%rsp), %rdx
jmp 0x1317
movq %rax, %r13
movq %rsp, %rcx
movl $0x3, %edx
movq %r15, %rdi
movl $0x6c, %esi
callq 0x1030
testl %eax, %eax
jne 0x12fa
leaq 0xd71(%rip), %rsi       # 0x2069
jmp 0x1314
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1090
testq %rax, %rax
jne 0x132b
leaq 0xd7d(%rip), %rsi       # 0x2091
movq %rsp, %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1110
movl $0x1, %edi
callq 0x10c0
movq %rax, %rbp
movq %r13, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x10e0
testq %rax, %rax
jne 0x1354
leaq 0xd74(%rip), %rsi       # 0x20bc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1110
jmp 0x1321
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1040
movq %r13, %rdi
callq 0x1070
movq %r12, %rdi
callq 0x10d0
movq %rbp, %rdi
callq 0x1080
movq %r14, %rdi
callq 0x1060
movq %r15, %rdi
callq 0x1060
movq %rbx, %rdi
callq 0x1060
xorl %edi, %edi
callq 0x10c0