// File: llefranc[P]42_webserv/srcs/server/ServerSocket.cpp
// Function: ServerSocket::createSocket()
void ServerSocket::createSocket()
{
	// Creating a TCP socket
	if ((_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		throw std::runtime_error("Fatal error: socket function failed\n");

	// Setting its options
	int yes = true;
	if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
		throw std::runtime_error("Fatal error: setsockopt function failed\n");
	
	// Initializing structure for socket information (IP, port..)
	struct sockaddr_in socketAddr;
	bzero((char *) &socketAddr, sizeof(socketAddr));
	socketAddr.sin_family = AF_INET;
	socketAddr.sin_addr.s_addr = INADDR_ANY;
	socketAddr.sin_port = htons(_port);
	
	// Binding the socket to a port
	if (bind(_fd, (struct sockaddr *) &socketAddr, sizeof(socketAddr)) < 0)
		throw std::runtime_error("Fatal error: bind function failed\n");

	listen(_fd, _maxClient);
	printLog(" >> PORT " + convertNbToString(_port) + ": HTTP socket created\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x5160
movq -0xb8(%rbp), %rcx
movl %eax, 0x8(%rcx)
cmpl $0x0, %eax
jge 0x16426
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc0(%rbp)
leaq 0xd14f(%rip), %rsi      # 0x23537
callq 0x5210
jmp 0x163ef
movq -0xc0(%rbp), %rdi
movq 0x1abf3(%rip), %rsi     # 0x30ff0
movq 0x1abc4(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
callq 0x53f0
jmp 0x166a9
movq -0xb8(%rbp), %rax
movl $0x1, -0x18(%rbp)
movl 0x8(%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x18(%rbp), %rcx
movl $0x4, %r8d
callq 0x5900
cmpl $-0x1, %eax
jne 0x164b1
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc8(%rbp)
leaq 0xd0e9(%rip), %rsi      # 0x2355c
callq 0x5210
jmp 0x1647a
movq -0xc8(%rbp), %rdi
movq 0x1ab68(%rip), %rsi     # 0x30ff0
movq 0x1ab39(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
callq 0x53f0
jmp 0x166a9
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x52e0
movq -0xb8(%rbp), %rax
movw $0x2, -0x28(%rbp)
movl $0x0, -0x24(%rbp)
movl (%rax), %eax
movzwl %ax, %edi
callq 0x5070
movw %ax, %cx
movq -0xb8(%rbp), %rax
movw %cx, -0x26(%rbp)
movl 0x8(%rax), %edi
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x50c0
cmpl $0x0, %eax
jge 0x1655f
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0xd064(%rip), %rsi      # 0x23585
callq 0x5210
jmp 0x16528
movq -0xd0(%rbp), %rdi
movq 0x1aaba(%rip), %rsi     # 0x30ff0
movq 0x1aa8b(%rip), %rdx     # 0x30fc8
callq 0x58a0
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
callq 0x53f0
jmp 0x166a9
movq -0xb8(%rbp), %rax
movl 0x4(%rax), %esi
movl 0x8(%rax), %edi
callq 0x5600
movq -0xb8(%rbp), %rax
movslq (%rax), %rsi
leaq -0x88(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x20c10
movq -0xd8(%rbp), %rdx
leaq 0xd00c(%rip), %rsi      # 0x235a8
leaq -0x68(%rbp), %rdi
callq 0x6700
jmp 0x165a7
leaq 0xd004(%rip), %rdx      # 0x235b2
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x66b0
jmp 0x165bd
leaq -0xa9(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x58f0
movq -0xe0(%rbp), %rdx
leaq 0xd12e(%rip), %rsi      # 0x2370c
leaq -0xa8(%rbp), %rdi
callq 0x63e0
jmp 0x165ec
leaq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x21750
jmp 0x165fe
leaq -0xa8(%rbp), %rdi
callq 0x5a08
leaq -0xa9(%rbp), %rdi
callq 0x55c0
leaq -0x48(%rbp), %rdi
callq 0x5a08
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x1669d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x16694
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
jmp 0x1667f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x10(%rbp)
movl %eax, -0x14(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5a08
leaq -0xa9(%rbp), %rdi
callq 0x55c0
leaq -0x48(%rbp), %rdi
callq 0x5a08
leaq -0x68(%rbp), %rdi
callq 0x5a08
leaq -0x88(%rbp), %rdi
callq 0x5a08
movq -0x10(%rbp), %rdi
callq 0x58e0
nopw %cs:(%rax,%rax)
nopl (%rax)