// File: /workspace/github_cpp_makefile_star2_O1/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 0x6410
movq 0x18(%r14), %rcx
movq 0x40(%rcx), %rsi
movq %rax, %rdi
callq 0x6720
testl %eax, %eax
je 0x964c
cmpl $-0x1, 0x8(%r14)
je 0x976f
movq 0x38(%r14), %r12
testq %r12, %r12
je 0x965e
cmpl $0x0, 0xc(%r12)
je 0x965e
movslq %ebx, %rbp
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %rbp, %rdi
callq 0x6030
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x63e0
movl 0xc(%r12), %eax
cmpl (%r12), %eax
jne 0x9613
movq %r12, %rdi
callq 0x6230
movq 0x10(%r12), %rcx
movslq 0x8(%r12), %rdx
leal 0x1(%rdx), %eax
shlq $0x4, %rdx
movq %r13, 0x8(%rcx,%rdx)
movl %ebx, 0x4(%rcx,%rdx)
movl $0x0, (%rcx,%rdx)
cltd
idivl (%r12)
movl %edx, 0x8(%r12)
incl 0xc(%r12)
addl %ebx, 0x34(%r14)
jmp 0x9779
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x6570
jmp 0x9779
movslq %ebx, %r12
movl 0x8(%r14), %edi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x6280
movq %rax, %r13
cmpl $-0x1, %r13d
jne 0x9685
callq 0x6040
cmpl $0x4, (%rax)
je 0x9661
cmpl %ebx, %r13d
jge 0x9776
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 0x63e0
cmpq $0x0, 0x38(%r14)
jne 0x970b
movl $0x18, %edi
callq 0x64a0
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)
movq 0x38(%r14), %r15
movl 0xc(%r15), %eax
cmpl (%r15), %eax
jne 0x9720
movq %r15, %rdi
callq 0x6230
movq 0x10(%r15), %rcx
movslq 0x8(%r15), %rdx
leal 0x1(%rdx), %eax
shlq $0x4, %rdx
movq %r12, 0x8(%rcx,%rdx)
movl %ebp, 0x4(%rcx,%rdx)
movl $0x0, (%rcx,%rdx)
cltd
idivl (%r15)
movl %edx, 0x8(%r15)
incl 0xc(%r15)
addl %ebp, 0x34(%r14)
cmpb $0x0, 0x30(%r14)
jne 0x9779
movq 0x18(%r14), %rdi
movl 0x8(%r14), %esi
movl $0x4, %edx
callq 0x6210
movb $0x1, 0x30(%r14)
jmp 0x9779
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x9779
movl %r13d, %ebx
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 0x6480
movq %rbx, %rdi
callq 0x6750
nop