// File: /workspace/github_cpp_makefile_star2_O3/shaovie[P]reactor/src/io_handle.cpp
// Function: io_handle::send(char const*, int)
int io_handle::send(const char *buff, const int len) {
    if (!pthread_equal(pthread_self(), this->poll->thread_id))
        return this->async_send(buff, len);

    if (this->fd == -1)
        return -1;
    
    // sync send in poller thread
    if (this->async_send_buf_q != nullptr && !this->async_send_buf_q->empty()) {
        char *bf = new char[len];
        ::memcpy(bf, buff, len);
        this->async_send_buf_q->push_back(async_send_buf(bf, len));
        this->async_send_buf_size += len;
        return len;
    }

    int ret = 0;
    do {
        ret = ::send(this->fd, buff, len, 0);
    } while (ret == -1 && errno == EINTR);

    if (ret < len) { // error or partial
        if (ret < 0)
            ret = 0;
        auto left = len - ret;
        char *bf = new char[left];
        ::memcpy(bf, buff + ret, left);
        if (this->async_send_buf_q == nullptr)
            this->async_send_buf_q = new ringq<async_send_buf>(2);
        this->async_send_buf_q->push_back(async_send_buf(bf, left));
        this->async_send_buf_size += left;
        if (this->async_send_polling == false) {
            this->poll->append(this->fd, ev_handler::ev_write);
            this->async_send_polling = true;
        }
        ret = len;
    }
    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
callq 0x6400
movq 0x18(%r14), %rcx
movq 0x40(%rcx), %rsi
movq %rax, %rdi
callq 0x6710
testl %eax, %eax
je 0x957d
cmpl $-0x1, 0x8(%r14)
je 0x9602
movq 0x38(%r14), %r12
testq %r12, %r12
je 0x958f
movl 0xc(%r12), %r13d
testl %r13d, %r13d
je 0x958f
movslq %ebx, %rbp
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %rbp, %rdi
callq 0x6030
movq %rax, (%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x63d0
movl (%r12), %ecx
cmpl %ecx, %r13d
jne 0x953f
movq %r12, %rdi
callq 0x6210
movl (%r12), %ecx
movl 0xc(%r12), %r13d
movq 0x10(%r12), %rdx
movslq 0x8(%r12), %rsi
leal 0x1(%rsi), %eax
shlq $0x4, %rsi
movq (%rsp), %rdi
movq %rdi, 0x8(%rdx,%rsi)
movl %ebx, 0x4(%rdx,%rsi)
movl $0x0, (%rdx,%rsi)
cltd
idivl %ecx
movl %edx, 0x8(%r12)
incl %r13d
movl %r13d, 0xc(%r12)
addl %ebx, 0x34(%r14)
jmp 0x96bb
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x6560
jmp 0x96bb
movslq %ebx, %r12
movl 0x8(%r14), %edi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x6260
movq %rax, %r13
cmpl $-0x1, %r13d
jne 0x95b6
callq 0x6040
cmpl $0x4, (%rax)
je 0x9592
cmpl %ebx, %r13d
jge 0x960c
xorl %r12d, %r12d
testl %r13d, %r13d
cmovgl %r13d, %r12d
movl %ebx, %ebp
subl %r12d, %ebp
movslq %ebp, %r13
testl %r13d, %r13d
movq $-0x1, %rdi
cmovnsq %r13, %rdi
callq 0x6030
addq %r12, %r15
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x63d0
movq 0x38(%r14), %r15
testq %r15, %r15
je 0x9614
movl (%r15), %ecx
jmp 0x9656
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x96bb
movl %r13d, %ebx
jmp 0x96bb
movl $0x18, %edi
callq 0x6490
movq %rax, %r15
movl $0x2, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movl $0x0, 0x14(%rax)
movl $0x20, %edi
callq 0x6030
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, 0x10(%r15)
movq %r15, 0x38(%r14)
movl $0x2, %ecx
movl 0xc(%r15), %esi
cmpl %ecx, %esi
jne 0x966d
movq %r15, %rdi
callq 0x6210
movl (%r15), %ecx
movl 0xc(%r15), %esi
movq 0x10(%r15), %rdx
movslq 0x8(%r15), %rdi
leal 0x1(%rdi), %eax
shlq $0x4, %rdi
movq %r12, 0x8(%rdx,%rdi)
movl %ebp, 0x4(%rdx,%rdi)
movl $0x0, (%rdx,%rdi)
cltd
idivl %ecx
movl %edx, 0x8(%r15)
incl %esi
movl %esi, 0xc(%r15)
addl %ebp, 0x34(%r14)
cmpb $0x0, 0x30(%r14)
jne 0x96bb
movq 0x18(%r14), %rdi
movl 0x8(%r14), %esi
movl $0x4, %edx
callq 0x61f0
movb $0x1, 0x30(%r14)
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x6470
movq %rbx, %rdi
callq 0x6740
nop