// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: set_ssl_option
static int set_ssl_option(struct mg_context *ctx) {
  struct mg_request_info request_info;
  SSL_CTX *CTX;
  int i, size;
  const char *pem = ctx->config[SSL_CERTIFICATE];
  const char *chain = ctx->config[SSL_CHAIN_FILE];

  if (pem == NULL) {
    return 1;
  }

#if !defined(NO_SSL_DL)
  if (!load_dll(ctx, SSL_LIB, ssl_sw) ||
      !load_dll(ctx, CRYPTO_LIB, crypto_sw)) {
    return 0;
  }
#endif // NO_SSL_DL

  // Initialize SSL crap
  SSL_library_init();
  SSL_load_error_strings();

  if ((CTX = SSL_CTX_new(SSLv23_server_method())) == NULL) {
    cry(fc(ctx), "SSL_CTX_new error: %s", ssl_error());
  } else if (ctx->user_callback != NULL) {
    memset(&request_info, 0, sizeof(request_info));
    request_info.user_data = ctx->user_data;
    ctx->user_callback(MG_INIT_SSL, (struct mg_connection *) CTX,
                       &request_info);
  }

  if (CTX != NULL && SSL_CTX_use_certificate_file(CTX, pem,
        SSL_FILETYPE_PEM) == 0) {
    cry(fc(ctx), "%s: cannot open %s: %s", __func__, pem, ssl_error());
    return 0;
  } else if (CTX != NULL && SSL_CTX_use_PrivateKey_file(CTX, pem,
        SSL_FILETYPE_PEM) == 0) {
    cry(fc(ctx), "%s: cannot open %s: %s", NULL, pem, ssl_error());
    return 0;
  }

  if (CTX != NULL && chain != NULL &&
      SSL_CTX_use_certificate_chain_file(CTX, chain) == 0) {
    cry(fc(ctx), "%s: cannot open %s: %s", NULL, chain, ssl_error());
    return 0;
  }

  // Initialize locking callbacks, needed for thread safety.
  // http://www.openssl.org/support/faq.html#PROG1
  size = sizeof(pthread_mutex_t) * CRYPTO_num_locks();
  if ((ssl_mutexes = (pthread_mutex_t *) malloc((size_t)size)) == NULL) {
    cry(fc(ctx), "%s: cannot allocate mutexes: %s", __func__, ssl_error());
    return 0;
  }

  for (i = 0; i < CRYPTO_num_locks(); i++) {
    pthread_mutex_init(&ssl_mutexes[i], NULL);
  }

  CRYPTO_set_locking_callback(&ssl_locking_callback);
  CRYPTO_set_id_callback(&ssl_id_callback);

  // Done with everything. Save the context.
  ctx->ssl_ctx = CTX;

  return 1;
}

// Assembly:
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, 0x4c8(%rsp)
movq 0x4c8(%rsp), %rax
movq 0xa8(%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x4c8(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x60(%rsp)
jne 0x1883c
movl $0x1, 0x4d4(%rsp)
jmp 0x18b95
movq 0x4c8(%rsp), %rdi
leaq 0xa2ff(%rip), %rsi      # 0x22b4a
leaq 0x1307e(%rip), %rdx     # 0x2b8d0
callq 0x1a930
cmpl $0x0, %eax
je 0x1887c
movq 0x4c8(%rsp), %rdi
leaq 0xa2e9(%rip), %rsi      # 0x22b54
leaq 0x1318e(%rip), %rdx     # 0x2ba00
callq 0x1a930
cmpl $0x0, %eax
jne 0x1888c
movl $0x0, 0x4d4(%rsp)
jmp 0x18b95
callq *0x130e6(%rip)          # 0x2b978
callq *0x13130(%rip)          # 0x2b9c8
movq 0x130b9(%rip), %rax     # 0x2b958
movq %rax, 0x50(%rsp)
callq *0x130be(%rip)          # 0x2b968
movq %rax, %rdi
movq 0x50(%rsp), %rax
callq *%rax
movq %rax, 0x70(%rsp)
cmpq $0x0, %rax
jne 0x188ee
movq 0x4c8(%rsp), %rdi
callq 0x18750
movq %rax, 0x48(%rsp)
callq 0x1aa30
movq 0x48(%rsp), %rdi
movq %rax, %rdx
leaq 0xa27c(%rip), %rsi      # 0x22b61
movb $0x0, %al
callq 0x184e0
jmp 0x18947
movq 0x4c8(%rsp), %rax
cmpq $0x0, 0xc0(%rax)
je 0x18945
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
movl $0x450, %edx            # imm = 0x450
callq 0x52a0
movq 0x4c8(%rsp), %rax
movq 0xc8(%rax), %rax
movq %rax, 0x78(%rsp)
movq 0x4c8(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x70(%rsp), %rsi
movl $0x3, %edi
leaq 0x78(%rsp), %rdx
callq *%rax
jmp 0x18947
cmpq $0x0, 0x70(%rsp)
je 0x189bf
movq 0x13042(%rip), %rax     # 0x2b998
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl $0x1, %edx
callq *%rax
cmpl $0x0, %eax
jne 0x189bf
movq 0x4c8(%rsp), %rdi
callq 0x18750
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x40(%rsp)
callq 0x1aa30
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rcx
movq %rax, %r8
leaq 0xa1d6(%rip), %rsi      # 0x22b77
leaq 0xa1e6(%rip), %rdx      # 0x22b8e
movb $0x0, %al
callq 0x184e0
movl $0x0, 0x4d4(%rsp)
jmp 0x18b95
cmpq $0x0, 0x70(%rsp)
je 0x18a34
movq 0x12fba(%rip), %rax     # 0x2b988
movq 0x70(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl $0x1, %edx
callq *%rax
cmpl $0x0, %eax
jne 0x18a34
movq 0x4c8(%rsp), %rdi
callq 0x18750
movq %rax, 0x28(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x30(%rsp)
callq 0x1aa30
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rcx
movq %rax, %r8
leaq 0xa15e(%rip), %rsi      # 0x22b77
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x184e0
movl $0x0, 0x4d4(%rsp)
jmp 0x18b95
jmp 0x18a36
cmpq $0x0, 0x70(%rsp)
je 0x18aae
cmpq $0x0, 0x58(%rsp)
je 0x18aae
movq 0x12f8b(%rip), %rax     # 0x2b9d8
movq 0x70(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq *%rax
cmpl $0x0, %eax
jne 0x18aae
movq 0x4c8(%rsp), %rdi
callq 0x18750
movq %rax, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x20(%rsp)
callq 0x1aa30
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq %rax, %r8
leaq 0xa0e4(%rip), %rsi      # 0x22b77
xorl %eax, %eax
movl %eax, %edx
movb $0x0, %al
callq 0x184e0
movl $0x0, 0x4d4(%rsp)
jmp 0x18b95
callq *0x12f54(%rip)          # 0x2ba08
cltq
imulq $0x28, %rax, %rax
movl %eax, 0x68(%rsp)
movslq 0x68(%rsp), %rdi
callq 0x5790
movq %rax, 0x13091(%rip)     # 0x2bb60
cmpq $0x0, %rax
jne 0x18b16
movq 0x4c8(%rsp), %rdi
callq 0x18750
movq %rax, 0x10(%rsp)
callq 0x1aa30
movq 0x10(%rsp), %rdi
movq %rax, %rcx
leaq 0xa0a2(%rip), %rsi      # 0x22b9d
leaq 0xa08c(%rip), %rdx      # 0x22b8e
movb $0x0, %al
callq 0x184e0
movl $0x0, 0x4d4(%rsp)
jmp 0x18b95
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0xc(%rsp)
callq *0x12edc(%rip)          # 0x2ba08
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x18b5f
movq 0x13023(%rip), %rdi     # 0x2bb60
movslq 0x6c(%rsp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x58d0
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x18b1e
leaq 0x1f1a(%rip), %rdi      # 0x1aa80
callq *0x12eac(%rip)          # 0x2ba18
leaq 0x1f7d(%rip), %rdi      # 0x1aaf0
callq *0x12eaf(%rip)          # 0x2ba28
movq 0x70(%rsp), %rcx
movq 0x4c8(%rsp), %rax
movq %rcx, 0x8(%rax)
movl $0x1, 0x4d4(%rsp)
movl 0x4d4(%rsp), %eax
addq $0x4d8, %rsp            # imm = 0x4D8
retq
nopw %cs:(%rax,%rax)