// File: /workspace/github_cpp_makefile_star2_O1/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 0x3a254
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 0x26278
testb %al, %al
je 0x3a40b
movq %rsp, %rsi
cmpq $0x10000, %rbx          # imm = 0x10000
movl $0x8, %ecx
movl $0x2, %eax
cmovbq %rax, %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, %rdi
subq %rcx, %rdi
negq %rcx
movq %rdi, %rsp
orb $-0x80, %r12b
movb %r12b, (%rdx,%rcx)
cmpq $0x7d, %rbx
movq %rsi, -0x38(%rbp)
jbe 0x3a329
cmpq $0xffff, %rbx           # imm = 0xFFFF
jbe 0x3a33b
movb $-0x1, 0x1(%rdi)
movq %rbx, %rcx
shrq $0x38, %rcx
movb %cl, 0x2(%rdi)
movq %rbx, %rcx
shrq $0x30, %rcx
movb %cl, 0x3(%rdi)
movq %rbx, %rcx
shrq $0x28, %rcx
movb %cl, 0x4(%rdi)
movq %rbx, %rcx
shrq $0x20, %rcx
movb %cl, 0x5(%rdi)
movl %ebx, %ecx
shrl $0x18, %ecx
movb %cl, 0x6(%rdi)
movl %ebx, %ecx
shrl $0x10, %ecx
movb %cl, 0x7(%rdi)
movb %bh, 0x8(%rdi)
movl $0xa, %r12d
movl $0x9, %ecx
jmp 0x3a34d
movl %ebx, %edx
orb $-0x80, %dl
movl $0x1, %ecx
movl $0x2, %r12d
jmp 0x3a34f
movb $-0x2, 0x1(%rdi)
movb %bh, 0x2(%rdi)
movl $0x4, %r12d
movl $0x3, %ecx
movl %ebx, %edx
addq %rbx, %rax
addq $0x6, %rax
movq %rax, -0x30(%rbp)
movb %dl, (%rdi,%rcx)
leaq 0x1f00c(%rip), %rsi     # 0x59370
leaq 0x2038d(%rip), %rdx     # 0x5a6f8
movq %rdi, %r13
movq %rdx, %rdi
callq 0x3b0bc
movb %al, (%r13,%r12)
leaq 0x20376(%rip), %rdi     # 0x5a6f8
leaq 0x1efe7(%rip), %rsi     # 0x59370
movq %rdi, %rdx
callq 0x3b0bc
movb %al, 0x1(%r13,%r12)
leaq 0x2035b(%rip), %rdi     # 0x5a6f8
leaq 0x1efcc(%rip), %rsi     # 0x59370
movq %rdi, %rdx
callq 0x3b0bc
movb %al, 0x2(%r13,%r12)
leaq 0x20340(%rip), %rdi     # 0x5a6f8
leaq 0x1efb1(%rip), %rsi     # 0x59370
movq %rdi, %rdx
callq 0x3b0bc
movb %al, 0x3(%r13,%r12)
testq %rbx, %rbx
je 0x3a3f8
leaq (%r12,%r13), %rax
leaq (%r12,%r13), %rcx
addq $0x4, %rcx
xorl %edx, %edx
movl %edx, %esi
andl $0x3, %esi
movb (%rax,%rsi), %sil
xorb (%r15,%rdx), %sil
movb %sil, (%rcx,%rdx)
incq %rdx
cmpq %rdx, %rbx
jne 0x3a3df
movq %r14, %rdi
movq %r13, %rsi
movq -0x30(%rbp), %rdx
callq 0x2627c
movq -0x38(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq