// File: /workspace/github_cpp_makefile_star2_O3/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 $0x198, %rsp            # imm = 0x198
movq %r8, %r12
movl %ecx, %r15d
movq %rdx, %r14
movl %esi, 0x8(%rsp)
movq %rdi, %rbx
testl %ecx, %ecx
jle 0x203bd
movl %r15d, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
xorl %r13d, %r13d
addq 0x8(%r14,%rcx), %r13
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x203ad
jmp 0x203c0
xorl %r13d, %r13d
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21410
testb %al, %al
jne 0x203f9
leaq 0xd0(%rbx), %rbp
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7100
movq %rbx, %rdi
movq %r13, %rsi
callq 0x21410
testb %al, %al
je 0x203d6
cmpb $0x0, 0xa0(%rbx)
je 0x20409
xorl %ebp, %ebp
jmp 0x20779
testl %r15d, %r15d
jle 0x204d5
movl %r15d, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
xorl %r12d, %r12d
movl 0x8(%rsp), %ebp
movq 0x8(%r14,%rcx), %rdx
testq %rdx, %rdx
je 0x20447
movq (%r14,%rcx), %rsi
movslq %r12d, %r12
movq %r12, %rdi
shlq $0x4, %rdi
movq %rsi, 0x50(%rsp,%rdi)
movq %rdx, 0x58(%rsp,%rdi)
incl %r12d
addq $0x10, %rcx
cmpq %rcx, %rax
jne 0x20422
movq 0x70(%rbx), %rax
xorl %r13d, %r13d
cmpq 0x78(%rbx), %rax
jne 0x2050f
xorl %r13d, %r13d
testl %r12d, %r12d
jle 0x20503
leaq 0x58(%rsp), %r14
movslq %r13d, %r15
movl %r12d, %edx
subl %r15d, %edx
shlq $0x4, %r15
leaq (%rsp,%r15), %rsi
addq $0x50, %rsi
movl %ebp, %edi
callq 0x7270
cmpq $-0x1, %rax
je 0x204b2
testq %rax, %rax
jle 0x204ce
addq %r14, %r15
movq (%r15), %rcx
cmpq %rcx, %rax
jb 0x204c4
incl %r13d
addq $0x10, %r15
subq %rcx, %rax
jg 0x2049c
jmp 0x204ce
callq 0x7040
movl (%rax), %ebp
cmpl $0x4, %ebp
jne 0x204fa
movl 0x8(%rsp), %ebp
jmp 0x204ce
addq %rax, -0x8(%r15)
subq %rax, %rcx
movq %rcx, (%r15)
cmpl %r12d, %r13d
jl 0x20472
jmp 0x20503
movq 0x70(%rbx), %rax
cmpq 0x78(%rbx), %rax
je 0x20776
leaq 0x70(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x78(%rbx), %rbp
xorl %esi, %esi
xorl %r13d, %r13d
xorl %r12d, %r12d
jmp 0x20551
cmpl $0xb, %ebp
jne 0x2078d
movb $0x1, %bpl
cmpl %r12d, %r13d
je 0x20779
leaq 0x70(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x78(%rbx), %rbp
cmpl %r12d, %r13d
setl %sil
jge 0x2054f
movslq %r13d, %rcx
movslq %r12d, %rax
subq %rcx, %rax
shlq $0x4, %rcx
leaq 0x50(%rsp), %rdx
addq %rdx, %rcx
addq $0x8, %rcx
xorl %r15d, %r15d
addq (%rcx), %r15
addq $0x10, %rcx
decq %rax
jne 0x20541
jmp 0x20554
xorl %esi, %esi
xorl %r15d, %r15d
movq 0x60(%rbx), %rax
subq 0x58(%rbx), %rax
shlq $0x11, %rax
movq 0x78(%rbx), %rcx
cmpq %rcx, 0x70(%rbx)
je 0x20574
subq -0x10(%rcx), %rax
addq $0x100000, %rax         # imm = 0x100000
cmpq %r15, %rax
jae 0x2065f
movq %rsi, 0x8(%rsp)
leaq 0x264ff(%rip), %rax     # 0x46a88
movl (%rax), %eax
xorl %esi, %esi
cmpl $0x3, %eax
setge %sil
leaq 0x13cac(%rip), %rdx     # 0x34247
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x96e6
movb (%r14), %al
cmpb $0x1, %al
jne 0x205c9
movq 0x28(%rsp), %rdi
leaq 0x1620b(%rip), %rsi     # 0x367c6
movl $0x1f, %edx
callq 0x7570
movb 0x20(%rsp), %al
testb $0x1, %al
je 0x205de
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x7330
movb 0x20(%rsp), %al
testb $0x1, %al
je 0x205f8
movq 0x28(%rsp), %rdi
leaq 0x161b3(%rip), %rsi     # 0x367a1
movl $0xc, %edx
callq 0x7570
leaq 0x20(%rsp), %rdi
callq 0x8f4e
addq $0x88, %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21456
movb $0x1, %bpl
cmpb $0x0, 0x8(%rsp)
je 0x20779
movq (%rbx), %rbx
movslq %r13d, %rax
movslq %r12d, %r14
subq %rax, %r14
shlq $0x4, %rax
leaq 0x50(%rsp), %rcx
leaq (%rax,%rcx), %r15
addq $0x8, %r15
movq -0x8(%r15), %rsi
movq (%r15), %rdx
movq %rbx, %rdi
callq 0x7400
addq (%r15), %rbx
addq $0x10, %r15
decq %r14
jne 0x2063f
jmp 0x20779
testb %sil, %sil
je 0x20776
movq %rbx, 0x48(%rsp)
movq (%rbp), %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, (%rax)
je 0x20687
movq -0x10(%rcx), %rax
cmpq $0x100000, %rax         # imm = 0x100000
jne 0x206cd
movq 0x60(%rbx), %rax
movq -0x8(%rax), %rcx
addq $-0x8, %rax
movq %rcx, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
movq %rax, 0x60(%rbx)
movl $0x0, 0x44(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x44(%rsp), %rcx
callq 0x21482
movq 0x10(%rsp), %rax
jmp 0x206db
movq -0x8(%rcx), %rcx
movq %rcx, 0x20(%rsp)
movq %rax, 0x10(%rsp)
movl $0x100000, %r14d        # imm = 0x100000
subq %rax, %r14
testq %r14, %r14
je 0x2076d
movq %r12, %rbx
movslq %r13d, %rax
shlq $0x4, %rax
leaq (%rsp,%rax), %r12
addq $0x50, %r12
movq 0x8(%r12), %r15
cmpq %r14, %r15
cmovaeq %r14, %r15
movq (%r12), %rsi
movq 0x20(%rsp), %rdi
addq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x7400
subq %r15, %r14
movq 0x10(%rsp), %rax
addq %r15, %rax
movq %rax, 0x10(%rsp)
movq (%rbp), %rcx
movq %rax, -0x10(%rcx)
movq 0x8(%r12), %rax
subq %r15, %rax
je 0x20753
addq %r15, (%r12)
movq %rax, 0x8(%r12)
testq %r14, %r14
jne 0x206ff
jmp 0x20765
incl %r13d
movq %rbx, %r12
cmpl %r12d, %r13d
movq 0x48(%rsp), %rbx
jne 0x206e4
jmp 0x2076d
movq %rbx, %r12
movq 0x48(%rsp), %rbx
cmpl %r12d, %r13d
jl 0x2066d
movb $0x1, %bpl
movl %ebp, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x72e0
movq (%rax), %rcx
movq %rax, %rdi
movl %ebp, %esi
callq *0x28(%rcx)
cmpl $0x20, %eax
je 0x20402
movl %eax, %ebx
movq %rdx, %r14
leaq 0x262d6(%rip), %rax     # 0x46a88
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
leaq 0x13a06(%rip), %rdx     # 0x341c6
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x96e6
movb (%r15), %al
cmpb $0x1, %al
jne 0x207ee
movq 0x18(%rsp), %rdi
leaq 0x15fce(%rip), %rsi     # 0x367ae
movl $0x17, %edx
callq 0x7570
movb 0x10(%rsp), %al
testb $0x1, %al
je 0x20802
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x7980
movb 0x10(%rsp), %al
testb $0x1, %al
je 0x2081c
movq 0x18(%rsp), %rdi
leaq 0x138c0(%rip), %rsi     # 0x340d2
movl $0x2, %edx
callq 0x7570
movq (%r14), %rax
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
cmpb $0x1, 0x10(%rsp)
jne 0x20847
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0x7570
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20862
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x7500
leaq 0x10(%rsp), %rdi
callq 0x8f4e
jmp 0x20402
movq %rax, %rbx
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x20898
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x7500
jmp 0x20898
jmp 0x20895
jmp 0x20895
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x208a7
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x8f4e
movq %rbx, %rdi
callq 0x79a0