// File: /workspace/github_c_makefile_star2_O2/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)
pushq $0x10
popq %rsi
leaq 0x40(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rdx
callq 0x4509
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x662f
movq 0xc8(%rsp), %rsi
testq %rsi, %rsi
je 0xfe00
leaq 0xd0(%rsp), %rax
leaq 0xc0(%rsp), %rcx
movq (%rcx), %rdi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
leaq 0x18(%rsp), %r8
callq 0xf5ca
leaq 0xa0(%rsp), %r14
movl 0x20(%rsp), %r8d
movq 0x18(%rsp), %rax
testq %rax, %rax
leaq 0x59b4(%rip), %r9       # 0x157d0
cmovneq %rax, %r9
movq %rbp, (%rsp)
leaq 0x51f3(%rip), %rsi      # 0x1501e
movq %rbx, %rdi
movl %r13d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x7bce
testq %r12, %r12
je 0xfe5c
movq 0x10(%rsp), %rcx
subl %ecx, %r15d
leaq 0x5055(%rip), %rsi      # 0x14ea4
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x7bce
leaq 0xb0(%rsp), %r15
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0xfe81
movq (%r14), %rcx
leaq 0x521a(%rip), %rsi      # 0x15091
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0xfe9e
movq (%r15), %rcx
leaq 0x4db8(%rip), %rsi      # 0x14c4c
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0x5929(%rip), %rsi      # 0x157ce
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7bce
leaq 0x18(%rsp), %rdi
callq 0x6683
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq