// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movl %esi, %r13d
movq %rdi, %rbp
leaq 0xa8(%rdi), %rax
leaq 0x58(%rsp), %r14
movq %rax, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0x17c58
movb $0x1, 0x8(%r14)
leaq 0x88(%rbp), %r14
movq 0x88(%rbp), %rsi
movq 0x90(%rbp), %rdx
cmpq %rdx, %rsi
movl %r13d, 0xc(%rsp)
je 0x212f3
subq %rsi, %rdx
movl %r13d, %edi
xorl %ecx, %ecx
callq 0x7240
movq %rax, %r12
cmpq $-0x1, %rax
je 0x211c0
xorl %ebx, %ebx
testq %r12, %r12
je 0x212d3
movq (%r14), %r13
addq %r13, %r12
movq 0x90(%rbp), %rdx
subq %r12, %rdx
je 0x2119a
movq %r13, %rdi
movq %r12, %rsi
callq 0x78e0
movq 0x90(%rbp), %rax
movq %rax, %rcx
subq %r12, %rcx
addq %rcx, %r13
cmpq %r13, %rax
je 0x211b6
movq %r13, 0x90(%rbp)
movl 0xc(%rsp), %r13d
jmp 0x212d3
callq 0x7040
movl (%rax), %r12d
cmpl $0x4, %r12d
je 0x211e1
cmpl $0xb, %r12d
jne 0x211eb
movb $0x1, %r15b
movl $0x1, %ebx
jmp 0x212d3
movl $0x2, %ebx
jmp 0x212d3
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %r12d, %esi
callq *0x28(%rcx)
movl $0x1, %ebx
cmpl $0x20, %eax
je 0x212d0
movl %eax, %r15d
movq %rdx, %r12
leaq 0x25881(%rip), %rax     # 0x46a98
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
leaq 0x10(%rsp), %rdi
leaq 0x12f9c(%rip), %rdx     # 0x341c6
callq 0x9718
cmpb $0x1, 0x10(%rsp)
jne 0x2124c
movq 0x18(%rsp), %rdi
movl $0x23, %edx
leaq 0x155d9(%rip), %rsi     # 0x36820
callq 0x7580
cmpb $0x1, 0x10(%rsp)
jne 0x21260
movq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0x7990
cmpb $0x1, 0x10(%rsp)
jne 0x2127d
movq 0x18(%rsp), %rdi
movl $0x2, %edx
leaq 0x12e5a(%rip), %rsi     # 0x340d2
callq 0x7580
movq (%r12), %rax
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
movl %r15d, %edx
callq *0x20(%rax)
cmpb $0x1, 0x10(%rsp)
jne 0x212aa
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x7580
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x212c6
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x7510
leaq 0x10(%rsp), %rdi
callq 0x8f94
xorl %r15d, %r15d
testb $0x1, %bl
jne 0x21545
movq 0x88(%rbp), %rsi
movq 0x90(%rbp), %rdx
cmpq %rdx, %rsi
jne 0x2115c
cmpq %rdx, 0x98(%rbp)
je 0x21304
movq %r14, %rdi
callq 0x21fbc
leaq 0x70(%rbp), %rax
movq %rax, 0x70(%rsp)
leaq 0xd0(%rbp), %rax
movq %rax, 0x48(%rsp)
leaq 0x58(%rbp), %rax
movq %rax, 0x68(%rsp)
movq %rbp, 0x50(%rsp)
movq 0x70(%rbp), %r12
cmpq 0x78(%rbp), %r12
je 0x21515
movq 0x10(%r12), %rax
movq %rax, 0x40(%rsp)
movq (%r12), %r14
movq 0x8(%r12), %rbx
cmpq %r14, %rbx
je 0x214a1
movq 0x40(%rsp), %rsi
addq %r14, %rsi
movq %rbx, %rdx
subq %r14, %rdx
movl %r13d, %edi
xorl %ecx, %ecx
callq 0x7240
cmpq $-0x1, %rax
setne %bpl
je 0x2137d
addq %rax, %r14
xorl %eax, %eax
jmp 0x2148b
callq 0x7040
movl (%rax), %r13d
cmpl $0x4, %r13d
je 0x21398
cmpl $0xb, %r13d
jne 0x213a7
movl $0x7, %eax
jmp 0x2139d
movl $0x6, %eax
movl 0xc(%rsp), %r13d
jmp 0x2148b
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %r13d, %esi
callq *0x28(%rcx)
movl %eax, %r15d
movq %rdx, %r13
leaq 0x256d3(%rip), %rax     # 0x46a98
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
leaq 0x10(%rsp), %rdi
leaq 0x12dee(%rip), %rdx     # 0x341c6
callq 0x9718
cmpb $0x1, 0x10(%rsp)
jne 0x213fa
movq 0x18(%rsp), %rdi
movl $0x23, %edx
leaq 0x1542b(%rip), %rsi     # 0x36820
callq 0x7580
cmpb $0x1, 0x10(%rsp)
jne 0x2140e
movq 0x18(%rsp), %rdi
movl %r15d, %esi
callq 0x7990
cmpb $0x1, 0x10(%rsp)
jne 0x2142b
movq 0x18(%rsp), %rdi
movl $0x2, %edx
leaq 0x12cac(%rip), %rsi     # 0x340d2
callq 0x7580
movq (%r13), %rax
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
movl %r15d, %edx
callq *0x20(%rax)
cmpb $0x1, 0x10(%rsp)
movl 0xc(%rsp), %r13d
jne 0x2145d
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x7580
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x21479
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x7510
leaq 0x10(%rsp), %rdi
callq 0x8f94
movl $0x1, %eax
xorl %r15d, %r15d
testl %eax, %eax
je 0x21348
cmpl $0x6, %eax
je 0x21348
cmpl $0x7, %eax
jne 0x21505
cmpq %r14, %rbx
jne 0x214d4
movq 0x70(%rsp), %rdi
movq (%rdi), %rsi
callq 0x22382
movq 0x50(%rsp), %rcx
movq 0x60(%rcx), %rsi
cmpq 0x68(%rcx), %rsi
je 0x214f3
movq 0x40(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x60(%rcx)
movb $0x1, %bpl
jmp 0x21505
movq %r14, (%r12)
leaq 0x58(%rsp), %rdi
callq 0x17c94
movq 0x48(%rsp), %rdi
callq 0x7950
movb $0x1, %r15b
xorl %ebp, %ebp
jmp 0x21505
movb $0x1, %bpl
movq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x21e9c
testb %bpl, %bpl
movq 0x50(%rsp), %rbp
jne 0x21327
jmp 0x21545
movq %rbp, %rdi
movl %r13d, %esi
callq 0x17c04
cmpb $0x0, 0xa0(%rbp)
je 0x2152e
xorl %r15d, %r15d
jmp 0x21545
leaq 0x58(%rsp), %rdi
callq 0x17c94
movq 0x48(%rsp), %rdi
callq 0x7950
movb $0x1, %r15b
leaq 0x58(%rsp), %rdi
callq 0x17c42
andb $0x1, %r15b
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x21575
jmp 0x2159a
jmp 0x215ad
jmp 0x2159a
jmp 0x2159a
jmp 0x215ad
jmp 0x215ad
jmp 0x2159a
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x2159d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x2159d
jmp 0x215ad
jmp 0x2159a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8f94
jmp 0x215b0
jmp 0x215ad
jmp 0x215ad
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x17c42
movq %rbx, %rdi
callq 0x79b0