// File: 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
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
addq $0x18, %rdi
callq 0x14660
movq %rax, -0x10(%rbp)
movq -0x1b8(%rbp), %rdi
addq $0x18, %rdi
callq 0x146c0
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x14690
testb $0x1, %al
jne 0x13341
jmp 0x138dd
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x168f0
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x1b8(%rbp), %rax
movslq %ecx, %rcx
movq 0xb0(%rax,%rcx,8), %rax
movq %rax, -0x1c0(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x168f0
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1c0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x138cd
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x16920
movq %rax, %rdi
callq 0x1ac60
movq %rax, -0x20(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x168f0
movl %eax, -0x1cc(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5240
movq %rax, -0x1e0(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x16940
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x1c8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x53b0
movq %rax, -0x1d8(%rbp)
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x16940
movq -0x1d8(%rbp), %rdx
movl -0x1cc(%rbp), %edi
movq -0x1c8(%rbp), %rsi
subq %rax, %rdx
xorl %ecx, %ecx
callq 0x52a0
movq %rax, -0x28(%rbp)
cmpq $-0x1, -0x28(%rbp)
je 0x13477
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
movq -0x28(%rbp), %rsi
callq 0x16d90
cmpq $-0x1, -0x28(%rbp)
je 0x134b2
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x16940
movq %rax, -0x1e8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x53b0
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
cmpq %rcx, %rax
jne 0x138b7
cmpq $-0x1, -0x28(%rbp)
jne 0x13618
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x168f0
movslq %eax, %rsi
leaq -0x88(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x20c10
movq -0x1f0(%rbp), %rdx
leaq 0xff16(%rip), %rsi      # 0x23408
leaq -0x68(%rbp), %rdi
callq 0x6700
jmp 0x134fd
leaq 0xff13(%rip), %rdx      # 0x23417
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x66b0
jmp 0x13513
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x58f0
movq -0x1f8(%rbp), %rdx
leaq 0x101d8(%rip), %rsi     # 0x2370c
leaq -0xb8(%rbp), %rdi
callq 0x63e0
jmp 0x13542
leaq -0x48(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x21750
jmp 0x13554
leaq -0xb8(%rbp), %rdi
callq 0x5a08
leaq -0xb9(%rbp), %rdi
callq 0x55c0
leaq -0x48(%rbp), %rdi
callq 0x5a08
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
jmp 0x13892
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x13607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x135fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x135e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x5a08
leaq -0xb9(%rbp), %rdi
callq 0x55c0
leaq -0x48(%rbp), %rdi
callq 0x5a08
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
jmp 0x138e6
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x168f0
movslq %eax, %rsi
leaq -0x160(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x20c10
movq -0x200(%rbp), %rdx
leaq 0xfdfc(%rip), %rsi      # 0x23449
leaq -0x140(%rbp), %rdi
callq 0x6700
jmp 0x1365b
leaq 0xfdf6(%rip), %rdx      # 0x23458
leaq -0x120(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x66b0
jmp 0x13677
leaq -0x10(%rbp), %rdi
callq 0x146f0
movq %rax, %rdi
callq 0x16920
movq %rax, -0x208(%rbp)
jmp 0x13691
movq -0x208(%rbp), %rdi
callq 0x1ac40
movl %eax, -0x20c(%rbp)
jmp 0x136a5
movl -0x20c(%rbp), %eax
movslq %eax, %rsi
leaq -0x180(%rbp), %rdi
callq 0x20c10
jmp 0x136bc
leaq -0x100(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x14710
jmp 0x136d8
leaq 0xfd91(%rip), %rdx      # 0x23470
leaq -0xe0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x66b0
jmp 0x136f4
leaq -0x1a1(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x58f0
movq -0x218(%rbp), %rdx
leaq 0xfff7(%rip), %rsi      # 0x2370c
leaq -0x1a0(%rbp), %rdi
callq 0x63e0
jmp 0x13723
leaq -0xe0(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x21750
jmp 0x13738
leaq -0x1a0(%rbp), %rdi
callq 0x5a08
leaq -0x1a1(%rbp), %rdi
callq 0x55c0
leaq -0xe0(%rbp), %rdi
callq 0x5a08
leaq -0x100(%rbp), %rdi
callq 0x5a08
leaq -0x180(%rbp), %rdi
callq 0x5a08
leaq -0x120(%rbp), %rdi
callq 0x5a08
leaq -0x140(%rbp), %rdi
callq 0x5a08
leaq -0x160(%rbp), %rdi
callq 0x5a08
jmp 0x13892
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x13884
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x13878
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x1386c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x13860
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x13854
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
jmp 0x1383c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x90(%rbp)
movl %eax, -0x94(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x5a08
leaq -0x1a1(%rbp), %rdi
callq 0x55c0
leaq -0xe0(%rbp), %rdi
callq 0x5a08
leaq -0x100(%rbp), %rdi
callq 0x5a08
leaq -0x180(%rbp), %rdi
callq 0x5a08
leaq -0x120(%rbp), %rdi
callq 0x5a08
leaq -0x140(%rbp), %rdi
callq 0x5a08
leaq -0x160(%rbp), %rdi
callq 0x5a08
jmp 0x138e6
leaq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x147e0
movq -0x1b8(%rbp), %rdi
movq %rax, -0x1b0(%rbp)
leaq -0x1b0(%rbp), %rsi
callq 0x14110
movq -0x1b8(%rbp), %rax
movl 0x130(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x130(%rax)
jmp 0x138cf
leaq -0x10(%rbp), %rdi
callq 0x14810
jmp 0x13317
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0x58e0
nopw %cs:(%rax,%rax)