// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xa8(%rdi), %rsi
leaq 0x48(%rsp), %rdi
callq 0x159c8
leaq 0x88(%rbx), %r14
movq 0x88(%rbx), %rsi
movq 0x90(%rbx), %rdx
cmpq %rdx, %rsi
je 0x1c7fa
subq %rsi, %rdx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x7260
cmpq $-0x1, %rax
je 0x1c765
movq (%r14), %rsi
addq %rsi, %rax
movq %r14, %rdi
movq %rax, %rdx
callq 0x1d0f4
jmp 0x1c729
callq 0x7030
movl (%rax), %r15d
cmpl $0x4, %r15d
je 0x1c729
cmpl $0xb, %r15d
je 0x1c971
callq 0x72f0
movq (%rax), %rcx
movq %rax, %rdi
movl %r15d, %esi
callq *0x28(%rcx)
cmpl $0x20, %eax
je 0x1c931
movl %eax, %ebx
movq %rdx, %r14
leaq 0x8(%rsp), %rdi
callq 0x865e
leaq 0xbfc9(%rip), %rsi      # 0x28776
leaq 0x8(%rsp), %rdi
callq 0x1d0dc
leaq 0x20(%rsp), %rsi
movl %ebx, (%rsi)
movq %rax, %rdi
callq 0x93b8
leaq 0x9905(%rip), %rsi      # 0x260d2
movq %rax, %rdi
callq 0x86b2
movq %rax, %r15
movq (%r14), %rax
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x869a
jmp 0x1c91d
movq %r14, %rdi
callq 0x1d326
leaq 0x70(%rbx), %r14
leaq 0x58(%rbx), %rax
movq %rax, 0x58(%rsp)
leaq 0x20(%rsp), %r12
movq 0x70(%rbx), %r13
cmpq 0x78(%rbx), %r13
je 0x1c935
movq %rsp, %rax
movq %rax, 0x28(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r12, 0x38(%rsp)
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x1d0fa
movq 0x18(%rsp), %rdx
movq (%rsp), %rsi
subq %rsi, %rdx
je 0x1c891
addq 0x20(%rsp), %rsi
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x7260
cmpq $-0x1, %rax
je 0x1c86e
addq %rax, (%rsp)
jmp 0x1c846
callq 0x7030
movl (%rax), %r15d
cmpl $0x4, %r15d
je 0x1c846
cmpl $0xb, %r15d
jne 0x1c8ae
movq (%rsp), %rax
cmpq %rax, 0x18(%rsp)
jne 0x1c954
movq (%r14), %rsi
movq %r14, %rdi
callq 0x1d110
movq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x1cf8c
jmp 0x1c814
callq 0x72f0
movq (%rax), %rcx
movq %rax, %rdi
movl %r15d, %esi
callq *0x28(%rcx)
movl %eax, %ebx
movq %rdx, %r14
leaq 0x8(%rsp), %rdi
callq 0x865e
leaq 0xbea1(%rip), %rsi      # 0x28776
leaq 0x8(%rsp), %rdi
callq 0x1d0dc
leaq 0x64(%rsp), %rsi
movl %ebx, (%rsi)
movq %rax, %rdi
callq 0x93b8
leaq 0x97dd(%rip), %rsi      # 0x260d2
movq %rax, %rdi
callq 0x86b2
movq %rax, %r15
movq (%r14), %rax
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x869a
leaq 0x28(%rsp), %rdi
callq 0x71d0
leaq 0x8(%rsp), %rdi
callq 0x86e4
xorl %ebx, %ebx
jmp 0x1c973
movq %rbx, %rdi
movl %ebp, %esi
callq 0x159de
cmpb $0x0, 0xa0(%rbx)
jne 0x1c931
leaq 0x48(%rsp), %rdi
callq 0x15a56
jmp 0x1c962
movq %rax, (%r13)
leaq 0x48(%rsp), %rdi
callq 0x15a56
addq $0xd0, %rbx
movq %rbx, %rdi
callq 0x7930
movb $0x1, %bl
leaq 0x48(%rsp), %rdi
callq 0x15a16
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1c9c8
jmp 0x1c99c
jmp 0x1c9b1
jmp 0x1c9b1
jmp 0x1c9c8
jmp 0x1c9c8
jmp 0x1c9b1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x71d0
jmp 0x1c9b4
jmp 0x1c9b1
jmp 0x1c9b1
jmp 0x1c9c8
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x86e4
jmp 0x1c9cb
jmp 0x1c9c8
jmp 0x1c9c8
jmp 0x1c9c8
jmp 0x1c9c8
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x15a16
movq %rbx, %rdi
callq 0x7990