// File: /workspace/github_cpp_makefile_star2_O3/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, %r14
movq %rdi, 0x18(%rsp)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2080
testl %eax, %eax
jns 0x2a6b
callq 0x23d6
jmp 0x2b2c
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 0x2a9e
callq 0x239c
jmp 0x2b2c
leaq 0xc(%rsp), %rcx
movl %ebx, %edi
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x22d0
cmpl $-0x1, %eax
jne 0x2ac6
callq 0x2362
jmp 0x2b2c
leaq 0x20(%rsp), %r15
movq $0x0, 0x8(%r15)
movw $0x2, (%r15)
movq 0x8(%r14), %rdi
callq 0x21c0
movl %eax, 0x4(%r15)
movzwl 0x28(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r15)
movl %ebx, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x2060
movl $0x208, %edi            # imm = 0x208
cmpl $-0x1, %eax
je 0x2b22
movl %ebx, %edi
movl $0x96, %esi
callq 0x21f0
movl $0x20f, %edi            # imm = 0x20F
testl %eax, %eax
jns 0x2b3d
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 0x4(%rsp), %rax
movl $0x10, (%rax)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r12
movaps %xmm0, (%r12)
movq 0x546c(%rip), %r15      # 0x7fc8
leaq 0x2517(%rip), %rsi      # 0x507a
movl $0x7, %edx
movq %r15, %rdi
callq 0x2200
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
leaq 0x24f8(%rip), %rsi      # 0x5082
movl $0x1, %edx
movq %rax, %rdi
callq 0x2200
movl 0x28(%r14), %esi
movq %r15, %rdi
callq 0x22a0
movq %rax, %rdi
callq 0x2110
leaq 0x61(%rip), %r14        # 0x2c13
leaq 0x10(%rsp), %r13
leaq 0x18(%rsp), %rbp
leaq 0x8(%rsp), %r15
movl %ebx, %edi
movq %r12, %rsi
leaq 0x4(%rsp), %rdx
callq 0x20c0
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x2bc1
movq %r13, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r15, %rcx
movq %r12, %r8
callq 0x3644
movq %r13, %rdi
callq 0x22b0
cmpq $0x0, 0x10(%rsp)
je 0x2bc1
jmp 0x2c0e
cmpq $0x0, 0x10(%rsp)
jne 0x2c0e
movq %rax, %rdi
callq 0x22c0
callq 0x2090