// File: /workspace/github_cpp_makefile_star2_O1/cybozu[P]yrmcds/cybozu/tcp.cpp
// Function: cybozu::tcp_socket::_sendv(int, cybozu::tcp_socket::iovec const*, int, std::unique_lock<std::mutex>&)
bool tcp_socket::_sendv(int fd, const iovec* iov, const int iovcnt, lock_guard& g) {
    std::size_t total = 0;
    for( int i = 0; i < iovcnt; ++i ) {
        total += iov[i].len;
    }

    while( ! can_send(total) ) {
        on_buffer_full();
        m_cond_write.wait(g);
    }
    if( m_shutdown ) return false;

    ::iovec v[MAX_IOVCNT];
    int v_size = 0;
    for( int i = 0; i < iovcnt; ++i ) {
        if( iov[i].len == 0 )
            continue;
        v[v_size].iov_base = const_cast<char*>(iov[i].p);
        v[v_size].iov_len = iov[i].len;
        ++v_size;
    }
    int ind = 0;

    if( m_pending.empty() ) {
        while( ind < v_size ) {
            ssize_t n = ::writev(fd, &(v[ind]), v_size - ind);
            if( n == -1 ) {
                if( errno == EAGAIN || errno == EWOULDBLOCK ) break;
                if( errno == EINTR ) continue;
                auto ecnd = std::system_category().default_error_condition(errno);
                if( ecnd.value() != EPIPE )
                    logger::error() << "<tcp_socket::_sendv>: ("
                                    << ecnd.value() << ") "
                                    << ecnd.message();
                return false;
            }
            while( n > 0 ) {
                if( static_cast<std::size_t>(n) < v[ind].iov_len ) {
                    v[ind].iov_base = ((char*)v[ind].iov_base) + n;
                    v[ind].iov_len = v[ind].iov_len - n;
                    break;
                }
                n -= v[ind].iov_len;
                ++ind;
            }
        }
        if( ind == v_size ) return true;
    }

    // recalculate total length
    total = 0;
    for( int i = ind; i < v_size; ++i ) {
        total += v[i].iov_len;
    }

    // put data in the pending request queue.
    if( capacity() < total ) {
        // here, m_pending.empty() and m_tmpbuf.empty() holds true.
        logger::debug() << "<tcp_socket::_sendv> buffering "
                        << total << " bytes data.";
        m_tmpbuf.resize(total);
        char* p = m_tmpbuf.data();
        for( int i = ind; i < v_size; ++i ) {
            std::memcpy(p, v[i].iov_base, v[i].iov_len);
            p += v[i].iov_len;
        }
        return true;
    }

    while( ind < v_size ) {
        char* t_p;
        std::size_t t_len;
        if(m_pending.empty() || std::get<1>(m_pending.back()) == SENDBUF_SIZE) {
            t_p = m_free_buffers.back();
            t_len = 0;
            m_free_buffers.pop_back();
            m_pending.emplace_back(t_p, t_len, 0);
        } else {
            std::tie(t_p, t_len, std::ignore) = m_pending.back();
        }
        std::size_t room = SENDBUF_SIZE - t_len;
        while( room > 0 ) {
            std::size_t to_write = std::min(room, v[ind].iov_len);
            std::memcpy(t_p + t_len, v[ind].iov_base, to_write);
            room -= to_write;
            t_len += to_write;
            std::get<1>(m_pending.back()) = t_len;
            if( to_write == v[ind].iov_len ) {
                ++ind;
                if( ind == v_size ) break;
                continue;
            }
            v[ind].iov_base = ((char*)v[ind].iov_base) + to_write;
            v[ind].iov_len -= to_write;
        }
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %r8, %r14
movl %ecx, %r12d
movq %rdx, %r15
movl %esi, 0x10(%rsp)
movq %rdi, %rbx
testl %ecx, %ecx
jle 0x20c37
movl %r12d, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
xorl %r13d, %r13d
addq 0x8(%r15,%rcx), %r13
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x20c27
jmp 0x20c3a
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21d46
testb %al, %al
jne 0x20c73
leaq 0xd0(%rbx), %rbp
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x7100
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21d46
testb %al, %al
je 0x20c50
xorl %ebp, %ebp
cmpb $0x0, 0xa0(%rbx)
movl $0x0, %eax
je 0x20c95
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
jle 0x20cd2
movl %r12d, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
xorl %ebp, %ebp
movq 0x8(%r15,%rcx), %rdx
testq %rdx, %rdx
je 0x20cc9
movq (%r15,%rcx), %rsi
movslq %ebp, %rbp
movq %rbp, %rdi
shlq $0x4, %rdi
movq %rsi, 0x40(%rsp,%rdi)
movq %rdx, 0x48(%rsp,%rdi)
incl %ebp
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x20ca5
movq 0x70(%rbx), %rax
xorl %r15d, %r15d
cmpq 0x78(%rbx), %rax
jne 0x20e88
xorl %r15d, %r15d
testl %ebp, %ebp
jle 0x20e7d
movl 0x10(%rsp), %r12d
movslq %r15d, %r14
movl %ebp, %edx
subl %r14d, %edx
shlq $0x4, %r14
leaq (%rsp,%r14), %rsi
addq $0x40, %rsi
movl %r12d, %edi
callq 0x7270
cmpq $-0x1, %rax
je 0x20d42
xorl %r13d, %r13d
testq %rax, %rax
jle 0x20e5d
leaq 0x48(%rsp), %rcx
addq %rcx, %r14
movq (%r14), %rcx
cmpq %rcx, %rax
jb 0x20d65
incl %r15d
addq $0x10, %r14
subq %rcx, %rax
jg 0x20d29
jmp 0x20e5d
callq 0x7040
movl (%rax), %r13d
cmpl $0xb, %r13d
je 0x20e5d
cmpl $0x4, %r13d
jne 0x20d74
movl $0xa, %r13d
jmp 0x20e5d
addq %rax, -0x8(%r14)
subq %rax, %rcx
movq %rcx, (%r14)
jmp 0x20e5d
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %r13d, %esi
callq *0x28(%rcx)
movl $0x1, %r13d
cmpl $0x20, %eax
jne 0x20d9a
movl 0x10(%rsp), %r12d
jmp 0x20e5d
movl %eax, %r14d
movq %rdx, %r12
leaq 0x25cf1(%rip), %rax     # 0x46a98
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
movq %rsp, %rdi
leaq 0x1340e(%rip), %rdx     # 0x341c6
callq 0x9718
cmpb $0x1, (%rsp)
jne 0x20dd9
movq 0x8(%rsp), %rdi
movl $0x17, %edx
leaq 0x15a14(%rip), %rsi     # 0x367e8
callq 0x7580
cmpb $0x1, (%rsp)
jne 0x20dec
movq 0x8(%rsp), %rdi
movl %r14d, %esi
callq 0x7990
cmpb $0x1, (%rsp)
jne 0x20e08
movq 0x8(%rsp), %rdi
movl $0x2, %edx
leaq 0x132cf(%rip), %rsi     # 0x340d2
callq 0x7580
movq (%r12), %rax
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
movl %r14d, %edx
callq *0x20(%rax)
cmpb $0x1, (%rsp)
movl 0x10(%rsp), %r12d
jne 0x20e39
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x7580
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x20e55
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7510
movq %rsp, %rdi
callq 0x8f94
testl %r13d, %r13d
je 0x20e68
cmpl $0xa, %r13d
jne 0x20e73
cmpl %ebp, %r15d
jl 0x20cf3
jmp 0x20e7d
cmpl $0xb, %r13d
jne 0x210bb
movb $0x1, %al
cmpl %ebp, %r15d
je 0x20c83
cmpl %ebp, %r15d
jge 0x20eb7
movslq %r15d, %rcx
movslq %ebp, %rax
subq %rcx, %rax
shlq $0x4, %rcx
leaq 0x40(%rsp), %rdx
addq %rdx, %rcx
addq $0x8, %rcx
xorl %r14d, %r14d
addq (%rcx), %r14
addq $0x10, %rcx
decq %rax
jne 0x20ea9
jmp 0x20eba
xorl %r14d, %r14d
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shlq $0x11, %rax
movq 0x78(%rbx), %rcx
cmpq %rcx, 0x70(%rbx)
je 0x20eda
subq -0x10(%rcx), %rax
addq $0x100000, %rax         # imm = 0x100000
cmpq %r14, %rax
jae 0x20fbd
leaq 0x25bae(%rip), %rax     # 0x46a98
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x3, %eax
setge %sil
leaq 0x1334b(%rip), %rdx     # 0x34247
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x9718
cmpb $0x1, (%r12)
jne 0x20f26
movq 0x20(%rsp), %rdi
leaq 0x158e4(%rip), %rsi     # 0x36800
movl $0x1f, %edx
callq 0x7580
cmpb $0x1, 0x18(%rsp)
jne 0x20f3a
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x7330
cmpb $0x1, 0x18(%rsp)
jne 0x20f57
movq 0x20(%rsp), %rdi
leaq 0x1588e(%rip), %rsi     # 0x367db
movl $0xc, %edx
callq 0x7580
leaq 0x18(%rsp), %rdi
callq 0x8f94
addq $0x88, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d94
movb $0x1, %al
cmpl %ebp, %r15d
jge 0x20c83
movq (%rbx), %rbx
movslq %r15d, %rax
movslq %ebp, %r14
subq %rax, %r14
shlq $0x4, %rax
leaq 0x40(%rsp), %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq -0x8(%r15), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq 0x7410
addq (%r15), %rbx
addq $0x10, %r15
decq %r14
jne 0x20f9b
movb $0x1, %al
jmp 0x20c83
movb $0x1, %al
cmpl %ebp, %r15d
jge 0x20c83
leaq 0x70(%rbx), %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rbx), %rax
cmpq %rax, 0x70(%rbx)
je 0x20fe8
movq -0x10(%rax), %rcx
cmpq $0x100000, %rcx         # imm = 0x100000
jne 0x21026
movq 0x60(%rbx), %rax
movq -0x8(%rax), %rcx
addq $-0x8, %rax
movq %rcx, 0x18(%rsp)
movq $0x0, (%rsp)
movq %rax, 0x60(%rbx)
movl $0x0, 0x3c(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %rsp, %rdx
leaq 0x3c(%rsp), %rcx
callq 0x21dc0
jmp 0x21033
movq -0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq %rcx, (%rsp)
movl $0x100000, %r14d        # imm = 0x100000
subq (%rsp), %r14
testq %r14, %r14
je 0x210ab
movslq %r15d, %r13
shlq $0x4, %r13
movq 0x48(%rsp,%r13), %r12
cmpq %r14, %r12
cmovaeq %r14, %r12
movq 0x40(%rsp,%r13), %rsi
movq 0x18(%rsp), %rdi
addq (%rsp), %rdi
movq %r12, %rdx
callq 0x7410
movq (%rsp), %rax
addq %r12, %rax
movq %rax, (%rsp)
movq 0x78(%rbx), %rcx
movq %rax, -0x10(%rcx)
movq 0x48(%rsp,%r13), %rax
subq %r12, %rax
jne 0x21093
incl %r15d
cmpl %ebp, %r15d
sete %al
jmp 0x210a4
leaq (%rsp,%r13), %rcx
addq $0x40, %rcx
addq %r12, (%rcx)
movq %rax, 0x8(%rcx)
xorl %eax, %eax
subq %r12, %r14
testb %al, %al
je 0x2103d
cmpl %ebp, %r15d
movb $0x1, %al
jl 0x20fd1
jmp 0x20c83
xorl %eax, %eax
jmp 0x20c83
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x210ea
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x7510
jmp 0x210ea
jmp 0x210e7
jmp 0x210e7
movq %rax, %rbx
movq %rsp, %rdi
jmp 0x210f7
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x8f94
movq %rbx, %rdi
callq 0x79b0