// File: /workspace/github_c_makefile_star2_O1/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
leaq 0x160(%rsp), %rbp
movaps (%rbp), %xmm0
movaps 0x10(%rbp), %xmm1
movaps %xmm0, 0x30(%rsp)
movaps %xmm1, 0x40(%rsp)
movq %rsi, %rdi
callq 0x6be8
movq %rax, %r12
movq %rdx, %r13
leaq 0xc5a9(%rip), %rdi      # 0x157af
callq 0x6be8
movq %rdx, %rcx
movl $0x5, %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6e09
testl %eax, %eax
je 0x9358
movq %r15, %rdi
callq 0x6be8
movq %rax, %r12
movq %rdx, %r13
leaq 0xc576(%rip), %rdi      # 0x157b5
callq 0x6be8
movq %rdx, %rcx
movl $0x6, %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6e09
testl %eax, %eax
je 0x9358
leaq 0x50(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r15, %rdi
callq 0x8bc7
testl %eax, %eax
jle 0x93b4
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8239
testq %rax, %rax
je 0x93c7
movq %rax, %rbx
movq %rax, %rsi
addq $0x28, %rsi
movups 0x50(%rsp), %xmm0
movups %xmm0, 0x28(%rax)
movq 0xc8(%rax), %rax
movq %rax, %rcx
orq $0x1, %rcx
movq %rcx, 0xc8(%rbx)
cmpl $0x2, 0x2c(%rsp)
jne 0x92e3
orq $0x3, %rax
movq %rax, 0xc8(%rbx)
movl 0xc8(%rbx), %eax
andl $0x2, %eax
movq 0xc0(%rbx), %rcx
movq 0x10(%rcx), %rcx
movq %rbx, %rdi
callq *0x28(%rcx,%rax,4)
testl %eax, %eax
je 0x93de
movl %eax, %r14d
leaq 0xc4ac(%rip), %rsi      # 0x157bc
leaq 0xc293(%rip), %rdx      # 0x155aa
movl $0x4, %edi
callq 0x44b4
testl %eax, %eax
je 0x9336
leaq 0xc49c(%rip), %rdi      # 0x157c8
movl %r14d, %esi
xorl %eax, %eax
callq 0x45a0
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x9349
leaq 0xc49e(%rip), %rcx      # 0x157e4
movq %rcx, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7c27
jmp 0x93c7
movq %r14, %rdi
movq %r15, %rsi
callq 0x12afb
testq %rax, %rax
je 0x93c7
movq %rax, %r14
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x78(%rsp)
movq (%r14), %rdi
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xe616(%rip), %rsi      # 0x179b9
movq %rbx, %rdx
callq 0x91bd
movq %rax, %rbx
movq %rax, 0x28(%r14)
jmp 0x93c9
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x93c7
leaq 0xc3bb(%rip), %rcx      # 0x1577f
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 0x82af
jmp 0x93c9