// File: /workspace/github_cpp_makefile_star2_O2/m1c0l[P]http-client-server/web-server.cpp
// Function: thread_func(sockaddr_in, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
void thread_func(sockaddr_in clientAddr, string filedir, int clientSockfd) {

	//        socklen_t clientAddrSize = sizeof(clientAddr);
	char ipstr[INET_ADDRSTRLEN] = {'\0'};
	inet_ntop(clientAddr.sin_family, &clientAddr.sin_addr, ipstr, sizeof(ipstr));
	std::cout << "Accept a connection from: " << ipstr << ":" <<
		ntohs(clientAddr.sin_port) << std::endl;

	char buf[BUFFER_SIZE + 1] = {0};
	string httpTemp;
	int startInd =0;
	HttpRequest message;
	int status = 200;
	int bytesRead = 0;

	// read/write data from/into the connection
	while (true)
	{
		memset(buf, '\0', sizeof(buf));
		/*
		  if (recv(clientSockfd, buf, BUFFER_SIZE, 0) == -1) {
			perror("recv");
			return;
		}
		*/
		future<ssize_t> recvFut = async (launch::async, recv, clientSockfd, buf, BUFFER_SIZE, 0);
		if(waitFor(recvFut) == -1){
		  close(clientSockfd);
		  status = 408;
		  return;
		}
	
		bytesRead = recvFut.get();
		if (bytesRead == -1) {
			perror("recv");
			return;
		}

		httpTemp.append(buf, bytesRead);

		size_t x = httpTemp.find("\r\n\r\n", startInd);
		if(x == string::npos) {
			startInd += bytesRead;
		}
		else
		{
			httpTemp.resize(x+5);
			int decodeStatus = message.decode(httpTemp);
			if(decodeStatus && status ==200) {
				status = decodeStatus;
			}
			break;
		}
	}


	string url = message.getUrl();//need to decide when/how to process absolute url vs ppath

	// find the http requested file in the file directory
	string filepath = filedir + url;
	  

	// find body length
	off_t bodyLength = 0;
	struct stat st;
	if (stat(filepath.c_str(), &st) == 0) {
		bodyLength = st.st_size;
	}
	else {
		perror("stat");
		status = 404;
	}

	if (S_ISDIR(st.st_mode) && status == 200) {
	  perror("file type");
	  status = 404;
	}

	// prepare response header
	HttpResponse response;
	response.setVersion("HTTP/1.0");
	response.setStatus(to_string(status)); // or other error cases
	response.setDescription(codeToDescription(status));
	if (status == 200) {
		response.setHeader("Content-Length", to_string(bodyLength));
	}

	// send response header
	string header = response.encode();
	if (send(clientSockfd, header.c_str(), header.size(), 0) == -1) {
		perror("send");
	}

	// send requested file
	ifstream wantedFile;
	if(status != 404)
		wantedFile.open(filepath);
	
	if(!wantedFile) {
		perror("fstream::open");
		status = 404;
	}
	while (wantedFile && (status != 404)) {
		memset(buf, 0, BUFFER_SIZE);
		wantedFile.read(buf, BUFFER_SIZE);
		if (send(clientSockfd, buf, wantedFile.gcount(), 0) == -1) {
			perror("send");
			break;
		}
	}
	wantedFile.close();

	close(clientSockfd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x638, %rsp            # imm = 0x638
movq %rdx, 0x18(%rsp)
leaq 0x21c(%rsp), %rbx
movq %rdi, -0x4(%rbx)
movq %rsi, 0x4(%rbx)
leaq 0x14(%rsp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
leaq 0x160(%rsp), %r14
movaps %xmm0, (%r14)
movzwl %di, %edi
pushq $0x10
popq %rcx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x43b0
movq 0x845b(%rip), %rdi      # 0xdfb0
leaq 0x357e(%rip), %rsi      # 0x90da
callq 0x4350
movq %rax, %rdi
movq %r14, %rsi
callq 0x4350
leaq 0x3491(%rip), %rsi      # 0x9004
movq %rax, %rdi
callq 0x4350
movzwl -0x2(%rbx), %ecx
rolw $0x8, %cx
movzwl %cx, %esi
movq %rax, %rdi
callq 0x4510
movq %rax, %rdi
callq 0x4190
leaq 0x30(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x430(%rsp), %rdi
callq 0x645e
xorl %r13d, %r13d
leaq 0x560(%rsp), %r12
leaq 0x228(%rsp), %r14
pushq $0x1
popq %r15
leaq 0x20(%rsp), %rbx
movl $0xc9, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x41f0
andl $0x0, 0x178(%rsp)
leaq 0x178(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl %r15d, %esi
movq 0x8378(%rip), %rdx      # 0xdf78
leaq 0x14(%rsp), %rcx
movq %r12, %r8
leaq 0x35c1(%rip), %r9       # 0x91d0
callq 0x64a5
movq %r14, %rdi
callq 0x65f1
cmpl $-0x1, %eax
je 0x5c70
movq %r14, %rdi
callq 0x6622
movq %rax, %rbp
cmpl $-0x1, %ebp
je 0x5fd3
movslq %ebp, %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4050
movslq %r13d, %rdx
movq %rbx, %rdi
leaq 0x34aa(%rip), %rsi      # 0x90fa
callq 0x45c0
cmpq $-0x1, %rax
jne 0x5c8b
addl %ebp, %r13d
leaq 0x230(%rsp), %rdi
callq 0x68ee
jmp 0x5bd0
movl 0x14(%rsp), %edi
callq 0x45b0
leaq 0x230(%rsp), %rdi
callq 0x68ee
jmp 0x5f3c
addq $0x5, %rax
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x4490
leaq 0x140(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4480
leaq 0x430(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x490a
movl %eax, %ebp
leaq 0x140(%rsp), %rdi
callq 0x4170
testl %ebp, %ebp
movl $0xc8, %ebx
cmovnel %ebp, %ebx
leaq 0x230(%rsp), %rdi
callq 0x68ee
leaq 0x120(%rsp), %rdi
leaq 0x430(%rsp), %rsi
callq 0x5296
leaq 0x60(%rsp), %rdi
leaq 0x120(%rsp), %rdx
movq 0x18(%rsp), %rsi
callq 0x6658
movq 0x60(%rsp), %rdi
leaq 0x4d0(%rsp), %rsi
callq 0x4300
testl %eax, %eax
jne 0x5ffa
movq 0x500(%rsp), %r14
cmpl $0xc8, %ebx
jne 0x5d56
movl $0xf000, %eax           # imm = 0xF000
andl 0x4e8(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
je 0x5fe4
leaq 0x178(%rsp), %rdi
callq 0x6690
leaq 0x32dc(%rip), %rsi      # 0x9046
leaq 0x100(%rsp), %rdi
leaq 0x228(%rsp), %rdx
callq 0x4bfa
leaq 0x178(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x472c
leaq 0x100(%rsp), %rdi
callq 0x4170
leaq 0xe0(%rsp), %rdi
movl %ebx, %esi
callq 0x66d7
leaq 0x178(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x58fa
leaq 0xe0(%rsp), %rdi
callq 0x4170
leaq 0xc0(%rsp), %rdi
movl %ebx, %esi
callq 0x5a4c
leaq 0x178(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x5904
leaq 0xc0(%rsp), %rdi
callq 0x4170
cmpl $0xc8, %ebx
jne 0x5e6e
leaq 0x32fc(%rip), %rsi      # 0x910e
leaq 0xa0(%rsp), %rdi
leaq 0x228(%rsp), %rdx
callq 0x4bfa
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x6730
leaq 0x178(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x4748
leaq 0x80(%rsp), %rdi
callq 0x4170
leaq 0xa0(%rsp), %rdi
callq 0x4170
leaq 0x40(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x4a8a
movl 0x14(%rsp), %edi
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41b0
cmpq $-0x1, %rax
je 0x6013
leaq 0x228(%rsp), %rdi
callq 0x43e0
cmpl $0x194, %ebx            # imm = 0x194
je 0x5ec9
leaq 0x228(%rsp), %rdi
leaq 0x60(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x42d0
movq 0x228(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x248(%rsp,%rax)
jne 0x6024
cmpl $0x194, %ebx            # imm = 0x194
jne 0x5f65
leaq 0x228(%rsp), %rdi
callq 0x4090
movl 0x14(%rsp), %edi
callq 0x45b0
leaq 0x228(%rsp), %rdi
callq 0x40d0
leaq 0x40(%rsp), %rdi
callq 0x4170
leaq 0x178(%rsp), %rdi
callq 0x6790
leaq 0x60(%rsp), %rdi
callq 0x4170
leaq 0x120(%rsp), %rdi
callq 0x4170
leaq 0x430(%rsp), %rdi
callq 0x67c0
leaq 0x20(%rsp), %rdi
callq 0x4170
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x560(%rsp), %r14
leaq 0x228(%rsp), %r15
leaq 0x31a1(%rip), %rbx      # 0x911d
movq 0x228(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x248(%rsp,%rax)
jne 0x5eeb
movl $0xc8, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x41f0
movl $0xc8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x4420
movl 0x14(%rsp), %edi
movq 0x230(%rsp), %rdx
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x41b0
cmpq $-0x1, %rax
jne 0x5f7c
jmp 0x602b
leaq 0x311b(%rip), %rdi      # 0x90f5
callq 0x42b0
jmp 0x5c79
leaq 0x3119(%rip), %rdi      # 0x9104
callq 0x42b0
movl $0x194, %ebx            # imm = 0x194
jmp 0x5d56
leaq 0x30fe(%rip), %rdi      # 0x90ff
callq 0x42b0
movl $0x194, %ebx            # imm = 0x194
xorl %r14d, %r14d
jmp 0x5d37
leaq 0x3103(%rip), %rdi      # 0x911d
callq 0x42b0
jmp 0x5e9f
leaq 0x30f7(%rip), %rbx      # 0x9122
movq %rbx, %rdi
callq 0x42b0
jmp 0x5eeb
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x4170
jmp 0x604d
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
jmp 0x60c6
jmp 0x60aa
jmp 0x605b
movq %rax, %rbx
jmp 0x60c1
jmp 0x60aa
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x60c6
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
jmp 0x60c6
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x60c6
jmp 0x60aa
movq %rax, %rbx
jmp 0x60e2
jmp 0x60f1
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x4170
jmp 0x60f9
jmp 0x60f6
jmp 0x60f6
jmp 0x60f6
movq %rax, %rbx
jmp 0x60cb
jmp 0x60b1
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x40d0
leaq 0x40(%rsp), %rdi
callq 0x4170
leaq 0x178(%rsp), %rdi
callq 0x6790
leaq 0x60(%rsp), %rdi
callq 0x4170
leaq 0x120(%rsp), %rdi
callq 0x4170
jmp 0x6106
movq %rax, %rbx
jmp 0x6106
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x68ee
leaq 0x430(%rsp), %rdi
callq 0x67c0
leaq 0x20(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x4540