// File: llefranc[P]42_webserv/srcs/server/HttpServer.cpp
// Function: HttpServer::connectNewClients(std::map<int, std::vector<ServerInfo, std::allocator<ServerInfo>>, std::less<int>, std::allocator<std::pair<int const, std::vector<ServerInfo, std::allocator<ServerInfo>>>>>&)
void HttpServer::connectNewClients(std::map<int, std::vector<ServerInfo> >& mSrv)
{
	for (std::list<ServerSocket>::iterator it = _serverSocks.begin(); it != _serverSocks.end() && _nbReadyFds; ++it)
	{
		if (FD_ISSET(it->getFd(), &_readFds))
		{
			// Not use but need to be set for accept function
			struct sockaddr_in addrCli;
			int lenCli = sizeof(addrCli);
			bzero((char *) &addrCli, sizeof(addrCli));

			// Creates a new socket for a client connection
			int newClient;
			if ((newClient = accept(it->getFd(), (struct sockaddr *)&addrCli, (socklen_t *)&lenCli)) < 0)
				throw std::runtime_error("Fatal error: accept function failed\n");

			// Setting the fd in non-blocking mode, then saving it
			fcntl(newClient, F_SETFL, O_NONBLOCK);
			addClientSocket(newClient, it->getPort(), mSrv);
			printLog("\033[1;36m >> FD " + convertNbToString(newClient) + ": Client socket created\033[0m\n");

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x14830
movq %rax, -0x18(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x14890
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x14860
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xd9(%rbp)
jne 0x13955
jmp 0x1396c
movq -0xd8(%rbp), %rax
cmpl $0x0, 0x130(%rax)
setne %al
movb %al, -0xd9(%rbp)
movb -0xd9(%rbp), %al
testb $0x1, %al
jne 0x1397b
jmp 0x13c26
leaq -0x18(%rbp), %rdi
callq 0x148c0
movq %rax, %rdi
callq 0x16360
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0xd8(%rbp), %rax
movslq %ecx, %rcx
movq 0x30(%rax,%rcx,8), %rax
movq %rax, -0xe8(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x148c0
movq %rax, %rdi
callq 0x16360
movl $0x40, %ecx
cltd
idivl %ecx
movq -0xe8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x13c16
movl $0x10, -0x34(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x52e0
leaq -0x18(%rbp), %rdi
callq 0x148c0
movq %rax, %rdi
callq 0x16360
movl %eax, %edi
leaq -0x30(%rbp), %rsi
leaq -0x34(%rbp), %rdx
callq 0x51c0
movl %eax, -0x38(%rbp)
cmpl $0x0, %eax
jge 0x13a81
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xf0(%rbp)
leaq 0xfaa3(%rip), %rsi      # 0x234e6
callq 0x5210
jmp 0x13a4a
movq -0xf0(%rbp), %rdi
movq 0x1d598(%rip), %rsi     # 0x30ff0
movq 0x1d569(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x53f0
jmp 0x13c2f
movl -0x38(%rbp), %edi
xorl %eax, %eax
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x5300
movl -0x38(%rbp), %eax
movl %eax, -0xfc(%rbp)
leaq -0x18(%rbp), %rdi
callq 0x148c0
movq %rax, %rdi
callq 0x16380
movq -0xd8(%rbp), %rdi
movl -0xfc(%rbp), %esi
movl %eax, %edx
movq -0x10(%rbp), %rcx
callq 0x13040
movslq -0x38(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x20c10
movq -0xf8(%rbp), %rdx
leaq 0xfa1f(%rip), %rsi      # 0x2350b
leaq -0x88(%rbp), %rdi
callq 0x6700
jmp 0x13afa
leaq 0xfa19(%rip), %rdx      # 0x2351a
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x66b0
jmp 0x13b13
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x58f0
movq -0x108(%rbp), %rdx
leaq 0xfbd8(%rip), %rsi      # 0x2370c
leaq -0xc8(%rbp), %rdi
callq 0x63e0
jmp 0x13b42
leaq -0x68(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x21750
jmp 0x13b54
leaq -0xc8(%rbp), %rdi
callq 0x5a08
leaq -0xc9(%rbp), %rdi
callq 0x55c0
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
leaq -0xa8(%rbp), %rdi
callq 0x5a08
movq -0xd8(%rbp), %rax
movl 0x130(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x130(%rax)
jmp 0x13c16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x13c08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x13bfc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x13be7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x5a08
leaq -0xc9(%rbp), %rdi
callq 0x55c0
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
leaq -0xa8(%rbp), %rdi
callq 0x5a08
jmp 0x13c2f
jmp 0x13c18
leaq -0x18(%rbp), %rdi
callq 0x148e0
jmp 0x13927
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x58e0
nopl (%rax,%rax)