// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/mongoose/mongoose.c
// Function: mg_websocket_write
size_t mg_websocket_write(struct mg_connection *conn, int opcode,
                          const char *data, size_t data_len) {
    unsigned char mem[4192], *copy = mem;
    size_t copy_len = 0;

    /* Check overflow */
    if (data_len > ~(size_t)0 - (size_t)10) {
      return 0;
    }

    if (data_len + 10 > sizeof(mem) &&
        (copy = (unsigned char *) NS_MALLOC(data_len + 10)) == NULL) {
      return 0;
    }

    copy[0] = 0x80 + (opcode & 0x0f);

    // Frame format: http://tools.ietf.org/html/rfc6455#section-5.2
    if (data_len < 126) {
      // Inline 7-bit length field
      copy[1] = data_len;
      memcpy(copy + 2, data, data_len);
      copy_len = 2 + data_len;
    } else if (data_len <= 0xFFFF) {
      // 16-bit length field
      copy[1] = 126;
      * (uint16_t *) (copy + 2) = (uint16_t) htons((uint16_t) data_len);
      memcpy(copy + 4, data, data_len);
      copy_len = 4 + data_len;
    } else {
      // 64-bit length field
      const uint32_t hi = htonl((uint32_t) ((uint64_t) data_len >> 32));
      const uint32_t lo = htonl(data_len & 0xffffffff);
      copy[1] = 127;
      memcpy(copy+2,&hi,sizeof(hi));
      memcpy(copy+6,&lo,sizeof(lo));
      memcpy(copy + 10, data, data_len);
      copy_len = 10 + data_len;
    }

    if (copy_len > 0) {
      mg_write(conn, copy, copy_len);
    }
    if (copy != mem) {
      NS_FREE(copy);
    }

    // If we send closing frame, schedule a connection to be closed after
    // data is drained to the client.
    if (opcode == WEBSOCKET_OPCODE_CONNECTION_CLOSE) {
      MG_CONN_2_CONN(conn)->ns_conn->flags |= NSF_FINISHED_SENDING_DATA;
    }

    return MG_CONN_2_CONN(conn)->ns_conn->send_iobuf.len;
}

// Assembly:
cmpq $-0xb, %rcx
jbe 0x6a202
xorl %eax, %eax
retq
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1068, %rsp           # imm = 0x1068
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, %r14
leaq 0xa(%rcx), %rbx
cmpq $0x1057, %rcx           # imm = 0x1057
jb 0x6a249
movq %rbx, %rdi
movl %esi, %r15d
callq 0xca00
movl %r15d, %esi
movq %rax, %r15
testq %rax, %rax
jne 0x6a250
xorl %eax, %eax
jmp 0x6a30f
leaq -0x1090(%rbp), %r15
movl %esi, %eax
andb $0xf, %al
orb $-0x80, %al
movb %al, (%r15)
cmpq $0x7d, %r12
movl %esi, -0x2c(%rbp)
ja 0x6a27b
movb %r12b, 0x1(%r15)
leaq 0x2(%r15), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xc0e0
addq $0x2, %r12
jmp 0x6a2a8
cmpq $0xffff, %r12           # imm = 0xFFFF
ja 0x6a2ad
movb $0x7e, 0x1(%r15)
movl %r12d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
leaq 0x4(%r15), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xc0e0
addq $0x4, %r12
movq %r12, %rbx
jmp 0x6a2d7
movq %r12, %rax
shrq $0x20, %rax
bswapl %eax
movl %r12d, %ecx
bswapl %ecx
movb $0x7f, 0x1(%r15)
movl %eax, 0x2(%r15)
movl %ecx, 0x6(%r15)
leaq 0xa(%r15), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xc0e0
movq -0x8(%r14), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x681c0
movq -0x8(%r14), %rbx
leaq -0x1090(%rbp), %rax
cmpq %rax, %r15
je 0x6a2fe
movq %r15, %rdi
callq 0xc250
cmpl $0x8, -0x2c(%rbp)
jne 0x6a30b
orb $0x1, 0xa0(%rbx)
movq 0x60(%rbx), %rax
addq $0x1068, %rsp           # imm = 0x1068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq