// File: /workspace/github_c_makefile_star2_O0/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: deliver_websocket_frame
static size_t deliver_websocket_frame(struct connection *conn) {
  // Having buf unsigned char * is important, as it is used below in arithmetic
  unsigned char *buf = (unsigned char *) conn->ns_conn->recv_iobuf.buf;
  size_t i, len, buf_len = conn->ns_conn->recv_iobuf.len, frame_len = 0,
      mask_len = 0, header_len = 0, data_len = 0, buffered = 0;

  if (buf_len >= 2) {
    len = buf[1] & 127;
    mask_len = buf[1] & 128 ? 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 = ((((size_t) buf[2]) << 8) + buf[3]);
    } else if (buf_len >= 10 + mask_len) {
      header_len = 10 + mask_len;
      data_len = (size_t) (((uint64_t) htonl(* (uint32_t *) &buf[2])) << 32) +
        htonl(* (uint32_t *) &buf[6]);
    }
  }

  frame_len = header_len + data_len;
  buffered = frame_len > 0 && frame_len <= buf_len;

  if (buffered) {
    conn->mg_conn.content_len = data_len;
    conn->mg_conn.content = (char *) buf + header_len;
    conn->mg_conn.wsbits = 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];
      }
    }

    // Call the handler and remove frame from the iobuf
    if (call_user(conn, MG_REQUEST) == MG_FALSE ||
        (buf[0] & 0x0f) == WEBSOCKET_OPCODE_CONNECTION_CLOSE) {
      conn->ns_conn->flags |= NSF_FINISHED_SENDING_DATA;
    }
    iobuf_remove(&conn->ns_conn->recv_iobuf, frame_len);
  }

  return buffered;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x40(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x48(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
cmpq $0x2, -0x28(%rbp)
jb 0xbdb1e
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x20(%rbp)
movq -0x10(%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, -0x20(%rbp)
jae 0xbda89
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jb 0xbda89
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
jmp 0xbdb1c
cmpq $0x7e, -0x20(%rbp)
jne 0xbdaca
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jb 0xbdaca
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
shlq $0x8, %rax
movq -0x10(%rbp), %rcx
movzbl 0x3(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0xbdb1a
movq -0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
addq $0xa, %rcx
cmpq %rcx, %rax
jb 0xbdb18
movq -0x38(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl 0x2(%rax), %edi
callq 0xd930
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0x6(%rax), %edi
callq 0xd930
movl %eax, %ecx
movq -0x58(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
jmp 0xbdb1a
jmp 0xbdb1c
jmp 0xbdb1e
movq -0x40(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x30(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x30(%rbp)
movb %al, -0x59(%rbp)
jbe 0xbdb44
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
setbe %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cltq
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0xbdc37
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x278(%rax)
movq -0x10(%rbp), %rcx
addq -0x40(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x270(%rax)
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x288(%rax)
cmpq $0x0, -0x38(%rbp)
jbe 0xbdbeb
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xbdbe9
movq -0x10(%rbp), %rax
addq -0x40(%rbp), %rax
xorl %ecx, %ecx
subq -0x38(%rbp), %rcx
addq %rcx, %rax
movq -0x18(%rbp), %rcx
andq $0x3, %rcx
movzbl (%rax,%rcx), %esi
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
addq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %edx
xorl %esi, %edx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0xbdb9f
jmp 0xbdbeb
movq -0x8(%rbp), %rdi
movl $0x67, %esi
callq 0xb7300
cmpl $0x0, %eax
je 0xbdc0d
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
cmpl $0x8, %eax
jne 0xbdc23
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0xa0(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0xa0(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
addq $0x40, %rdi
movq -0x30(%rbp), %rsi
callq 0xaedd0
movq -0x50(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)