// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x10b0, %rsp           # imm = 0x10B0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
leaq -0x1090(%rbp), %rax
movq %rax, -0x1098(%rbp)
movq $0x0, -0x10a0(%rbp)
cmpq $-0xb, -0x28(%rbp)
jbe 0xb1d97
movq $0x0, -0x8(%rbp)
jmp 0xb1f73
movq -0x28(%rbp), %rax
addq $0xa, %rax
cmpq $0x1060, %rax           # imm = 0x1060
jbe 0xb1dce
movq -0x28(%rbp), %rdi
addq $0xa, %rdi
callq 0xdb70
movq %rax, -0x1098(%rbp)
cmpq $0x0, %rax
jne 0xb1dce
movq $0x0, -0x8(%rbp)
jmp 0xb1f73
movl -0x14(%rbp), %eax
andl $0xf, %eax
addl $0x80, %eax
movb %al, %cl
movq -0x1098(%rbp), %rax
movb %cl, (%rax)
cmpq $0x7e, -0x28(%rbp)
jae 0xb1e27
movq -0x28(%rbp), %rax
movb %al, %cl
movq -0x1098(%rbp), %rax
movb %cl, 0x1(%rax)
movq -0x1098(%rbp), %rdi
addq $0x2, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xd100
movq -0x28(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x10a0(%rbp)
jmp 0xb1f06
cmpq $0xffff, -0x28(%rbp)    # imm = 0xFFFF
ja 0xb1e82
movq -0x1098(%rbp), %rax
movb $0x7e, 0x1(%rax)
movq -0x28(%rbp), %rax
movzwl %ax, %edi
callq 0xd920
movw %ax, %cx
movq -0x1098(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x1098(%rbp), %rdi
addq $0x4, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xd100
movq -0x28(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10a0(%rbp)
jmp 0xb1f04
movq -0x28(%rbp), %rax
shrq $0x20, %rax
movl %eax, %edi
callq 0xd930
movl %eax, -0x10a4(%rbp)
movabsq $0xffffffff, %rax       # imm = 0xFFFFFFFF
andq -0x28(%rbp), %rax
movl %eax, %edi
callq 0xd930
movl %eax, -0x10a8(%rbp)
movq -0x1098(%rbp), %rax
movb $0x7f, 0x1(%rax)
movq -0x1098(%rbp), %rax
movl -0x10a4(%rbp), %ecx
movl %ecx, 0x2(%rax)
movq -0x1098(%rbp), %rax
movl -0x10a8(%rbp), %ecx
movl %ecx, 0x6(%rax)
movq -0x1098(%rbp), %rdi
addq $0xa, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0xd100
movq -0x28(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x10a0(%rbp)
jmp 0xb1f06
cmpq $0x0, -0x10a0(%rbp)
jbe 0xb1f27
movq -0x10(%rbp), %rdi
movq -0x1098(%rbp), %rsi
movq -0x10a0(%rbp), %rdx
callq 0xb1580
movq -0x1098(%rbp), %rax
leaq -0x1090(%rbp), %rcx
cmpq %rcx, %rax
je 0xb1f46
movq -0x1098(%rbp), %rdi
callq 0xd2c0
cmpl $0x8, -0x14(%rbp)
jne 0xb1f63
movq -0x10(%rbp), %rax
movq -0x8(%rax), %rax
movl 0xa0(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0xa0(%rax)
movq -0x10(%rbp), %rax
movq -0x8(%rax), %rax
movq 0x60(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x10b0, %rsp           # imm = 0x10B0
popq %rbp
retq