// File: /workspace/github_cpp_makefile_star2_O2/llefranc[P]42_webserv/srcs/server/HttpServer.cpp
// Function: HttpServer::sendToClients()
void HttpServer::sendToClients()
{
	for (std::list<ClientSocket>::iterator it = _clientSocks.begin(); it != _clientSocks.end(); ++it)
	{
		if (FD_ISSET(it->getFd(), &_writeFds))
		{
			const std::string *buffer = &it->getResponse()->getBuffer();
            
            long n = send(it->getFd(), static_cast<const void*>(buffer->c_str() + it->getNbBytesSent()), 
                           buffer->size() - it->getNbBytesSent(), 0);

            if (n != -1)
                it->updateNbBytesSent(n);
                
            // If an error occured or we have send everything, we close the connection. Else is still
            // some part of the buffer need to be send, going again through the select.
            if (n == -1 || it->getNbBytesSent() == buffer->size())
            {
                if (n == -1)
                    printLog("\033[1;31m >> FD " + convertNbToString(it->getFd()) + 
                        ": Failed to send response, connection closed\033[0m\n");
                else
                {
                    printLog("\033[1;32m >> FD " + convertNbToString(it->getFd()) + ": Response sent (code: " +
                        convertNbToString(it->getResponse()->getCode()) + "), connection closed\033[0m\n");
                }
                        
                std::list<ClientSocket>::iterator tmp = it--;
                closeConnection(&tmp);
            }

			// Exit for loop if no other sockets are ready
			--_nbReadyFds;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %r15
leaq 0x18(%rdi), %r13
movq %r13, -0x48(%rbp)
movq %rdi, -0x38(%rbp)
movq (%r13), %r13
cmpq -0x48(%rbp), %r13
je 0x9ceb
leaq 0x10(%r13), %r14
movq %r14, %rdi
callq 0xac4c
cltd
pushq $0x40
popq %r12
idivl %r12d
cltq
movq 0xb0(%r15,%rax,8), %rbx
movq %r14, %rdi
callq 0xac4c
cltd
idivl %r12d
btq %rdx, %rbx
jae 0x9a69
movq %r14, %rdi
callq 0xac5e
movq %rax, %rdi
callq 0xcbce
movq %rax, %r12
movq %r14, %rdi
callq 0xac4c
movl %eax, -0x3c(%rbp)
movq (%r12), %r15
movq %r14, %rdi
callq 0xac6c
addq %rax, %r15
movq 0x8(%r12), %rbx
movq %r14, %rdi
callq 0xac6c
subq %rax, %rbx
movl -0x3c(%rbp), %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x4230
cmpq $-0x1, %rax
je 0x9c26
movq %r14, %rdi
movq %rax, %rsi
callq 0xaf26
movq %r14, %rdi
callq 0xac6c
cmpq 0x8(%r12), %rax
jne 0x9cdb
movq %r14, %rdi
callq 0xac4c
movslq %eax, %rsi
leaq -0x108(%rbp), %r15
movq %r15, %rdi
callq 0xfc62
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
leaq 0x78f0(%rip), %rsi      # 0x11437
movq %r15, %rdx
callq 0x4f60
movq -0x38(%rbp), %r15
leaq -0xc8(%rbp), %r12
leaq -0x68(%rbp), %rdi
movq %rbx, %rsi
leaq 0x78de(%rip), %rdx      # 0x11446
callq 0x4f37
movq %r14, %rdi
callq 0xac5e
movq %rax, %rdi
callq 0xcbc4
movslq %eax, %rsi
leaq -0xe8(%rbp), %rdi
callq 0xfc62
leaq -0x88(%rbp), %rbx
movq %rbx, %rdi
leaq -0x68(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0xa3e1
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x78ab(%rip), %rdx      # 0x1145e
callq 0x4f37
leaq -0x128(%rbp), %rbx
movq %rbx, %rdi
leaq 0x7b31(%rip), %rsi      # 0x116fa
leaq -0x29(%rbp), %rdx
callq 0x4ec8
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10271
movq %rbx, %rdi
callq 0x47d8
movq %r12, %rdi
callq 0x47d8
leaq -0x88(%rbp), %rdi
callq 0x47d8
leaq -0xe8(%rbp), %rdi
callq 0x47d8
leaq -0x68(%rbp), %rdi
callq 0x47d8
leaq -0xa8(%rbp), %rdi
callq 0x47d8
leaq -0x108(%rbp), %rdi
jmp 0x9cbb
movq %r14, %rdi
callq 0xac4c
movslq %eax, %rsi
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
callq 0xfc62
leaq -0x88(%rbp), %r14
movq %r14, %rdi
leaq 0x77a8(%rip), %rsi      # 0x113f6
movq %rbx, %rdx
callq 0x4f60
leaq -0xc8(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
leaq 0x779b(%rip), %rdx      # 0x11405
callq 0x4f37
movq -0x38(%rbp), %r15
leaq -0xa8(%rbp), %rbx
movq %rbx, %rdi
leaq 0x7a76(%rip), %rsi      # 0x116fa
leaq -0x108(%rbp), %rdx
callq 0x4ec8
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10271
movq %rbx, %rdi
callq 0x47d8
movq %r12, %rdi
callq 0x47d8
leaq -0x88(%rbp), %rdi
callq 0x47d8
leaq -0x68(%rbp), %rdi
callq 0x47d8
movq 0x8(%r13), %rbx
movq %r13, -0xc8(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0xa1ce
movq %rbx, %r13
jmp 0x9cdf
movq -0x38(%rbp), %r15
decl 0x130(%r15)
jmp 0x9a69
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x9d37
movq %rax, %rbx
jmp 0x9d43
movq %rax, %rbx
jmp 0x9d99
movq %rax, %rbx
jmp 0x9da5
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
callq 0x47d8
jmp 0x9d2b
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x47d8
leaq -0x88(%rbp), %rdi
callq 0x47d8
leaq -0xe8(%rbp), %rdi
callq 0x47d8
jmp 0x9d90
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x47d8
jmp 0x9d65
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x47d8
jmp 0x9d76
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x47d8
jmp 0x9d87
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0x9dac
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x47d8
leaq -0xa8(%rbp), %rdi
callq 0x47d8
leaq -0x108(%rbp), %rdi
callq 0x47d8
movq %rbx, %rdi
callq 0x46d0
nop