// File: /workspace/github_cpp_makefile_star2_O1/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
leaq 0x18(%rdi), %r12
movq 0x18(%rdi), %r15
cmpq %r12, %r15
je 0xae0b
movq %rdi, %r13
movq %rdi, -0xb8(%rbp)
movq %r12, -0xb0(%rbp)
leaq 0x10(%r15), %r14
movq %r14, %rdi
callq 0xbfde
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rax
movq 0xb0(%r13,%rax,8), %rbx
movq %r14, %rdi
callq 0xbfde
btq %rax, %rbx
jae 0xadff
movq %r15, -0xc8(%rbp)
movq %r14, %rdi
callq 0xbff0
movq %rax, %rdi
callq 0xe802
movq %rax, %r13
movq %r14, %rdi
callq 0xbfde
movl %eax, %r15d
movq (%r13), %rbx
movq %r14, %rdi
callq 0xbffe
addq %rax, %rbx
movq 0x8(%r13), %r12
movq %r14, %rdi
callq 0xbffe
subq %rax, %r12
movl %r15d, %edi
movq %rbx, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x4200
cmpq $-0x1, %rax
movq %r14, %r15
movq %r14, %rdi
je 0xa9a5
movq %rax, %rsi
callq 0xc4da
movq %r15, %rdi
callq 0xbffe
cmpq 0x8(%r13), %rax
leaq -0x98(%rbp), %r14
jne 0xaa08
movq %r15, -0xc0(%rbp)
movq %r15, %rdi
callq 0xbfde
movslq %eax, %rsi
leaq -0x128(%rbp), %rbx
movq %rbx, %rdi
callq 0x1378a
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xaae1(%rip), %rcx      # 0x1543e
callq 0x4440
leaq -0x78(%rbp), %rbx
leaq -0x58(%rbp), %r13
leaq -0x108(%rbp), %r15
movq %r14, -0xa8(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
leaq -0x38(%rbp), %r12
je 0xab36
movq %rdx, -0xa8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x98(%rbp)
jmp 0xab3d
callq 0xbfde
movslq %eax, %rsi
leaq -0x88(%rbp), %rbx
movq %rbx, %rdi
callq 0x1378a
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xaa2d(%rip), %rcx      # 0x153fd
callq 0x4440
leaq -0x38(%rbp), %r12
movq %r12, -0x48(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
leaq -0x78(%rbp), %rbx
leaq -0x98(%rbp), %r14
leaq -0x58(%rbp), %r13
je 0xaa22
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xaa2a
movq -0xb8(%rbp), %r13
movq -0xb0(%rbp), %r12
movq -0xc8(%rbp), %r15
jmp 0xadf8
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, -0x40(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0x48(%rbp), %rdi
leaq 0xa9c0(%rip), %rsi      # 0x1540c
callq 0x4760
movq %r13, -0x68(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
movq %r15, -0xc0(%rbp)
je 0xaa78
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
jmp 0xaa80
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, -0x60(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, -0xa8(%rbp)
leaq -0xa8(%rbp), %r15
movq %r15, %rdi
leaq 0xac52(%rip), %rdx      # 0x15701
movq %rdx, %rsi
callq 0x5790
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x13ed0
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xaade
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
movq -0xb8(%rbp), %r13
je 0xaafa
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
movq -0xc8(%rbp), %r15
je 0xab16
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
movq -0xb0(%rbp), %r12
je 0xadd3
movq -0x78(%rbp), %rsi
jmp 0xadcb
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
movq %rdx, -0xa0(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xa8(%rbp), %rdi
leaq 0xa8e8(%rip), %rsi      # 0x1544d
callq 0x4760
movq %rbx, -0x88(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xab90
movq %rdx, -0x88(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x78(%rbp)
jmp 0xab96
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, -0x80(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0xc0(%rbp), %rdi
callq 0xbff0
movq %rax, %rdi
callq 0xe7f8
movslq %eax, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1378a
movq -0x88(%rbp), %rcx
movl $0xf, %esi
cmpq %rbx, %rcx
je 0xabe5
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %r8
movq -0xe0(%rbp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
jbe 0xac1a
movl $0xf, %esi
leaq -0xd8(%rbp), %rdi
cmpq %rdi, -0xe8(%rbp)
je 0xac15
movq -0xd8(%rbp), %rsi
cmpq %rsi, %rax
jbe 0xac2f
movq -0xe8(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x4150
jmp 0xac3f
leaq -0xe8(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
movq %r12, -0x48(%rbp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0xac5c
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xac64
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, -0x40(%rbp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq -0x48(%rbp), %rdi
leaq 0xa7da(%rip), %rsi      # 0x15465
callq 0x4760
movq %r13, -0x68(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xacb0
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
jmp 0xacb8
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
movq %rdx, -0x60(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xf8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq %r15, %rdi
leaq 0xaa1a(%rip), %rdx      # 0x15701
movq %rdx, %rsi
callq 0x5790
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x13ed0
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0xad1d
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0xad32
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
movq -0xc8(%rbp), %r15
je 0xad4e
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
movq -0xb0(%rbp), %r12
je 0xad77
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
je 0xad8f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xadaa
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
movq -0xb8(%rbp), %r13
je 0xadd3
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
movq 0x8(%r15), %rbx
movq -0xc0(%rbp), %rdi
callq 0xbfde
movl %eax, %edi
callq 0x4780
movq %r12, %rdi
movq %r15, %rsi
callq 0xbac2
movq %rbx, %r15
decl 0x130(%r13)
movq (%r15), %r15
cmpq %r12, %r15
jne 0xa870
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xae7a
movq %rax, %rbx
jmp 0xae93
movq %rax, %rbx
jmp 0xaf4f
movq %rax, %rbx
jmp 0xaf71
movq %rax, %rbx
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0xae61
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xae61
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xae7a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xae93
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xaf33
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf33
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xaede
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaede
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xaefc
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaefc
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xaf1a
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf1a
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xaf93
movq -0x78(%rbp), %rsi
jmp 0xaf8b
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xaf4f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xaf71
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
je 0xaf93
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0
nop