// File: /workspace/github_cpp_makefile_star2_O3/cybozu[P]yrmcds/cybozu/tcp.cpp
// Function: cybozu::tcp_socket::write_pending_data(int)
bool tcp_socket::write_pending_data(int fd) {
    lock_guard g(m_lock);

    while( ! m_tmpbuf.empty() ) {
        ssize_t n = ::send(fd, m_tmpbuf.data(), m_tmpbuf.size(), 0);
        if( n == -1 ) {
            if( errno == EINTR ) continue;
            if( errno == EAGAIN || errno == EWOULDBLOCK ) return true;
            auto ecnd = std::system_category().default_error_condition(errno);
            if( ecnd.value() != EPIPE )
                logger::error() << "<tcp_socket::write_pending_data>: ("
                                << ecnd.value() << ") "
                                << ecnd.message();
            return false;
        }
        m_tmpbuf.erase(m_tmpbuf.begin(), m_tmpbuf.begin() + n);
    }
    m_tmpbuf.shrink_to_fit();

    while( ! m_pending.empty() ) {
        auto& t = m_pending.front();
        char* p;
        std::size_t len;
        std::size_t sent;
        std::tie(p, len, sent) = t;

        while( len != sent ) {
            ssize_t n = ::send(fd, p+sent, len-sent, 0);
            if( n == -1 ) {
                if( errno == EINTR ) continue;
                if( errno == EAGAIN || errno == EWOULDBLOCK ) break;
                auto ecnd = std::system_category().default_error_condition(errno);
                logger::error() << "<tcp_socket::write_pending_data>: ("
                                << ecnd.value() << ") "
                                << ecnd.message();
                return false;
            }
            sent += n;
        }
        if( len == sent ) {
            m_pending.erase(m_pending.begin());
            m_free_buffers.push_back(p);
        } else {
            std::get<2>(t) = sent;
            g.unlock();
            // notify other threads of the new free space
            m_cond_write.notify_all();
            return true;
        }
    }

    // all data have been sent.
    _flush(fd);

    if( ! m_shutdown ) {
        g.unlock();
        m_cond_write.notify_all();
        return true;
    }

    // invalidate and close this socket when m_shutdown==true.
    return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xa8(%rdi), %rax
leaq 0x38(%rsp), %r14
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0x17b8c
movb $0x1, 0x8(%r14)
leaq 0x88(%rbx), %r14
movq 0x88(%rbx), %rsi
movq 0x90(%rbx), %rdx
cmpq %rdx, %rsi
je 0x20991
subq %rsi, %rdx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x7240
movq %rax, %r15
cmpq $-0x1, %rax
je 0x2094d
movq 0x90(%rbx), %rdx
testq %r15, %r15
je 0x20983
movq 0x88(%rbx), %r12
addq %r12, %r15
subq %r15, %rdx
je 0x20968
movq %r12, %rdi
movq %r15, %rsi
callq 0x78d0
movq 0x90(%rbx), %rdx
movq %rdx, %rax
jmp 0x2096e
callq 0x7040
movl (%rax), %r15d
cmpl $0x4, %r15d
jne 0x20b69
movq 0x90(%rbx), %rdx
jmp 0x20983
movq %r15, %rdx
movq %r15, %rax
subq %r15, %rax
addq %rax, %r12
cmpq %r12, %rdx
je 0x20983
movq %r12, 0x90(%rbx)
movq %r12, %rdx
movq (%r14), %rsi
cmpq %rdx, %rsi
jne 0x20906
jmp 0x20994
movq %rsi, %rdx
cmpq %rdx, 0x98(%rbx)
je 0x209a5
movq %r14, %rdi
callq 0x21680
movq 0x70(%rbx), %r12
cmpq 0x78(%rbx), %r12
je 0x20a63
leaq 0x70(%rbx), %rax
movq %rax, 0x50(%rsp)
leaq 0x58(%rbx), %rax
movq %rax, 0x48(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movq (%r12), %r15
movq 0x8(%r12), %r14
cmpq %r15, %r14
je 0x20a20
movq 0x10(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdx
subq %r15, %rdx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x7240
cmpq $-0x1, %rax
je 0x209ff
addq %rax, %r15
jmp 0x209d8
callq 0x7040
movl (%rax), %r13d
cmpl $0x4, %r13d
je 0x209d8
cmpl $0xb, %r13d
jne 0x20a98
cmpq %r15, %r14
jne 0x20c44
movq 0x50(%rsp), %rdi
movq (%rdi), %rsi
callq 0x21a2e
movq 0x60(%rbx), %rsi
cmpq 0x68(%rbx), %rsi
je 0x20a46
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x60(%rbx)
jmp 0x20a55
movq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x21560
movq 0x70(%rbx), %r12
cmpq 0x78(%rbx), %r12
jne 0x209c5
movq %rbx, %rdi
movl %ebp, %esi
callq 0x17b38
cmpb $0x0, 0xa0(%rbx)
jne 0x20b65
leaq 0x38(%rsp), %rdi
callq 0x17bc8
addq $0xd0, %rbx
movq %rbx, %rdi
callq 0x7940
jmp 0x20b6f
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %r13d, %esi
callq *0x28(%rcx)
movl %eax, %ebx
movq %rdx, %r14
leaq 0x25fd3(%rip), %rax     # 0x46a88
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
leaq 0x13703(%rip), %rdx     # 0x341c6
movq %rsp, %rdi
callq 0x96e6
movb (%rsp), %al
cmpb $0x1, %al
jne 0x20aeb
movq 0x8(%rsp), %rdi
leaq 0x15d08(%rip), %rsi     # 0x367e6
movl $0x23, %edx
callq 0x7570
movb (%rsp), %al
testb $0x1, %al
je 0x20afe
movq 0x8(%rsp), %rdi
movl %ebx, %esi
callq 0x7980
movb (%rsp), %al
testb $0x1, %al
je 0x20b18
movq 0x8(%rsp), %rdi
leaq 0x135c4(%rip), %rsi     # 0x340d2
movl $0x2, %edx
callq 0x7570
movq (%r14), %rax
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
cmpb $0x1, (%rsp)
jne 0x20b42
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x7570
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20b5d
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7500
movq %rsp, %rdi
callq 0x8f4e
xorl %ebx, %ebx
jmp 0x20b71
cmpl $0xb, %r15d
jne 0x20b8c
movb $0x1, %bl
leaq 0x38(%rsp), %rdi
callq 0x17b76
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %r15d, %esi
callq *0x28(%rcx)
cmpl $0x20, %eax
je 0x20b65
movl %eax, %ebx
movq %rdx, %r14
leaq 0x25eda(%rip), %rax     # 0x46a88
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
leaq 0x1360a(%rip), %rdx     # 0x341c6
movq %rsp, %rdi
callq 0x96e6
movb (%rsp), %al
cmpb $0x1, %al
jne 0x20be4
movq 0x8(%rsp), %rdi
leaq 0x15c0f(%rip), %rsi     # 0x367e6
movl $0x23, %edx
callq 0x7570
movb (%rsp), %al
testb $0x1, %al
je 0x20bf7
movq 0x8(%rsp), %rdi
movl %ebx, %esi
callq 0x7980
movb (%rsp), %al
testb $0x1, %al
je 0x20c11
movq 0x8(%rsp), %rdi
leaq 0x134cb(%rip), %rsi     # 0x340d2
movl $0x2, %edx
callq 0x7570
movq (%r14), %rax
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
cmpb $0x1, (%rsp)
jne 0x20b42
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x7570
jmp 0x20b42
movq %r15, (%r12)
leaq 0x38(%rsp), %rdi
callq 0x17bc8
jmp 0x20a84
jmp 0x20c9e
jmp 0x20c63
jmp 0x20c87
jmp 0x20c87
jmp 0x20c87
jmp 0x20c9e
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20c8a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7500
jmp 0x20c8a
jmp 0x20c87
jmp 0x20c87
movq %rax, %rbx
movq %rsp, %rdi
callq 0x8f4e
jmp 0x20ca1
jmp 0x20c9e
jmp 0x20c9e
jmp 0x20c9e
jmp 0x20c9e
jmp 0x20c9e
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x17b76
movq %rbx, %rdi
callq 0x79a0