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

  MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts);

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

  if ((rc = mg_parse_address(address, &nc->sa, &proto, host, sizeof(host))) <
      0) {
    /* Address is malformed */
    MG_SET_PTRPTR(opts.error_string, "cannot parse address");
    mg_destroy_conn(nc, 1 /* destroy_if */);
    return NULL;
  }

  nc->flags |= opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK;
  nc->flags |= (proto == SOCK_DGRAM) ? MG_F_UDP : 0;
#if MG_ENABLE_CALLBACK_USERDATA
  nc->user_data = user_data;
#else
  nc->user_data = opts.user_data;
#endif

#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 ||
      opts.ssl_psk_identity != 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;
    params.psk_identity = opts.ssl_psk_identity;
    params.psk_key = opts.ssl_psk_key;
    if (opts.ssl_ca_cert != NULL) {
      if (opts.ssl_server_name != NULL) {
        if (strcmp(opts.ssl_server_name, "*") != 0) {
          params.server_name = opts.ssl_server_name;
        }
      } else if (rc == 0) { /* If it's a DNS name, use host. */
        params.server_name = host;
      }
    }
    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 (rc == 0) {
#if MG_ENABLE_ASYNC_RESOLVER
    /*
     * DNS resolution is required for host.
     * mg_parse_address() fills port in nc->sa, which we pass to resolve_cb()
     */
    struct mg_connection *dns_conn = NULL;
    struct mg_resolve_async_opts o;
    memset(&o, 0, sizeof(o));
    o.dns_conn = &dns_conn;
    o.nameserver = opts.nameserver;
    if (mg_resolve_async_opt(nc->mgr, host, MG_DNS_A_RECORD, resolve_cb, nc,
                             o) != 0) {
      MG_SET_PTRPTR(opts.error_string, "cannot schedule DNS lookup");
      mg_destroy_conn(nc, 1 /* destroy_if */);
      return NULL;
    }
    nc->priv_2 = dns_conn;
    nc->flags |= MG_F_RESOLVING;
    return nc;
#else
    MG_SET_PTRPTR(opts.error_string, "Resolver is disabled");
    mg_destroy_conn(nc, 1 /* destroy_if */);
    return NULL;
#endif
  } else {
    /* Address is parsed and resolved to IP. proceed with connect() */
    return mg_do_connect(nc, proto, &nc->sa);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
leaq 0x10(%rbp), %rax
movq %rax, -0x150(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq (%rax), %rcx
movq %rcx, -0x50(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x48(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x50(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0xb500
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0xc193
movq $0x0, -0x8(%rbp)
jmp 0xc37b
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x28, %rsi
leaq -0x120(%rbp), %rcx
leaq -0x2c(%rbp), %rdx
movl $0xc8, %r8d
callq 0xc390
movl %eax, -0x30(%rbp)
cmpl $0x0, %eax
jge 0xc1ff
jmp 0xc1bf
movq -0x150(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc1e2
movq -0x150(%rbp), %rax
movq 0x10(%rax), %rax
leaq 0x13540(%rip), %rcx     # 0x1f71f
movq %rcx, (%rax)
jmp 0xc1e4
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xabf0
movq $0x0, -0x8(%rbp)
jmp 0xc37b
movq -0x150(%rbp), %rax
movl 0x8(%rax), %ecx
andl $0x3f05000, %ecx        # imm = 0x3F05000
movl %ecx, %ecx
movl %ecx, %edx
movq -0x28(%rbp), %rcx
orq 0xc8(%rcx), %rdx
movq %rdx, 0xc8(%rcx)
movl -0x2c(%rbp), %esi
xorl %ecx, %ecx
movl $0x2, %edx
cmpl $0x2, %esi
cmovel %edx, %ecx
movslq %ecx, %rdx
movq -0x28(%rbp), %rcx
orq 0xc8(%rcx), %rdx
movq %rdx, 0xc8(%rcx)
movq (%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xa0(%rax)
cmpl $0x0, -0x30(%rbp)
jne 0xc363
movq $0x0, -0x128(%rbp)
leaq -0x148(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3260
movq -0x150(%rbp), %rax
leaq -0x128(%rbp), %rcx
movq %rcx, -0x130(%rbp)
movq 0x20(%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x120(%rbp), %rsi
movq -0x28(%rbp), %r8
movl $0x1, %edx
leaq 0x581(%rip), %rcx       # 0xc840
leaq -0x148(%rbp), %rax
movq (%rax), %r9
movq %r9, (%rsp)
movq 0x8(%rax), %r9
movq %r9, 0x8(%rsp)
movq 0x10(%rax), %r9
movq %r9, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0xc610
cmpl $0x0, %eax
je 0xc331
jmp 0xc2f4
movq -0x150(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0xc317
movq -0x150(%rbp), %rax
movq 0x10(%rax), %rax
leaq 0x13420(%rip), %rcx     # 0x1f734
movq %rcx, (%rax)
jmp 0xc319
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xabf0
movq $0x0, -0x8(%rbp)
jmp 0xc37b
movq -0x128(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x28(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x4, %rcx
movq %rcx, 0xc8(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xc37b
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x28(%rbp), %rdx
addq $0x28, %rdx
callq 0xc9c0
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopl (%rax,%rax)