// File: llefranc[P]42_webserv/srcs/main.cpp
// Function: main
int main(int ac, char **av)
{
	if (ac != 2)
	{
		printLog(" >> Fatal error: wrong arguments\n");
		exit(EXIT_FAILURE);
	}
	
	// ** CONFIRURATION FILE PARSER PART **
	FileParser conf(av[1]);
	std::map<int, std::vector<ServerInfo> > m_srv;

	try{
		m_srv = conf.getConfigFile();
	}
	catch (std::exception &errmsg){
		std::cout << errmsg.what();
		exit(EXIT_FAILURE);
	}

	// ** SERVER PART **
	// boucle pour add tous les sockets >> ou les mettre dans la boucle en dessous ?
	std::vector<int> sockArray;
	for (std::map<int, std::vector<ServerInfo> >::iterator it = m_srv.begin(); it != m_srv.end(); it++)
		sockArray.push_back(it->first);

	HttpServer server;

	// Creating sockets and binding them to a port
	try
	{
		for (size_t i = 0; i < sockArray.size(); ++i)
			server.addServerSocket(ServerSocket(sockArray[i], 5));
			
		server.etablishConnection(m_srv);
	}
	catch (std::exception& e)
	{
		printLog(" >> Exception occured " + std::string(e.what()) + "\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5e0, %rsp            # imm = 0x5E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x5f53
leaq -0x31(%rbp), %rdi
movq %rdi, -0x5a0(%rbp)
callq 0x58f0
movq -0x5a0(%rbp), %rdx
leaq 0x1d173(%rip), %rsi     # 0x23004
leaq -0x30(%rbp), %rdi
callq 0x63e0
jmp 0x5e9c
leaq -0x69(%rbp), %rdi
movq %rdi, -0x5a8(%rbp)
callq 0x58f0
movq -0x5a8(%rbp), %rdx
leaq 0x1d852(%rip), %rsi     # 0x2370c
leaq -0x68(%rbp), %rdi
callq 0x63e0
jmp 0x5ec5
leaq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x21750
jmp 0x5ed4
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x69(%rbp), %rdi
callq 0x55c0
leaq -0x30(%rbp), %rdi
callq 0x5a08
leaq -0x31(%rbp), %rdi
callq 0x55c0
movl $0x1, %edi
callq 0x5660
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5f45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x5f33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x69(%rbp), %rdi
callq 0x55c0
leaq -0x30(%rbp), %rdi
callq 0x5a08
leaq -0x31(%rbp), %rdi
callq 0x55c0
jmp 0x63c9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x348(%rbp), %rdi
movq %rdi, -0x5b8(%rbp)
callq 0xb020
leaq -0x378(%rbp), %rdi
callq 0x6490
movq -0x5b8(%rbp), %rdi
callq 0xb650
movq %rax, -0x5b0(%rbp)
jmp 0x5f8f
movq -0x5b0(%rbp), %rsi
leaq -0x378(%rbp), %rdi
callq 0x64b0
jmp 0x5fa4
jmp 0x6015
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x63b1
movq -0x40(%rbp), %rdi
callq 0x5230
movq %rax, -0x380(%rbp)
movq -0x380(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, %rsi
movq 0x2afe4(%rip), %rdi     # 0x30fd0
callq 0x5540
jmp 0x5ff3
movl $0x1, %edi
callq 0x5660
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
callq 0x5860
jmp 0x6010
jmp 0x63b1
leaq -0x398(%rbp), %rdi
callq 0x64f0
leaq -0x378(%rbp), %rdi
callq 0x6510
movq %rax, -0x3a0(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x6570
movq %rax, -0x3a8(%rbp)
leaq -0x3a0(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x6540
testb $0x1, %al
jne 0x6060
jmp 0x60a7
leaq -0x3a0(%rbp), %rdi
callq 0x6610
movq %rax, %rsi
leaq -0x398(%rbp), %rdi
callq 0x65a0
jmp 0x607d
jmp 0x607f
leaq -0x3a0(%rbp), %rdi
xorl %esi, %esi
callq 0x6630
movq %rax, -0x3b0(%rbp)
jmp 0x6034
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x63a5
leaq -0x4e8(%rbp), %rdi
callq 0x12dd0
jmp 0x60b5
movq $0x0, -0x4f0(%rbp)
movq -0x4f0(%rbp), %rax
movq %rax, -0x5c0(%rbp)
leaq -0x398(%rbp), %rdi
callq 0x6670
movq %rax, %rcx
movq -0x5c0(%rbp), %rax
cmpq %rcx, %rax
jae 0x62a7
movq -0x4f0(%rbp), %rsi
leaq -0x398(%rbp), %rdi
callq 0x6690
movl (%rax), %esi
leaq -0x4fc(%rbp), %rdi
movl $0x5, %edx
callq 0x16260
jmp 0x6115
leaq -0x4e8(%rbp), %rdi
leaq -0x4fc(%rbp), %rsi
callq 0x13000
jmp 0x612a
leaq -0x4fc(%rbp), %rdi
callq 0x162c0
movq -0x4f0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x4f0(%rbp)
jmp 0x60c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6173
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x4fc(%rbp), %rdi
callq 0x162c0
movl -0x44(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x6399
movq -0x40(%rbp), %rdi
callq 0x5230
movq %rax, -0x508(%rbp)
movq -0x508(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, -0x5d0(%rbp)
leaq -0x569(%rbp), %rdi
movq %rdi, -0x5c8(%rbp)
callq 0x58f0
movq -0x5d0(%rbp), %rsi
movq -0x5c8(%rbp), %rdx
leaq -0x568(%rbp), %rdi
callq 0x63e0
jmp 0x61d9
leaq 0x1ce46(%rip), %rsi     # 0x23026
leaq -0x548(%rbp), %rdi
leaq -0x568(%rbp), %rdx
callq 0x6700
jmp 0x61f5
leaq 0x1d1ce(%rip), %rdx     # 0x233ca
leaq -0x528(%rbp), %rdi
leaq -0x548(%rbp), %rsi
callq 0x66b0
jmp 0x6211
leaq -0x591(%rbp), %rdi
movq %rdi, -0x5d8(%rbp)
callq 0x58f0
movq -0x5d8(%rbp), %rdx
leaq 0x1d4da(%rip), %rsi     # 0x2370c
leaq -0x590(%rbp), %rdi
callq 0x63e0
jmp 0x6240
leaq -0x528(%rbp), %rdi
leaq -0x590(%rbp), %rsi
callq 0x21750
jmp 0x6255
leaq -0x590(%rbp), %rdi
callq 0x5a08
leaq -0x591(%rbp), %rdi
callq 0x55c0
leaq -0x528(%rbp), %rdi
callq 0x5a08
leaq -0x548(%rbp), %rdi
callq 0x5a08
leaq -0x568(%rbp), %rdi
callq 0x5a08
leaq -0x569(%rbp), %rdi
callq 0x55c0
movl $0x1, %edi
callq 0x5660
leaq -0x4e8(%rbp), %rdi
leaq -0x378(%rbp), %rsi
callq 0x13110
jmp 0x62bc
jmp 0x6356
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6341
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6335
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6329
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x6311
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x590(%rbp), %rdi
callq 0x5a08
leaq -0x591(%rbp), %rdi
callq 0x55c0
leaq -0x528(%rbp), %rdi
callq 0x5a08
leaq -0x548(%rbp), %rdi
callq 0x5a08
leaq -0x568(%rbp), %rdi
callq 0x5a08
leaq -0x569(%rbp), %rdi
callq 0x55c0
callq 0x5860
jmp 0x6354
jmp 0x6399
movl $0x0, -0x4(%rbp)
leaq -0x4e8(%rbp), %rdi
callq 0x12e40
leaq -0x398(%rbp), %rdi
callq 0x6750
leaq -0x378(%rbp), %rdi
callq 0x67b0
leaq -0x348(%rbp), %rdi
callq 0xb5a0
movl -0x4(%rbp), %eax
addq $0x5e0, %rsp            # imm = 0x5E0
popq %rbp
retq
leaq -0x4e8(%rbp), %rdi
callq 0x12e40
leaq -0x398(%rbp), %rdi
callq 0x6750
leaq -0x378(%rbp), %rdi
callq 0x67b0
leaq -0x348(%rbp), %rdi
callq 0xb5a0
movq -0x40(%rbp), %rdi
callq 0x58e0
movq %rax, %rdi
callq 0x64e0
nopw (%rax,%rax)