// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1a8, %rsp            # imm = 0x1A8
movq %r8, %r12
movl %ecx, %r14d
movq %rdx, %r15
movl %esi, 0x28(%rsp)
movq %rdi, %rbx
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %r14d
shlq $0x4, %r14
xorl %r13d, %r13d
cmpq %rax, %r14
je 0x1c334
addq 0x8(%r15,%rax), %r13
addq $0x10, %rax
jmp 0x1c324
movq %r13, 0x30(%rsp)
leaq 0xd0(%rbx), %rbp
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1cfaa
testb %al, %al
jne 0x1c365
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x38(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7120
jmp 0x1c340
xorl %esi, %esi
cmpb $0x0, 0xa0(%rbx)
movl $0x0, %eax
jne 0x1c58f
xorl %eax, %eax
movslq %esi, %r12
cmpq %rax, %r14
je 0x1c3ae
movq 0x8(%r15,%rax), %rcx
testq %rcx, %rcx
je 0x1c3a8
movq (%r15,%rax), %rdx
movq %r12, %rsi
shlq $0x4, %rsi
movq %rdx, 0x60(%rsp,%rsi)
movq %rcx, 0x68(%rsp,%rsi)
incl %r12d
movl %r12d, %esi
addq $0x10, %rax
jmp 0x1c37b
movq 0x70(%rbx), %rax
xorl %r13d, %r13d
cmpq 0x78(%rbx), %rax
movq %rsi, %r14
je 0x1c5a1
leaq 0x70(%rbx), %rax
movq %rax, 0x28(%rsp)
movslq %r13d, %r15
movq %r15, %rax
shlq $0x4, %rax
leaq 0x60(%rsp), %rcx
movq %rax, 0x20(%rsp)
addq %rcx, %rax
addq $0x8, %rax
xorl %ebp, %ebp
movq %r15, %rcx
cmpq %r12, %rcx
jge 0x1c3fc
addq (%rax), %rbp
incq %rcx
addq $0x10, %rax
jmp 0x1c3eb
movq %rbp, 0x30(%rsp)
movq %rbx, %rdi
callq 0x1cffc
cmpq %rbp, %rax
jae 0x1c4a2
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x9360
leaq 0xc330(%rip), %rsi      # 0x28756
movq %r14, %rdi
callq 0x1d0c4
leaq 0x30(%rsp), %rsi
movq %rax, %rdi
callq 0xf538
leaq 0xc2ef(%rip), %rsi      # 0x28731
movq %rax, %rdi
callq 0x1d036
leaq 0x38(%rsp), %rdi
callq 0x86e4
leaq 0x88(%rbx), %rdi
movq 0x30(%rsp), %rsi
callq 0x1d04e
movq 0x88(%rbx), %rbx
movq 0x20(%rsp), %rax
leaq 0x60(%rsp), %rcx
leaq (%rax,%rcx), %r14
addq $0x8, %r14
cmpq %r12, %r15
jge 0x1c58d
movq -0x8(%r14), %rsi
movq (%r14), %rdx
movq %rbx, %rdi
callq 0x7420
addq (%r14), %rbx
incq %r15
addq $0x10, %r14
jmp 0x1c47e
movq %r14, %rax
cmpl %eax, %r13d
jge 0x1c58d
movq 0x78(%rbx), %rcx
cmpq %rcx, 0x70(%rbx)
je 0x1c4c4
movq -0x10(%rcx), %rax
cmpq $0x100000, %rax         # imm = 0x100000
jne 0x1c504
movq 0x60(%rbx), %rax
movq -0x8(%rax), %rcx
addq $-0x8, %rax
movq %rcx, 0x38(%rsp)
andq $0x0, 0x10(%rsp)
movq %rax, 0x60(%rbx)
andl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0xc(%rsp), %rcx
callq 0x1d07a
movq 0x10(%rsp), %rax
jmp 0x1c512
movq -0x8(%rcx), %rcx
movq %rcx, 0x38(%rsp)
movq %rax, 0x10(%rsp)
movl $0x100000, %r12d        # imm = 0x100000
subq %rax, %r12
movslq %r13d, %rax
shlq $0x4, %rax
leaq (%rsp,%rax), %r15
addq $0x60, %r15
testq %r12, %r12
je 0x1c4a2
movq 0x8(%r15), %rbp
cmpq %r12, %rbp
cmovaeq %r12, %rbp
movq (%r15), %rsi
movq 0x38(%rsp), %rdi
addq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0x7420
subq %rbp, %r12
movq 0x10(%rsp), %rax
addq %rbp, %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rbx), %rcx
movq %rax, -0x10(%rcx)
movq 0x8(%r15), %rax
subq %rbp, %rax
je 0x1c57d
addq %rbp, (%r15)
movq %rax, 0x8(%r15)
jmp 0x1c52a
incl %r13d
movq %r14, %rax
cmpl %eax, %r13d
jne 0x1c51b
jmp 0x1c4a5
movb $0x1, %al
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
movslq %r13d, %rax
movl %esi, %r15d
subl %eax, %r15d
shlq $0x4, %rax
movq %rax, 0x58(%rsp)
addq %rsp, %rax
addq $0x60, %rax
movq %rax, 0x20(%rsp)
cmpl %r13d, %r14d
jle 0x1c629
movl 0x28(%rsp), %edi
movq 0x20(%rsp), %rsi
movl %r15d, %edx
callq 0x7280
cmpq $-0x1, %rax
jne 0x1c5ec
callq 0x7030
movl (%rax), %ebp
cmpl $0x4, %ebp
je 0x1c5c2
jmp 0x1c624
leaq 0x68(%rsp), %rcx
movq 0x58(%rsp), %rdi
addq %rcx, %rdi
movq %r14, %rsi
testq %rax, %rax
jle 0x1c5a4
movq (%rdi), %rdx
movq %rax, %rcx
subq %rdx, %rcx
jb 0x1c618
incl %r13d
addq $0x10, %rdi
movq %rcx, %rax
jmp 0x1c5fc
addq %rax, -0x8(%rdi)
subq %rax, %rdx
movq %rdx, (%rdi)
jmp 0x1c5a4
cmpl $0xb, %ebp
jne 0x1c63c
movb $0x1, %al
movq %r14, %rcx
cmpl %r13d, %ecx
jne 0x1c3c2
jmp 0x1c58f
callq 0x72f0
movq (%rax), %rcx
movq %rax, %rdi
movl %ebp, %esi
callq *0x28(%rcx)
cmpl $0x20, %eax
je 0x1c6c4
movl %eax, %ebx
movq %rdx, %r14
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x865e
leaq 0xc0d4(%rip), %rsi      # 0x2873e
movq %r15, %rdi
callq 0xd0da
leaq 0xc(%rsp), %rsi
movl %ebx, (%rsi)
movq %rax, %rdi
callq 0x93b8
leaq 0x9a4a(%rip), %rsi      # 0x260d2
movq %rax, %rdi
callq 0x86b2
movq %rax, %r15
movq (%r14), %rax
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq *0x20(%rax)
leaq 0x38(%rsp), %rsi
movq %r15, %rdi
callq 0x869a
leaq 0x38(%rsp), %rdi
callq 0x71d0
leaq 0x10(%rsp), %rdi
callq 0x86e4
xorl %eax, %eax
jmp 0x1c58f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x71d0
jmp 0x1c6e1
jmp 0x1c6de
jmp 0x1c6de
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x1c6f0
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x86e4
movq %rbx, %rdi
callq 0x7990
nop