// File: /workspace/github_cpp_makefile_star2_O2/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
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
callq 0x95f0
movq 0x18(%r14), %rcx
movq 0x40(%rcx), %rsi
movq %rax, %rdi
callq 0x9a70
testl %eax, %eax
je 0xc639
cmpl $-0x1, 0x8(%r14)
je 0xc70c
movq 0x38(%r14), %r12
testq %r12, %r12
je 0xc64b
cmpl $0x0, 0xc(%r12)
je 0xc64b
movslq %ebx, %r13
testl %r13d, %r13d
pushq $-0x1
popq %rdi
cmovnsq %r13, %rdi
callq 0x9030
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x95a0
leaq 0x8(%rsp), %rsi
andl $0x0, (%rsi)
movl %r13d, 0x4(%rsi)
movq %rbp, 0x8(%rsi)
movq %r12, %rdi
callq 0x97c0
addl %r13d, 0x34(%r14)
jmp 0xc714
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x9820
jmp 0xc714
movslq %ebx, %r12
movl 0x8(%r14), %edi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x9370
movq %rax, %r13
cmpl $-0x1, %r13d
jne 0xc672
callq 0x9060
cmpl $0x4, (%rax)
je 0xc64e
cmpl %ebx, %r13d
jge 0xc711
xorl %r12d, %r12d
testl %r13d, %r13d
cmovgl %r13d, %r12d
movl %ebx, %ebp
subl %r12d, %ebp
movslq %ebp, %r13
testl %r13d, %r13d
pushq $-0x1
popq %rdi
cmovnsq %r13, %rdi
callq 0x9030
addq %r12, %r15
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x95a0
movq 0x38(%r14), %r15
testq %r15, %r15
jne 0xc6d3
pushq $0x18
popq %rdi
callq 0x96c0
movq %rax, %r15
pushq $0x2
popq %rsi
movq %rax, %rdi
callq 0x96a0
movq %r15, 0x38(%r14)
leaq 0x8(%rsp), %rsi
andl $0x0, (%rsi)
movl %ebp, 0x4(%rsi)
movq %r12, 0x8(%rsi)
movq %r15, %rdi
callq 0x97c0
addl %ebp, 0x34(%r14)
cmpb $0x0, 0x30(%r14)
jne 0xc714
movq 0x18(%r14), %rdi
movl 0x8(%r14), %esi
pushq $0x4
popq %rdx
callq 0x9300
movb $0x1, 0x30(%r14)
jmp 0xc714
pushq $-0x1
popq %rbx
jmp 0xc714
movl %r13d, %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r15, %rdi
callq 0x9690
movq %rbx, %rdi
callq 0x9aa0