// File: /workspace/github_cpp_makefile_star2_O1/guoxuanhan[P]NetServer/tcpconnection.cpp
// Function: sendn(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
int sendn(int fd, std::string &bufferout)
{
    ssize_t nbyte = 0;
    int sendsum = 0;
    //char buffer[BUFSIZE+1];
    size_t length = 0;
    //length = bufferout.copy(buffer, BUFSIZE, 0);
    //buffer[length] = '\0';
    // if(bufferout.size() >= BUFSIZE)
    // {
    // 	length =  BUFSIZE;
    // }
    // else
    // {
    // 	length =  bufferout.size();
    // }
    //无拷贝优化
    length = bufferout.size();
    if(length >= BUFSIZE)
    {
        length = BUFSIZE;
    }
    for (;;)
    {
        //nbyte = send(fd, buffer, length, 0);
        //nbyte = send(fd, bufferout.c_str(), length, 0);
        nbyte = write(fd, bufferout.c_str(), length);
        if (nbyte > 0)
        {
            sendsum += nbyte;
            bufferout.erase(0, nbyte);
            //length = bufferout.copy(buffer, BUFSIZE, 0);
            //buffer[length] = '\0';
            length = bufferout.size();
            if(length >= BUFSIZE)
            {
                length = BUFSIZE;
            }
            if (length == 0 )
            {
                return sendsum;
            }
        }
        else if (nbyte < 0)//异常
        {
            if (errno == EAGAIN)//系统缓冲区满，非阻塞返回
            {
                std::cout << "write errno == EAGAIN,not finish!" << std::endl;
                return sendsum;
            }
            else if (errno == EINTR)
            {
                std::cout << "write errno == EINTR" << std::endl;
                continue;
            }
            else if (errno == EPIPE)
            {
                //客户端已经close，并发了RST，继续wirte会报EPIPE，返回0，表示close
                perror("write error");
                std::cout << "write errno == client send RST" << std::endl;
                return -1;
            }
            else
            {
                perror("write error");//Connection reset by peer
                std::cout << "write error, unknow error" << std::endl;
                return -1;
            }
        }
        else//返回0
        {
            //应该不会返回0
            //std::cout << "client close the Socket!" << std::endl;
            return 0;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r15
movq 0x8(%rsi), %r13
movl $0x1000, %eax           # imm = 0x1000
cmpq %rax, %r13
cmovaeq %rax, %r13
movl %edi, %ebp
xorl %ebx, %ebx
movq 0xb504(%rip), %r14      # 0x17fb8
leaq 0x466b(%rip), %r12      # 0x11126
movq (%r15), %rsi
movl %ebp, %edi
movq %r13, %rdx
callq 0x4120
testq %rax, %rax
jg 0xcb1b
testq %rax, %rax
jns 0xcb8d
callq 0x4050
movl (%rax), %eax
cmpl $0x4, %eax
jne 0xcb4b
movl $0x14, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x43a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4320
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4240
jmp 0xcabb
addl %eax, %ebx
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x4090
movq 0x8(%r15), %rax
movl $0x1000, %ecx           # imm = 0x1000
cmpq %rcx, %rax
movl $0x1000, %r13d          # imm = 0x1000
cmovbq %rax, %r13
testq %rax, %rax
jne 0xcabb
jmp 0xcb8f
cmpl $0xb, %eax
jne 0xcba0
leaq 0x45ad(%rip), %rsi      # 0x11104
movl $0x21, %edx
movq %r14, %rdi
callq 0x43a0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4320
movsbl %al, %esi
movq %r14, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4240
jmp 0xcb8f
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x20, %eax
jne 0xcbac
callq 0x473d
jmp 0xcbb1
callq 0x476c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xcb8f