// File: /workspace/github_cpp_makefile_star2_O2/king1600[P]cordpp/src/ws.cc
// Function: cordpp::WebsockClient::send(char const*, unsigned long, cordpp::WebsockOp)
void WebsockClient::send(const char *data, const size_t len, WebsockOp op) {
  // check if allowed to send data
  if (state != WebsockState::open || !connection.is_connected())
    return;
  
  // calculate payload size padding
  uint8_t padding;
  if (len < 0x7e)
    padding = 0;
  else if (len < 0x10000)
    padding = 2;
  else
    padding = 8;

  // create output buffer and incrementer
  uint64_t i, offset = 0;
  uint8_t output[6 + len + padding];

  // add the first byte header
  output[offset++] = 0x80 | static_cast<uint8_t>(op);

  // add the payload length
  if (padding == 0) {
    output[offset++] = 0x80 | len;
  } else if (padding == 2) {
    output[offset++] = 0x80 | 0x7e;
    output[offset++] = (len >> 8) & 0xff;
    output[offset++] = len & 0xff;
  } else if (padding == 8) {
    output[offset++] = 0x80 | 0x7f;
    output[offset++] = (len >> 56) & 0xff;
    output[offset++] = (len >> 48) & 0xff;
    output[offset++] = (len >> 40) & 0xff;
    output[offset++] = (len >> 32) & 0xff;
    output[offset++] = (len >> 24) & 0xff;
    output[offset++] = (len >> 16) & 0xff;
    output[offset++] = (len >> 8) & 0xff;
    output[offset++] = len & 0xff;
  }

  // add the mask and get its offset
  uint8_t mask_offset = offset;
  output[offset++] = rand_byte();
  output[offset++] = rand_byte();
  output[offset++] = rand_byte();
  output[offset++] = rand_byte();

  // add payload data and mask it
  for (i = 0; i < len; i++)
    output[offset++] = static_cast<uint8_t>(data[i])
      ^ output[(i % 4) + mask_offset];

  // write data to connection
  connection.write(reinterpret_cast<const char*>(output), sizeof(output));
}

// Assembly:
cmpl $0x0, 0x398(%rdi)
je 0x2fb9e
retq
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %r12d
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
addq $0x40, %r14
movq %r14, %rdi
callq 0x20eda
testb %al, %al
je 0x2fcfc
movq %rsp, -0x40(%rbp)
cmpq $0x10000, %rbx          # imm = 0x10000
pushq $0x8
popq %rcx
pushq $0x2
popq %rdi
cmovbq %rdi, %rcx
xorl %eax, %eax
cmpq $0x7e, %rbx
cmovaeq %rcx, %rax
leaq (%rbx,%rax), %rcx
addq $0x15, %rcx
andq $-0x10, %rcx
movq %rsp, %rdx
movq %rdx, %rsi
subq %rcx, %rsi
negq %rcx
movq %rsi, %rsp
orb $-0x80, %r12b
movb %r12b, (%rdx,%rcx)
cmpq $0x7d, %rbx
jbe 0x2fc67
cmpq $0xffff, %rbx           # imm = 0xFFFF
jbe 0x2fc74
movb $-0x1, 0x1(%rsi)
movq %rbx, %rcx
shrq $0x38, %rcx
movb %cl, 0x2(%rsi)
movq %rbx, %rcx
shrq $0x30, %rcx
movb %cl, 0x3(%rsi)
movq %rbx, %rcx
shrq $0x28, %rcx
movb %cl, 0x4(%rsi)
movq %rbx, %rcx
shrq $0x20, %rcx
movb %cl, 0x5(%rsi)
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x6(%rsi)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x7(%rsi)
movb %bh, 0x8(%rsi)
pushq $0xa
popq %r12
pushq $0x9
jmp 0x2fc81
movl %ebx, %edx
orb $-0x80, %dl
pushq $0x1
popq %rcx
movq %rdi, %r12
jmp 0x2fc84
movb $-0x2, 0x1(%rsi)
movb %bh, 0x2(%rsi)
pushq $0x4
popq %r12
pushq $0x3
popq %rcx
movl %ebx, %edx
addq %rbx, %rax
addq $0x6, %rax
movq %rax, -0x30(%rbp)
movb %dl, (%rsi,%rcx)
movq %rsi, %r13
callq 0x2fed9
movq %r12, %rcx
addq %r13, %r12
movq %r13, -0x38(%rbp)
addq %rcx, %r13
addq $0x4, %r13
movb %al, -0x4(%r13)
callq 0x2fed9
movb %al, -0x3(%r13)
callq 0x2fed9
movb %al, -0x2(%r13)
callq 0x2fed9
movb %al, -0x1(%r13)
xorl %eax, %eax
cmpq %rax, %rbx
je 0x2fce8
movl %eax, %ecx
andl $0x3, %ecx
movb (%r12,%rcx), %cl
xorb (%r15,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
jmp 0x2fccc
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x20ede
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop