// File: /workspace/github_cpp_makefile_star2_O3/king1600[P]cordpp/src/ws.cc
// Function: cordpp::WebsockClient::handle_frame()
void WebsockClient::handle_frame() {
  // prepend fragmented data if any
  if (frame.opcode != WebsockOp::cont && builder.size() > 0) {
    char *data = new char[builder.size() + frame.payload_len];
    std::memcpy(data, &builder[0], builder.size());
    std::memcpy(data + builder.size(), frame.payload, frame.payload_len);
    builder.clear();
    delete frame.payload;
    frame.payload = data;
    frame.payload_len = sizeof(frame.payload);
  }

  // handle what actiont to perform using the frame
  switch (frame.opcode) {

    // append to builder if payload is fragmented
    case WebsockOp::cont:
      builder.insert(builder.end(), frame.payload,
        frame.payload + frame.payload_len);
      break;

    // echo back ping from server if it sends
    case WebsockOp::ping:
      send(frame.payload, frame.payload_len, WebsockOp::pong);
      break;
    
    // handle ping response from server
    case WebsockOp::pong:
      if (!pings.empty()) {
        const WebsockPing task = std::move(pings.front());
        pings.pop_front();
        task.action(std::chrono::duration_cast<std::chrono::milliseconds>(
          websock_clock::now() - task.created).count());
      }
      break;

    // handle closing opcode from server
    case WebsockOp::close:
      if (state != WebsockState::closed) {
        send(frame.payload, frame.payload_len, WebsockOp::close);
        state = WebsockState::closed;
      }
      perform_close();
      break;

    // handle opcodes for binary and text
    default:
      message_cb(frame.payload, frame.payload_len);
      break;
  }

  // read the next frame
  parse_headers();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl 0x30(%rdi), %eax
testl %eax, %eax
je 0x38ff1
movq (%rbx), %r14
movq 0x8(%rbx), %r15
subq %r14, %r15
je 0x38fad
movq 0x38(%rbx), %r12
leaq (%r12,%r15), %rdi
callq 0xa040
movq %rax, %r13
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xa4d0
addq %r13, %r15
movq 0x20(%rbx), %rbp
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0xa4d0
movq %r14, 0x8(%rbx)
movl $0x1, %esi
movq %rbp, %rdi
callq 0xa5d0
movq %r13, 0x20(%rbx)
movq $0x8, 0x38(%rbx)
movl 0x30(%rbx), %eax
cmpl $0x8, %eax
jg 0x3901f
testl %eax, %eax
je 0x38ff1
cmpl $0x8, %eax
jne 0x390ed
cmpl $0x1, 0x398(%rbx)
je 0x38fe7
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl $0x8, %ecx
callq 0x39178
movl $0x1, 0x398(%rbx)
movq %rbx, %rdi
callq 0x39348
jmp 0x39008
movq 0x8(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x38(%rbx), %rcx
addq %rdx, %rcx
movq %rbx, %rdi
callq 0x3a236
movq %rbx, %rdi
callq 0x38d44
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x9, %eax
je 0x39125
cmpl $0xa, %eax
jne 0x390ed
movq 0x358(%rbx), %rax
cmpq %rax, 0x378(%rbx)
je 0x39008
leaq 0x348(%rbx), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x18(%rax), %r14
movq %r14, 0x28(%rsp)
movq 0x10(%rax), %r15
testq %r15, %r15
je 0x3907f
leaq 0x10(%rax), %rcx
movups (%rax), %xmm1
movaps %xmm1, 0x10(%rsp)
movq %r15, 0x20(%rsp)
movups %xmm0, (%rcx)
movq 0x20(%rax), %r12
movq %r12, 0x30(%rsp)
callq 0x39d96
callq 0xa7b0
subq %r12, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
movq %rdx, 0x8(%rsp)
testq %r15, %r15
je 0x39144
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq *%r14
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x39008
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x39008
movq 0x20(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rax, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
cmpq $0x0, 0x2f8(%rbx)
je 0x3913f
leaq 0x2e8(%rbx), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq *0x300(%rbx)
jmp 0x39008
movq 0x20(%rbx), %rsi
movq 0x38(%rbx), %rdx
movq %rbx, %rdi
movl $0xa, %ecx
callq 0x39178
jmp 0x39008
callq 0xa240
callq 0xa240
jmp 0x3916f
movq %rax, %rbx
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x39167
leaq 0x10(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %rbx, %rdi
callq 0xaa30
movq %rax, %rdi
callq 0xd3a3
nop