// File: /workspace/github_cpp_makefile_star2_O3/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 0xae47
movq %rdi, %r13
movq %rdi, -0xb8(%rbp)
movq %r12, -0xb0(%rbp)
leaq 0x10(%r15), %r14
movq %r14, %rdi
callq 0xc01e
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 0xc01e
btq %rax, %rbx
jae 0xae3b
movq %r15, -0xc8(%rbp)
movq %r14, %rdi
callq 0xc030
movq %rax, %rdi
callq 0xe7ce
movq %rax, %r13
movq %r14, %rdi
callq 0xc01e
movl %eax, %r15d
movq (%r13), %rbx
movq %r14, %rdi
callq 0xc03e
addq %rax, %rbx
movq 0x8(%r13), %r12
movq %r14, %rdi
callq 0xc03e
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 0xa9e5
movq %rax, %rsi
callq 0xc51a
movq %r15, %rdi
callq 0xc03e
cmpq 0x8(%r13), %rax
leaq -0x98(%rbp), %r14
jne 0xaa48
movq %r15, -0xc0(%rbp)
movq %r15, %rdi
callq 0xc01e
movslq %eax, %rsi
leaq -0x128(%rbp), %rbx
movq %rbx, %rdi
callq 0x136be
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xaaa1(%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 0xab76
movq %rdx, -0xa8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x98(%rbp)
jmp 0xab7d
callq 0xc01e
movslq %eax, %rsi
leaq -0x88(%rbp), %rbx
movq %rbx, %rdi
callq 0x136be
movl $0xe, %r8d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
leaq 0xa9ed(%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 0xaa62
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xaa6a
movq -0xb8(%rbp), %r13
movq -0xb0(%rbp), %r12
movq -0xc8(%rbp), %r15
jmp 0xae34
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 0xa980(%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 0xaab8
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
jmp 0xaac0
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 0xac12(%rip), %rdx      # 0x15701
movq %rdx, %rsi
callq 0x5796
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x13da7
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xab1e
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
movq -0xb8(%rbp), %r13
je 0xab3a
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
movq -0xc8(%rbp), %r15
je 0xab56
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
movq -0xb0(%rbp), %r12
je 0xae0f
movq -0x78(%rbp), %rsi
jmp 0xae07
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 0xa8a8(%rip), %rsi      # 0x1544d
callq 0x4760
movq %rbx, -0x88(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xabd0
movq %rdx, -0x88(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x78(%rbp)
jmp 0xabd6
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 0xc030
movq %rax, %rdi
callq 0xe7c4
movslq %eax, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x136be
movq -0x88(%rbp), %rcx
movl $0xf, %edi
cmpq %rbx, %rcx
je 0xac25
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %r8
movq -0xe8(%rbp), %rsi
movq -0xe0(%rbp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
jbe 0xac5d
movl $0xf, %edi
leaq -0xd8(%rbp), %r9
cmpq %r9, %rsi
je 0xac58
movq -0xd8(%rbp), %rdi
cmpq %rdi, %rax
jbe 0xac6b
leaq -0x88(%rbp), %rdi
callq 0x4150
jmp 0xac7b
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 0xac98
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0xaca0
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 0xa79e(%rip), %rsi      # 0x15465
callq 0x4760
movq %r13, -0x68(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xacec
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
jmp 0xacf4
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 0xa9de(%rip), %rdx      # 0x15701
movq %rdx, %rsi
callq 0x5796
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
callq 0x13da7
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0xad59
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0xad6e
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
movq -0xc8(%rbp), %r15
je 0xad8a
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
movq -0xb0(%rbp), %r12
je 0xadb3
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x88(%rbp), %rdi
cmpq %rbx, %rdi
je 0xadcb
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xade6
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
movq -0xb8(%rbp), %r13
je 0xae0f
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
movq 0x8(%r15), %rbx
movq -0xc0(%rbp), %rdi
callq 0xc01e
movl %eax, %edi
callq 0x4780
movq %r12, %rdi
movq %r15, %rsi
callq 0xbafe
movq %rbx, %r15
decl 0x130(%r13)
movq (%r15), %r15
cmpq %r12, %r15
jne 0xa8b0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xaeb6
movq %rax, %rbx
jmp 0xaecf
movq %rax, %rbx
jmp 0xaf8b
movq %rax, %rbx
jmp 0xafad
movq %rax, %rbx
movq -0x108(%rbp), %rdi
leaq -0xf8(%rbp), %rax
cmpq %rax, %rdi
je 0xae9d
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xae9d
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xaeb6
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xaecf
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xaf6f
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf6f
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r14, %rdi
je 0xaf1a
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf1a
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xaf38
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf38
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xaf56
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xaf56
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xafcf
movq -0x78(%rbp), %rsi
jmp 0xafc7
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xaf8b
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xafad
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x128(%rbp), %rdi
leaq -0x118(%rbp), %rax
cmpq %rax, %rdi
je 0xafcf
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0
nop