// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
leaq 0x40(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq 0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq 0x20(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x8(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x18(%rbp)
movq %rdi, -0x28(%rbp)
callq 0x16680
movl %eax, -0x70(%rbp)
callq 0x16680
movl %eax, -0x6c(%rbp)
callq 0x16680
movl %eax, -0x68(%rbp)
callq 0x16680
movl %eax, -0x64(%rbp)
leaq -0x60(%rbp), %rdx
leaq -0x70(%rbp), %rdi
movl $0x10, %esi
callq 0x166a0
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x8610
movq -0x80(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x16544
movq -0x98(%rbp), %rax
movq -0x80(%rbp), %rcx
movq (%rcx), %rdi
movq 0x8(%rcx), %rsi
movq (%rax), %rdx
movq 0x8(%rax), %rcx
leaq -0x40(%rbp), %r8
callq 0x15880
movq -0x28(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x38(%rbp), %rax
movl %eax, -0x9c(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x16585
leaq 0xa22f(%rip), %rax      # 0x207ab
movq %rax, -0xc0(%rbp)
jmp 0x16590
movq -0x40(%rbp), %rax
movq %rax, -0xc0(%rbp)
movl -0x9c(%rbp), %r8d
movq -0xa8(%rbp), %rcx
movl -0xac(%rbp), %edx
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %r9
leaq -0x60(%rbp), %rax
leaq 0x99fd(%rip), %rsi      # 0x1ffba
movq %rax, (%rsp)
movb $0x0, %al
callq 0xb2f0
cmpq $0x0, -0x18(%rbp)
jbe 0x165f6
movq -0x10(%rbp), %rax
movq -0x20(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rdi
movl -0x74(%rbp), %edx
movq -0x20(%rbp), %rcx
leaq 0x9851(%rip), %rsi      # 0x1fe40
movb $0x0, %al
callq 0xb2f0
movq -0x88(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x16626
movq -0x88(%rbp), %rax
movq -0x28(%rbp), %rdi
movq 0x8(%rax), %rcx
movl %ecx, %edx
movq (%rax), %rcx
leaq 0x9a0e(%rip), %rsi      # 0x2002d
movb $0x0, %al
callq 0xb2f0
movq -0x90(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x16656
movq -0x90(%rbp), %rax
movq -0x28(%rbp), %rdi
movq 0x8(%rax), %rcx
movl %ecx, %edx
movq (%rax), %rcx
leaq 0x9599(%rip), %rsi      # 0x1fbe8
movb $0x0, %al
callq 0xb2f0
movq -0x28(%rbp), %rdi
leaq 0xa148(%rip), %rsi      # 0x207a9
movb $0x0, %al
callq 0xb2f0
leaq -0x40(%rbp), %rdi
callq 0x86e0
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)