// File: /workspace/github_c_makefile_star2_O3/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 0x68640
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, %r15
movq %rdx, %r13
movq %rdi, %r14
cmpq $0x1057, %rcx           # imm = 0x1057
movl %esi, -0x2c(%rbp)
jb 0x686c7
leaq 0xa(%r15), %rbx
movq %rbx, %rdi
callq 0xca00
testq %rax, %rax
je 0x68777
movq %rax, %r12
movl -0x2c(%rbp), %eax
andb $0xf, %al
orb $-0x80, %al
movb %al, (%r12)
cmpq $0xffff, %r15           # imm = 0xFFFF
jbe 0x68703
movq %r15, %rax
shrq $0x20, %rax
bswapl %eax
movl %r15d, %ecx
bswapl %ecx
movb $0x7f, 0x1(%r12)
movl %eax, 0x2(%r12)
movl %ecx, 0x6(%r12)
movq %r12, %rdi
addq $0xa, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xc0e0
jmp 0x6872d
movl %esi, %eax
andb $0xf, %al
orb $-0x80, %al
movb %al, -0x1090(%rbp)
cmpq $0x7d, %r15
ja 0x686fc
leaq -0x108e(%rbp), %rdi
movb %r15b, -0x1(%rdi)
movq %r13, %rsi
movq %r15, %rdx
callq 0xc0e0
addq $0x2, %r15
leaq -0x1090(%rbp), %r12
jmp 0x6872a
leaq -0x1090(%rbp), %r12
movb $0x7e, 0x1(%r12)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, 0x2(%r12)
leaq 0x4(%r12), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0xc0e0
addq $0x4, %r15
movq %r15, %rbx
movq -0x8(%r14), %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x6661a
movq -0x8(%r14), %rbx
leaq -0x1090(%rbp), %rax
cmpq %rax, %r12
je 0x68754
movq %r12, %rdi
callq 0xc250
cmpl $0x8, -0x2c(%rbp)
jne 0x68761
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
xorl %eax, %eax
jmp 0x68765