// File: /workspace/github_c_makefile_star2_O3/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 0x4746
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x68dd
movq 0xc8(%rsp), %rsi
testq %rsi, %rsi
je 0x1092d
leaq 0xd0(%rsp), %rax
leaq 0xc0(%rsp), %rcx
movq (%rcx), %rdi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
leaq 0x18(%rsp), %r8
callq 0xff50
leaq 0xa0(%rsp), %r14
movl 0x20(%rsp), %r8d
movq 0x18(%rsp), %rax
testq %rax, %rax
leaq 0x5ea4(%rip), %r9       # 0x167ed
cmovneq %rax, %r9
movq %rbp, (%rsp)
leaq 0x56e3(%rip), %rsi      # 0x1603b
movq %rbx, %rdi
movl %r13d, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0x7ff5
testq %r12, %r12
je 0x10989
movq 0x10(%rsp), %rcx
subl %ecx, %r15d
leaq 0x5545(%rip), %rsi      # 0x15ec1
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x7ff5
leaq 0xb0(%rsp), %r15
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x109ae
movq (%r14), %rcx
leaq 0x570a(%rip), %rsi      # 0x160ae
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7ff5
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0x109cb
movq (%r15), %rcx
leaq 0x52a8(%rip), %rsi      # 0x15c69
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7ff5
leaq 0x5e19(%rip), %rsi      # 0x167eb
movq %rbx, %rdi
xorl %eax, %eax
callq 0x7ff5
leaq 0x18(%rsp), %rdi
callq 0x6934
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq