// File: alibakhtiar[P]ExampleHTTPServer/./HttpServer.hh
// Function: SimpleHttpServer::serverRequestHandler(SimpleHttpServer::Server*, int, sockaddr_in)
static void serverRequestHandler(Server *server, int sock, struct sockaddr_in clientAddr)
{
	bool rc;
	Request *req;
	Response *res;
	char *buffer;
	ssize_t recvLen;

	// New request
	req = new Request;
	req->sock = sock;
	req->addr = clientAddr;

	// New response
	res = new Response;
	res->sock = sock;
	res->req  = req;

	// Recv buffer
	buffer = new char[1+RECV_BUFFER_SIZE];

	while (1) {
		memset(buffer, '\0', 1+RECV_BUFFER_SIZE);

		recvLen = recv(sock, buffer, RECV_BUFFER_SIZE, 0);
		if (recvLen == 0) {
			break;
		}
		else if (recvLen < 0) {
			APPERROR(_STRERR, _FUNC, _LINE);
			break;
		}

		// Request parser
		rc = httpRequestParse(req, buffer, recvLen);
		if (rc == false)
			break;

		if (req->hasError == true) {
			res->statusCode = 400;
			res->errorPage();
			res->send();
			break;
		}

		if (server->onRequestCallback != NULL)
			server->onRequestCallback(req, res);

		break;
	}

	close(sock);

	delete[] buffer;
	buffer = NULL;

	delete req;
	req = NULL;

	delete res;
	res = NULL;

	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdx, -0x10(%rbp)
movq %rcx, -0x8(%rbp)
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movl $0xb8, %edi
callq 0x32a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x58(%rbp)
callq 0x51a0
jmp 0x3f55
movq -0x58(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movups -0x10(%rbp), %xmm0
movups %xmm0, 0xc(%rax)
movl $0x70, %edi
callq 0x32a0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
callq 0x52f0
jmp 0x3f8e
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x801, %edi            # imm = 0x801
callq 0x3030
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movl $0x801, %edx            # imm = 0x801
callq 0x3190
movl -0x1c(%rbp), %edi
movq -0x38(%rbp), %rsi
movl $0x800, %edx            # imm = 0x800
xorl %ecx, %ecx
callq 0x30f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x402b
jmp 0x40fa
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0xb8, %esi
callq 0x32c0
jmp 0x416a
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
movl $0x70, %esi
callq 0x32c0
jmp 0x416a
cmpq $0x0, -0x40(%rbp)
jge 0x4073
movq 0x9f97(%rip), %rax      # 0xdfd0
movq (%rax), %rax
movq %rax, -0x68(%rbp)
callq 0x3060
movl (%rax), %edi
callq 0x3100
movq -0x68(%rbp), %rdi
movq %rax, %rdx
leaq 0x4fe7(%rip), %rsi      # 0x9041
leaq 0x5161(%rip), %rcx      # 0x91c2
movl $0x1a7, %r8d            # imm = 0x1A7
movb $0x0, %al
callq 0x33c0
jmp 0x40fa
jmp 0x4075
movq -0x28(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x35f0
andb $0x1, %al
movb %al, -0x1d(%rbp)
movb -0x1d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x409a
jmp 0x40fa
movq -0x28(%rbp), %rax
movb 0x1c(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x40cc
movq -0x30(%rbp), %rax
movl $0x190, 0x18(%rax)      # imm = 0x190
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x30(%rbp), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x40fa
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5390
testb $0x1, %al
jne 0x40e3
jmp 0x40f8
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x53c0
jmp 0x40fa
movl -0x1c(%rbp), %edi
callq 0x3480
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
je 0x4119
movq -0x70(%rbp), %rdi
callq 0x3380
movq $0x0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x4139
movq -0x78(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x4159
movq -0x80(%rbp), %rdi
movq (%rdi), %rax
callq *0x28(%rax)
movq $0x0, -0x30(%rbp)
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)