// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_bind_opt
struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address,
                                  MG_CB(mg_event_handler_t callback,
                                        void *user_data),
                                  struct mg_bind_opts opts) {
  union socket_address sa;
  struct mg_connection *nc = NULL;
  int proto, rc;
  struct mg_add_sock_opts add_sock_opts;
  char host[MG_MAX_HOST_LEN];

#if MG_ENABLE_CALLBACK_USERDATA
  opts.user_data = user_data;
#endif

  MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts);

#if MG_ENABLE_TUN
  if (mg_strncmp(mg_mk_str(address), mg_mk_str("ws://"), 5) == 0 ||
      mg_strncmp(mg_mk_str(address), mg_mk_str("wss://"), 6) == 0) {
    return mg_tun_bind_opt(mgr, address, MG_CB(callback, user_data), opts);
  }
#endif

  if (mg_parse_address(address, &sa, &proto, host, sizeof(host)) <= 0) {
    MG_SET_PTRPTR(opts.error_string, "cannot parse address");
    return NULL;
  }

  nc = mg_create_connection(mgr, callback, add_sock_opts);
  if (nc == NULL) {
    return NULL;
  }

  nc->sa = sa;
  nc->flags |= MG_F_LISTENING;
  if (proto == SOCK_DGRAM) nc->flags |= MG_F_UDP;

#if MG_ENABLE_SSL
  DBG(("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"),
       (opts.ssl_key ? opts.ssl_key : "-"),
       (opts.ssl_ca_cert ? opts.ssl_ca_cert : "-")));

  if (opts.ssl_cert != NULL || opts.ssl_ca_cert != NULL) {
    const char *err_msg = NULL;
    struct mg_ssl_if_conn_params params;
    if (nc->flags & MG_F_UDP) {
      MG_SET_PTRPTR(opts.error_string, "SSL for UDP is not supported");
      mg_destroy_conn(nc, 1 /* destroy_if */);
      return NULL;
    }
    memset(&params, 0, sizeof(params));
    params.cert = opts.ssl_cert;
    params.key = opts.ssl_key;
    params.ca_cert = opts.ssl_ca_cert;
    params.cipher_suites = opts.ssl_cipher_suites;
    if (mg_ssl_if_conn_init(nc, &params, &err_msg) != MG_SSL_OK) {
      MG_SET_PTRPTR(opts.error_string, err_msg);
      mg_destroy_conn(nc, 1 /* destroy_if */);
      return NULL;
    }
    nc->flags |= MG_F_SSL;
  }
#endif /* MG_ENABLE_SSL */

  if (nc->flags & MG_F_UDP) {
    rc = nc->iface->vtable->listen_udp(nc, &nc->sa);
  } else {
    rc = nc->iface->vtable->listen_tcp(nc, &nc->sa);
  }
  if (rc != 0) {
    DBG(("Failed to open listener: %d", rc));
    MG_SET_PTRPTR(opts.error_string, "failed to open listener");
    mg_destroy_conn(nc, 1 /* destroy_if */);
    return NULL;
  }
  mg_add_conn(nc->mgr, nc);

  return nc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
movaps 0x160(%rsp), %xmm0
movaps 0x170(%rsp), %xmm1
movaps %xmm0, 0x30(%rsp)
movaps %xmm1, 0x40(%rsp)
movq %rsi, %rdi
callq 0x6875
movq %rax, %r12
movq %rdx, %r13
leaq 0xbc24(%rip), %rdi      # 0x147b3
callq 0x6875
movq %rdx, %rcx
pushq $0x5
popq %r8
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6a46
testl %eax, %eax
je 0x8cdd
movq %r15, %rdi
callq 0x6875
movq %rax, %r12
movq %rdx, %r13
leaq 0xbbf3(%rip), %rdi      # 0x147b9
callq 0x6875
movq %rdx, %rcx
pushq $0x6
popq %r8
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6a46
testl %eax, %eax
je 0x8cdd
leaq 0x160(%rsp), %r12
leaq 0x50(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r15, %rdi
callq 0x85c7
testl %eax, %eax
jle 0x8cfc
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7d2f
testq %rax, %rax
je 0x8d10
movq %rax, %rbx
movq %rax, %rsi
addq $0x28, %rsi
movups 0x50(%rsp), %xmm0
movups %xmm0, 0x28(%rax)
movq 0xc8(%rax), %rcx
movq %rcx, %rax
orq $0x1, %rax
movq %rax, 0xc8(%rbx)
cmpl $0x2, 0x2c(%rsp)
jne 0x8c73
orq $0x3, %rcx
movq %rcx, 0xc8(%rbx)
movq %rcx, %rax
andl $0x2, %eax
movq 0xc0(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rbx, %rdi
callq *0x28(%rcx,%rax,4)
testl %eax, %eax
je 0x8d27
movl %eax, %ebp
leaq 0xbb27(%rip), %rsi      # 0x147c0
leaq 0xb90e(%rip), %rdx      # 0x145ae
pushq $0x4
popq %rdi
callq 0x4227
testl %eax, %eax
je 0x8cbc
leaq 0xbb19(%rip), %rdi      # 0x147cc
movl %ebp, %esi
xorl %eax, %eax
callq 0x430a
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x8cd0
leaq 0xbb1b(%rip), %rcx      # 0x147e8
movq %rcx, (%rax)
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x778b
jmp 0x8d10
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8d35
movq 0x10(%r12), %rax
testq %rax, %rax
je 0x8d10
leaq 0xba76(%rip), %rcx      # 0x14783
movq %rcx, (%rax)
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rbx), %rdi
movq %rbx, %rsi
callq 0x7da5
jmp 0x8d12