// File: /workspace/github_c_makefile_star2_O0/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_deliver_websocket_data
static int mg_deliver_websocket_data(struct mg_connection *nc) {
  /* Using unsigned char *, cause of integer arithmetic below */
  uint64_t i, data_len = 0, frame_len = 0, buf_len = nc->recv_mbuf.len, len,
              mask_len = 0, header_len = 0;
  unsigned char *p = (unsigned char *) nc->recv_mbuf.buf, *buf = p,
                *e = p + buf_len;
  struct mg_ws_proto_data *wsd = mg_ws_get_proto_data(nc);
  int ok;
  int reass = buf_len > 0 && mg_is_ws_fragment(p[0]) &&
              !(nc->flags & MG_F_WEBSOCKET_NO_DEFRAG);

  /* If that's a continuation frame that must be reassembled, handle it */
  if (reass && !mg_is_ws_first_fragment(p[0]) && buf_len >= 1 &&
      buf_len >= 1 + wsd->reass_len) {
    buf += 1 + wsd->reass_len;
    buf_len -= 1 + wsd->reass_len;
  }

  if (buf_len >= 2) {
    len = buf[1] & 0x7f;
    mask_len = buf[1] & 0x80 ? 4 : 0;
    if (len < 126 && buf_len >= mask_len) {
      data_len = len;
      header_len = 2 + mask_len;
    } else if (len == 126 && buf_len >= 4 + mask_len) {
      header_len = 4 + mask_len;
      data_len = ntohs(*(uint16_t *) &buf[2]);
    } else if (buf_len >= 10 + mask_len) {
      header_len = 10 + mask_len;
      data_len = (((uint64_t) ntohl(*(uint32_t *) &buf[2])) << 32) +
                 ntohl(*(uint32_t *) &buf[6]);
    }
  }

  frame_len = header_len + data_len;
  ok = (frame_len > 0 && frame_len <= buf_len);

  /* Check for overflow */
  if (frame_len < header_len || frame_len < data_len) {
    ok = 0;
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
  }

  if (ok) {
    struct websocket_message wsm;

    wsm.size = (size_t) data_len;
    wsm.data = buf + header_len;
    wsm.flags = buf[0];

    /* Apply mask if necessary */
    if (mask_len > 0) {
      for (i = 0; i < data_len; i++) {
        buf[i + header_len] ^= (buf + header_len - mask_len)[i % 4];
      }
    }

    if (reass) {
      /* On first fragmented frame, nullify size */
      if (mg_is_ws_first_fragment(wsm.flags)) {
        p[0] &= ~0x0f; /* Next frames will be treated as continuation */
        buf = p + 1;
        wsd->reass_len = 0;
      }

      /* Append this frame to the reassembled buffer */
      memmove(buf, wsm.data, e - wsm.data);
      wsd->reass_len += wsm.size;
      nc->recv_mbuf.len -= wsm.data - buf;

      /* On last fragmented frame - call user handler and remove data */
      if (wsm.flags & 0x80) {
        wsm.data = p + 1;
        wsm.size = wsd->reass_len;
        mg_handle_incoming_websocket_frame(nc, &wsm);
        mbuf_remove(&nc->recv_mbuf, 1 + wsd->reass_len);
        wsd->reass_len = 0;
      }
    } else {
      /* TODO(lsm): properly handle OOB control frames during defragmentation */
      mg_handle_incoming_websocket_frame(nc, &wsm);
      mbuf_remove(&nc->recv_mbuf, (size_t) frame_len); /* Cleanup frame */
      wsd->reass_len = 0;
    }

    /* If the frame is not reassembled - client closes and close too */
    if (!reass && (buf[0] & 0x0f) == WEBSOCKET_OP_CLOSE) {
      nc->flags |= MG_F_SEND_AND_CLOSE;
    }
  }

  return ok;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1cbc0
movq %rax, -0x60(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x28(%rbp)
movb %al, -0x81(%rbp)
jbe 0x1c832
movq -0x48(%rbp), %rax
movzbl (%rax), %edi
callq 0x1cc00
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x81(%rbp)
je 0x1c832
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rax
andq $0x1000, %rax           # imm = 0x1000
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
je 0x1c89b
movq -0x48(%rbp), %rax
movzbl (%rax), %edi
callq 0x1cc40
cmpl $0x0, %eax
jne 0x1c89b
cmpq $0x1, -0x28(%rbp)
jb 0x1c89b
movq -0x28(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jb 0x1c89b
movq -0x60(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq -0x28(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x28(%rbp)
cmpq $0x2, -0x28(%rbp)
jb 0x1c996
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x30(%rbp)
movq -0x50(%rbp), %rax
movzbl 0x1(%rax), %edx
andl $0x80, %edx
xorl %eax, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
cltq
movq %rax, -0x38(%rbp)
cmpq $0x7e, -0x30(%rbp)
jae 0x1c902
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0x1c902
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
jmp 0x1c994
cmpq $0x7e, -0x30(%rbp)
jne 0x1c93c
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jb 0x1c93c
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movzwl 0x2(%rax), %edi
callq 0x34e0
movzwl %ax, %eax
movq %rax, -0x18(%rbp)
jmp 0x1c992
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0xa, %rcx
cmpq %rcx, %rax
jb 0x1c990
movq -0x38(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x40(%rbp)
movq -0x50(%rbp), %rax
movl 0x2(%rax), %edi
callq 0x3630
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rax
movl 0x6(%rax), %edi
callq 0x3630
movl %eax, %ecx
movq -0x90(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x1c992
jmp 0x1c994
jmp 0x1c996
movq -0x40(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x20(%rbp)
movb %al, -0x91(%rbp)
jbe 0x1c9c2
movq -0x20(%rbp), %rax
cmpq -0x28(%rbp), %rax
setbe %al
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x40(%rbp), %rax
jb 0x1c9e4
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1ca04
movl $0x0, -0x64(%rbp)
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x800, %rcx            # imm = 0x800
movq %rcx, 0xc8(%rax)
cmpl $0x0, -0x64(%rbp)
je 0x1cbb1
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rax
movb (%rax), %al
movb %al, -0x70(%rbp)
cmpq $0x0, -0x38(%rbp)
jbe 0x1ca86
movq $0x0, -0x10(%rbp)
movq -0x10(%rbp), %rax
cmpq -0x18(%rbp), %rax
jae 0x1ca84
movq -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
xorl %ecx, %ecx
subq -0x38(%rbp), %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
andq $0x3, %rcx
movzbl (%rax,%rcx), %esi
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
addq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
jmp 0x1ca3a
jmp 0x1ca86
cmpl $0x0, -0x68(%rbp)
je 0x1cb58
movzbl -0x70(%rbp), %edi
callq 0x1cc40
cmpl $0x0, %eax
je 0x1cac1
movq -0x48(%rbp), %rax
movzbl (%rax), %ecx
andl $-0x10, %ecx
movb %cl, (%rax)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rax
subq %rax, %rdx
callq 0x3540
movq -0x78(%rbp), %rcx
movq -0x60(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x80(%rbp), %rdx
movq -0x50(%rbp), %rax
subq %rax, %rdx
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
subq %rdx, %rcx
movq %rcx, 0x48(%rax)
movzbl -0x70(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1cb56
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1cc80
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movq -0x60(%rbp), %rax
movq (%rax), %rsi
addq $0x1, %rsi
callq 0x89f0
movq -0x60(%rbp), %rax
movq $0x0, (%rax)
jmp 0x1cb81
movq -0x8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x1cc80
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movq -0x20(%rbp), %rsi
callq 0x89f0
movq -0x60(%rbp), %rax
movq $0x0, (%rax)
cmpl $0x0, -0x68(%rbp)
jne 0x1cbaf
movq -0x50(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0x1cbaf
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rcx
orq $0x400, %rcx            # imm = 0x400
movq %rcx, 0xc8(%rax)
jmp 0x1cbb1
movl -0x64(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)