// File: /workspace/github_c_makefile_star2_O3/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 0x6b26
movq %rax, %r12
movq %rdx, %r13
leaq 0xc70e(%rip), %rdi      # 0x157d3
callq 0x6b26
movq %rdx, %rcx
movl $0x5, %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6d47
testl %eax, %eax
je 0x9214
movq %r15, %rdi
callq 0x6b26
movq %rax, %r12
movq %rdx, %r13
leaq 0xc6db(%rip), %rdi      # 0x157d9
callq 0x6b26
movq %rdx, %rcx
movl $0x6, %r8d
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x6d47
testl %eax, %eax
je 0x9214
leaq 0x50(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %r15, %rdi
callq 0x8a6c
testl %eax, %eax
jle 0x9270
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x8158
testq %rax, %rax
je 0x9283
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 0x91a5
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 0x929a
movl %eax, %r14d
leaq 0xc614(%rip), %rsi      # 0x157e0
leaq 0xc3fb(%rip), %rdx      # 0x155ce
movl $0x4, %edi
callq 0x4447
testl %eax, %eax
je 0x91f2
leaq 0xc604(%rip), %rdi      # 0x157ec
movl %r14d, %esi
xorl %eax, %eax
callq 0x4539
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x9205
leaq 0xc606(%rip), %rcx      # 0x15808
movq %rcx, (%rax)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x7b4f
jmp 0x9283
movq %r14, %rdi
movq %r15, %rsi
callq 0x12fe2
testq %rax, %rax
je 0x9283
movq %rax, %r14
movq 0x10(%rbp), %rax
movq %rax, 0x70(%rsp)
movups (%rbp), %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x8(%r14), %rax
movq %rax, 0x78(%rsp)
movq (%r14), %rdi
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xe778(%rip), %rsi      # 0x179d7
movq %rbx, %rdx
callq 0x907c
movq %rax, %rbx
movq %rax, 0x28(%r14)
jmp 0x9285
movq 0x10(%rbp), %rax
testq %rax, %rax
je 0x9283
leaq 0xc523(%rip), %rcx      # 0x157a3
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 0x81bd
jmp 0x9285