// File: JianjiaoSQ[P]MyPoorWebServer/http/http_conn.cpp
// Function: http_conn::write()
bool http_conn::write()
{
    int temp = 0;

    if (bytes_to_send == 0)
    {
        modfd(m_epollfd, m_sockfd, EPOLLIN, m_TRIGMode);
        init();
        return true;
    }

    while (1)
    {
        temp = writev(m_sockfd, m_iv, m_iv_count);

        if (temp < 0)
        {
            if (errno == EAGAIN)
            {
                modfd(m_epollfd, m_sockfd, EPOLLOUT, m_TRIGMode);
                return true;
            }
            unmap();
            return false;
        }

        bytes_have_send += temp;
        bytes_to_send -= temp;
        if (bytes_have_send >= m_iv[0].iov_len)
        {
            m_iv[0].iov_len = 0;
            m_iv[1].iov_base = m_file_address + (bytes_have_send - m_write_idx);
            m_iv[1].iov_len = bytes_to_send;
        }
        else
        {
            m_iv[0].iov_base = m_write_buf + bytes_have_send;
            m_iv[0].iov_len = m_iv[0].iov_len - bytes_have_send;
        }

        if (bytes_to_send <= 0)
        {
            unmap();
            modfd(m_epollfd, m_sockfd, EPOLLIN, m_TRIGMode);

            if (m_linger) // 写完之后是否保持连接
            {
                init();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x0, 0xdf0(%rax)
jne 0x65b3
movq -0x20(%rbp), %rax
movl 0xded2(%rip), %edi      # 0x14460
movl 0x14(%rax), %esi
movl 0xe30(%rax), %ecx
movl $0x1, %edx
callq 0x4dc0
movq -0x20(%rbp), %rdi
callq 0x4fd0
movb $0x1, -0x1(%rbp)
jmp 0x6720
jmp 0x65b5
movq -0x20(%rbp), %rax
movl 0x14(%rax), %edi
movq %rax, %rsi
addq $0xdc0, %rsi            # imm = 0xDC0
movl 0xde0(%rax), %edx
callq 0x3260
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x661c
callq 0x3060
cmpl $0xb, (%rax)
jne 0x660a
movq -0x20(%rbp), %rax
movl 0xde72(%rip), %edi      # 0x14460
movl 0x14(%rax), %esi
movl 0xe30(%rax), %ecx
movl $0x4, %edx
callq 0x4dc0
movb $0x1, -0x1(%rbp)
jmp 0x6720
movq -0x20(%rbp), %rdi
callq 0x6510
movb $0x0, -0x1(%rbp)
jmp 0x6720
movq -0x20(%rbp), %rcx
movl -0x14(%rbp), %eax
addl 0xdf4(%rcx), %eax
movl %eax, 0xdf4(%rcx)
movl -0x14(%rbp), %edx
movl 0xdf0(%rcx), %eax
subl %edx, %eax
movl %eax, 0xdf0(%rcx)
movslq 0xdf4(%rcx), %rax
cmpq 0xdc8(%rcx), %rax
jb 0x668f
movq -0x20(%rbp), %rax
movq $0x0, 0xdc8(%rax)
movq 0xd28(%rax), %rcx
movl 0xdf4(%rax), %edx
subl 0xc34(%rax), %edx
movslq %edx, %rdx
addq %rdx, %rcx
movq %rcx, 0xdd0(%rax)
movslq 0xdf0(%rax), %rcx
movq %rcx, 0xdd8(%rax)
jmp 0x66c6
movq -0x20(%rbp), %rax
movq %rax, %rcx
addq $0x834, %rcx            # imm = 0x834
movslq 0xdf4(%rax), %rdx
addq %rdx, %rcx
movq %rcx, 0xdc0(%rax)
movq 0xdc8(%rax), %rcx
movslq 0xdf4(%rax), %rdx
subq %rdx, %rcx
movq %rcx, 0xdc8(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0xdf0(%rax)
jg 0x671b
movq -0x20(%rbp), %rdi
callq 0x6510
movq -0x20(%rbp), %rax
movl 0xdd7a(%rip), %edi      # 0x14460
movl 0x14(%rax), %esi
movl 0xe30(%rax), %ecx
movl $0x1, %edx
callq 0x4dc0
movq -0x20(%rbp), %rax
testb $0x1, 0xd24(%rax)
je 0x6715
movq -0x20(%rbp), %rdi
callq 0x4fd0
movb $0x1, -0x1(%rbp)
jmp 0x6720
movb $0x0, -0x1(%rbp)
jmp 0x6720
jmp 0x65b5
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax,%rax)