// File: /workspace/github_c_makefile_star2_O1/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_send_websocket_handshake3v
void mg_send_websocket_handshake3v(struct mg_connection *nc,
                                   const struct mg_str path,
                                   const struct mg_str host,
                                   const struct mg_str protocol,
                                   const struct mg_str extra_headers,
                                   const struct mg_str user,
                                   const struct mg_str pass) {
  struct mbuf auth;
  char key[25];
  uint32_t nonce[4];
  nonce[0] = mg_ws_random_mask();
  nonce[1] = mg_ws_random_mask();
  nonce[2] = mg_ws_random_mask();
  nonce[3] = mg_ws_random_mask();
  mg_base64_encode((unsigned char *) &nonce, sizeof(nonce), key);

  mbuf_init(&auth, 0);
  if (user.len > 0) {
    mg_basic_auth_header(user, pass, &auth);
  }

  /*
   * NOTE: the  (auth.buf == NULL ? "" : auth.buf) is because cc3200 libc is
   * broken: it doesn't like zero length to be passed to %.*s
   * i.e. sprintf("f%.*so", (int)0, NULL), yields `f\0o`.
   * because it handles NULL specially (and incorrectly).
   */
  mg_printf(nc,
            "GET %.*s HTTP/1.1\r\n"
            "Upgrade: websocket\r\n"
            "Connection: Upgrade\r\n"
            "%.*s"
            "Sec-WebSocket-Version: 13\r\n"
            "Sec-WebSocket-Key: %s\r\n",
            (int) path.len, path.p, (int) auth.len,
            (auth.buf == NULL ? "" : auth.buf), key);

  /* TODO(mkm): take default hostname from http proto data if host == NULL */
  if (host.len > 0) {
    int host_len = (int) (path.p - host.p); /* Account for possible :PORT */
    mg_printf(nc, "Host: %.*s\r\n", host_len, host.p);
  }
  if (protocol.len > 0) {
    mg_printf(nc, "Sec-WebSocket-Protocol: %.*s\r\n", (int) protocol.len,
              protocol.p);
  }
  if (extra_headers.len > 0) {
    mg_printf(nc, "%.*s", (int) extra_headers.len, extra_headers.p);
  }
  mg_printf(nc, "\r\n");

  mbuf_free(&auth);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r12
movq %rcx, 0x10(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %rbx
callq 0x3640
leaq 0x30(%rsp), %r14
movl %eax, (%r14)
callq 0x3640
movl %eax, 0x4(%r14)
callq 0x3640
movl %eax, 0x8(%r14)
callq 0x3640
movl %eax, 0xc(%r14)
leaq 0x40(%rsp), %rbp
movq %r14, %rdi
movl $0x10, %esi
movq %rbp, %rdx
callq 0x47a8
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x69aa
movq 0xc8(%rsp), %rsi
testq %rsi, %rsi
je 0x1094c
leaq 0xd0(%rsp), %rax
leaq 0xc0(%rsp), %rcx
movq (%rcx), %rdi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
leaq 0x18(%rsp), %r8
callq 0x10064
leaq 0xa0(%rsp), %r14
movl 0x20(%rsp), %r8d
movq 0x18(%rsp), %rax
testq %rax, %rax
leaq 0x5e67(%rip), %r9       # 0x167cf
cmovneq %rax, %r9
movq %rbp, (%rsp)
leaq 0x56a3(%rip), %rsi      # 0x1601a
movq %rbx, %rdi
movl %r13d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x80d3
testq %r12, %r12
je 0x109a8
movq 0x10(%rsp), %rcx
subl %ecx, %r15d
leaq 0x5505(%rip), %rsi      # 0x15ea0
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x80d3
leaq 0xb0(%rsp), %r15
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x109cd
movq (%r14), %rcx
leaq 0x56ca(%rip), %rsi      # 0x1608d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x80d3
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0x109ea
movq (%r15), %rcx
leaq 0x5268(%rip), %rsi      # 0x15c48
movq %rbx, %rdi
xorl %eax, %eax
callq 0x80d3
leaq 0x5ddc(%rip), %rsi      # 0x167cd
movq %rbx, %rdi
xorl %eax, %eax
callq 0x80d3
leaq 0x18(%rsp), %rdi
callq 0x6a01
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq