// File: /workspace/github_cpp_makefile_star2_O1/alibakhtiar[P]ExampleHTTPServer/./HttpServer.hh
// Function: SimpleHttpServer::createServer(SimpleHttpServer::Server*)
bool createServer(Server *server)
{
	int st;
	int serverFd;

	// Create new TCP socket(int family, int type, int protocol)
#ifdef __linux__
	serverFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#else
	WSADATA WSAData;
	WSAStartup(MAKEWORD(2,0), &WSAData);
	serverFd = socket(AF_INET, SOCK_STREAM, 0);
#endif

	if (serverFd < 0) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return false;
	}

	// Socket options
#ifdef __linux__
	int optVal = 1;

	// Re use address
	st = setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, (void *)&optVal, sizeof(int));
	if (st == -1) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return false;
	}

	// No-delay
	st = setsockopt(serverFd, IPPROTO_TCP, TCP_NODELAY, (void *)&optVal, sizeof(int));
	if (st == -1) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return false;
	}
#else
	bool bOptVal = true;

	// No-delay
	int iResult = setsockopt(serverFd, SOL_SOCKET, SO_REUSEADDR, (char *)&bOptVal, sizeof(bool));
	if (iResult == SOCKET_ERROR) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return 1;
	}
#endif

	// Bind
	struct sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(struct sockaddr_in));

	sockAddr.sin_family = AF_INET;
	sockAddr.sin_addr.s_addr = inet_addr((char *)server->ip.c_str());
	sockAddr.sin_port = htons(server->port);

	st = bind(serverFd, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in));
	if (st == -1) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return false;
	}

	// Listen
	st = listen(serverFd, BACKLOG_SIZE);
	if (st < 0) {
		APPERROR(_STRERR, _FUNC, _LINE);
		return false;
	}

	// Run (main loop)
	int clientFd;
	struct sockaddr_in clientAddr;
	socklen_t clientLength;

	clientLength = (socklen_t)sizeof(struct sockaddr_in);
	memset(&clientAddr, 0, sizeof(struct sockaddr_in));

	std::cout << "http://" << 
		server->ip << ":" << server->port << std::endl;

	while (1) {
		// Accept new connection (new socket)
		clientFd = accept(serverFd, (struct sockaddr *)&clientAddr, &clientLength);
		if (clientFd <= 0) {
			continue;
		}

		// New thread per connection
#ifdef __linux__
		std::thread(serverRequestHandler, server, std::move(clientFd), std::move(clientAddr)).detach();
#else
		serverRequestHandler(server, clientFd, clientAddr);
#endif
	}

	close(serverFd);

#ifndef __linux__
	WSACleanup();
#endif

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, (%rsp)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2080
testl %eax, %eax
jns 0x2a52
callq 0x23d6
jmp 0x2b19
movl %eax, %ebx
leaq 0xc(%rsp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x22d0
cmpl $-0x1, %eax
jne 0x2a85
callq 0x239c
jmp 0x2b19
leaq 0xc(%rsp), %rcx
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x22d0
cmpl $-0x1, %eax
jne 0x2aad
callq 0x2362
jmp 0x2b19
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r14
movaps %xmm0, (%r14)
movw $0x2, (%r14)
movq (%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x21c0
movl %eax, 0x4(%r14)
movq (%rsp), %rax
movzwl 0x28(%rax), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movl %ebx, %edi
movq %r14, %rsi
movl $0x10, %edx
callq 0x2060
movl $0x208, %edi            # imm = 0x208
cmpl $-0x1, %eax
je 0x2b0f
movl %ebx, %edi
movl $0x96, %esi
callq 0x21f0
movl $0x20f, %edi            # imm = 0x20F
testl %eax, %eax
jns 0x2b2a
leaq 0x20(%rsp), %rsi
callq 0x2320
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c(%rsp), %r14
movl $0x10, (%r14)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq 0x547f(%rip), %r12      # 0x7fc8
leaq 0x252a(%rip), %rsi      # 0x507a
movl $0x7, %edx
movq %r12, %rdi
callq 0x2200
movq %rsp, %r13
movq (%r13), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r12, %rdi
callq 0x2200
movq %rax, %r12
leaq 0x2504(%rip), %rsi      # 0x5082
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movq (%r13), %rax
movl 0x28(%rax), %esi
movq %r12, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x2110
leaq 0x5a(%rip), %r13        # 0x2c03
leaq 0x10(%rsp), %rbp
leaq 0x8(%rsp), %r12
movl %ebx, %edi
movq %r15, %rsi
movq %r14, %rdx
callq 0x20c0
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x2bb3
movq %rbp, %rdi
movq %r13, %rsi
movq %rsp, %rdx
movq %r12, %rcx
movq %r15, %r8
callq 0x3634
movq %rbp, %rdi
callq 0x22b0
cmpq $0x0, 0x10(%rsp)
je 0x2bb3
jmp 0x2bfe
cmpq $0x0, 0x10(%rsp)
jne 0x2bfe
movq %rax, %rdi
callq 0x22c0
callq 0x2090