// File: 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
movq %rsp, %rbp
subq $0x730, %rsp            # imm = 0x730
movq %rdx, -0x688(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x8(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x5230
movzwl -0x10(%rbp), %edi
leaq -0x10(%rbp), %rsi
addq $0x4, %rsi
leaq -0x30(%rbp), %rdx
movl $0x10, %ecx
callq 0x5470
movq 0x1403e(%rip), %rdi     # 0x1cfb0
leaq 0xa1db(%rip), %rsi      # 0x13154
callq 0x53f0
movq %rax, %rdi
leaq -0x30(%rbp), %rsi
callq 0x53f0
movq %rax, %rdi
leaq 0xa070(%rip), %rsi      # 0x13004
callq 0x53f0
movq %rax, -0x680(%rbp)
movzwl -0xe(%rbp), %edi
callq 0x56c0
movq -0x680(%rbp), %rdi
movzwl %ax, %esi
callq 0x5650
movq %rax, %rdi
movq 0x13fb6(%rip), %rsi     # 0x1cf78
callq 0x5450
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0xc9, %edx
callq 0x5230
leaq -0x120(%rbp), %rdi
callq 0x55c0
movl $0x0, -0x124(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x9e60
movl $0xc8, -0x1cc(%rbp)
movl $0x0, -0x1d0(%rbp)
jmp 0x9012
xorps %xmm0, %xmm0
movaps %xmm0, -0x6a0(%rbp)
movups %xmm0, -0x47(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movl $0x0, -0x1e4(%rbp)
movq %rsp, %rax
leaq -0x1e4(%rbp), %rcx
movq %rcx, (%rax)
movq 0x13ee2(%rip), %rdx     # 0x1cf68
leaq 0xa1bf(%rip), %r9       # 0x1324c
leaq -0x1e0(%rbp), %rdi
movl $0x1, %esi
leaq -0x1c(%rbp), %rcx
leaq -0x100(%rbp), %r8
callq 0x9eb0
jmp 0x90ab
leaq -0x1e0(%rbp), %rdi
callq 0xa0a0
movl %eax, -0x6a4(%rbp)
jmp 0x90bf
movl -0x6a4(%rbp), %eax
cmpl $-0x1, %eax
jne 0x911b
movl -0x1c(%rbp), %edi
callq 0x5700
jmp 0x90d4
movl $0x198, -0x1cc(%rbp)    # imm = 0x198
movl $0x1, -0x1f8(%rbp)
jmp 0x927b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x993a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x92ae
leaq -0x1e0(%rbp), %rdi
callq 0xa100
movq %rax, -0x6b0(%rbp)
jmp 0x9130
movq -0x6b0(%rbp), %rax
movl %eax, -0x1d0(%rbp)
cmpl $-0x1, -0x1d0(%rbp)
jne 0x9163
leaq 0xa022(%rip), %rdi      # 0x1316f
callq 0x5330
jmp 0x9154
movl $0x1, -0x1f8(%rbp)
jmp 0x927b
movslq -0x1d0(%rbp), %rdx
leaq -0x120(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x5080
jmp 0x917f
movslq -0x124(%rbp), %rdx
leaq -0x120(%rbp), %rdi
leaq 0x9fe0(%rip), %rsi      # 0x13174
callq 0x5720
movq %rax, -0x200(%rbp)
cmpq $-0x1, -0x200(%rbp)
jne 0x91c1
movl -0x1d0(%rbp), %eax
addl -0x124(%rbp), %eax
movl %eax, -0x124(%rbp)
jmp 0x9271
movq -0x200(%rbp), %rsi
addq $0x5, %rsi
leaq -0x120(%rbp), %rdi
callq 0x55b0
jmp 0x91da
leaq -0x228(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x51a0
jmp 0x91ef
leaq -0x1c8(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x5c50
movl %eax, -0x6b4(%rbp)
jmp 0x920a
leaq -0x228(%rbp), %rdi
callq 0x5280
movl -0x6b4(%rbp), %eax
movl %eax, -0x204(%rbp)
cmpl $0x0, -0x204(%rbp)
je 0x9265
cmpl $0xc8, -0x1cc(%rbp)
jne 0x9265
movl -0x204(%rbp), %eax
movl %eax, -0x1cc(%rbp)
jmp 0x9265
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x5280
jmp 0x92ae
movl $0x3, -0x1f8(%rbp)
jmp 0x927b
movl $0x0, -0x1f8(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0xa170
movl -0x1f8(%rbp), %eax
movl %eax, -0x6b8(%rbp)
testl %eax, %eax
je 0x92a9
jmp 0x9299
movl -0x6b8(%rbp), %eax
subl $0x3, %eax
je 0x92bf
jmp 0x98e9
jmp 0x9010
leaq -0x1e0(%rbp), %rdi
callq 0xa170
jmp 0x993a
leaq -0x248(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x7f80
jmp 0x92d4
movq -0x688(%rbp), %rsi
leaq -0x268(%rbp), %rdi
leaq -0x248(%rbp), %rdx
callq 0xa190
jmp 0x92f0
movq $0x0, -0x270(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x5180
movq %rax, %rdi
leaq -0x300(%rbp), %rsi
callq 0x5390
cmpl $0x0, %eax
jne 0x9342
movq -0x2d0(%rbp), %rax
movq %rax, -0x270(%rbp)
jmp 0x9373
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x992e
leaq 0x9e30(%rip), %rdi      # 0x13179
callq 0x5330
jmp 0x9350
movl $0x194, -0x1cc(%rbp)    # imm = 0x194
jmp 0x9373
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x9922
movl -0x2e8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x93a9
cmpl $0xc8, -0x1cc(%rbp)
jne 0x93a9
leaq 0x9de6(%rip), %rdi      # 0x1317e
callq 0x5330
jmp 0x939f
movl $0x194, -0x1cc(%rbp)    # imm = 0x194
leaq -0x3a0(%rbp), %rdi
callq 0xa210
leaq -0x3c1(%rbp), %rdi
movq %rdi, -0x6c0(%rbp)
callq 0x5690
movq -0x6c0(%rbp), %rdx
leaq 0x9c70(%rip), %rsi      # 0x13046
leaq -0x3c0(%rbp), %rdi
callq 0x6200
jmp 0x93e4
leaq -0x3a0(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0x58e0
jmp 0x93f9
leaq -0x3c0(%rbp), %rdi
callq 0x5280
leaq -0x3c1(%rbp), %rdi
callq 0x5480
movl -0x1cc(%rbp), %esi
leaq -0x3e8(%rbp), %rdi
callq 0xa260
jmp 0x9425
leaq -0x3a0(%rbp), %rdi
leaq -0x3e8(%rbp), %rsi
callq 0x8a00
jmp 0x943a
leaq -0x3e8(%rbp), %rdi
callq 0x5280
movl -0x1cc(%rbp), %esi
leaq -0x408(%rbp), %rdi
callq 0x8c80
jmp 0x945a
leaq -0x3a0(%rbp), %rdi
leaq -0x408(%rbp), %rsi
callq 0x8a30
jmp 0x946f
leaq -0x408(%rbp), %rdi
callq 0x5280
cmpl $0xc8, -0x1cc(%rbp)
jne 0x9617
leaq -0x429(%rbp), %rdi
movq %rdi, -0x6c8(%rbp)
callq 0x5690
movq -0x6c8(%rbp), %rdx
leaq 0x9cdc(%rip), %rsi      # 0x13188
leaq -0x428(%rbp), %rdi
callq 0x6200
jmp 0x94ba
movq -0x270(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0xa380
jmp 0x94cf
leaq -0x3a0(%rbp), %rdi
leaq -0x428(%rbp), %rsi
leaq -0x450(%rbp), %rdx
callq 0x5940
jmp 0x94eb
leaq -0x450(%rbp), %rdi
callq 0x5280
leaq -0x428(%rbp), %rdi
callq 0x5280
leaq -0x429(%rbp), %rdi
callq 0x5480
jmp 0x9617
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x9546
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x3c0(%rbp), %rdi
callq 0x5280
leaq -0x3c1(%rbp), %rdi
callq 0x5480
jmp 0x9916
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x9916
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x3e8(%rbp), %rdi
callq 0x5280
jmp 0x9916
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x408(%rbp), %rdi
callq 0x5280
jmp 0x9916
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x9606
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x95fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x450(%rbp), %rdi
callq 0x5280
leaq -0x428(%rbp), %rdi
callq 0x5280
leaq -0x429(%rbp), %rdi
callq 0x5480
jmp 0x9916
leaq -0x470(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x5f30
jmp 0x962c
movl -0x1c(%rbp), %eax
movl %eax, -0x6dc(%rbp)
leaq -0x470(%rbp), %rdi
movq %rdi, -0x6e8(%rbp)
callq 0x5180
movq -0x6e8(%rbp), %rdi
movq %rax, -0x6d8(%rbp)
callq 0x52c0
movl -0x6dc(%rbp), %edi
movq -0x6d8(%rbp), %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x5200
movq %rax, -0x6d0(%rbp)
jmp 0x967b
movq -0x6d0(%rbp), %rax
cmpq $-0x1, %rax
jne 0x96af
leaq 0x9b08(%rip), %rdi      # 0x13197
callq 0x5330
jmp 0x9696
jmp 0x96af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
jmp 0x990a
leaq -0x678(%rbp), %rdi
callq 0x54d0
jmp 0x96bd
cmpl $0x194, -0x1cc(%rbp)    # imm = 0x194
je 0x9708
leaq -0x678(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x8, %edx
callq 0x5360
jmp 0x96e3
jmp 0x9708
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1f0(%rbp)
movl %eax, -0x1f4(%rbp)
leaq -0x678(%rbp), %rdi
callq 0x5110
jmp 0x990a
movq -0x678(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x678(%rbp,%rax), %rdi
callq 0x51d0
movb %al, -0x6e9(%rbp)
jmp 0x9728
movb -0x6e9(%rbp), %al
testb $0x1, %al
jne 0x9734
jmp 0x974c
leaq 0x9a61(%rip), %rdi      # 0x1319c
callq 0x5330
jmp 0x9742
movl $0x194, -0x1cc(%rbp)    # imm = 0x194
jmp 0x974e
movq -0x678(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x678(%rbp,%rax), %rdi
callq 0x5520
movb %al, -0x6ea(%rbp)
jmp 0x976e
movb -0x6ea(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x6eb(%rbp)
jne 0x9783
jmp 0x9796
cmpl $0x194, -0x1cc(%rbp)    # imm = 0x194
setne %al
movb %al, -0x6eb(%rbp)
movb -0x6eb(%rbp), %al
testb $0x1, %al
jne 0x97a5
jmp 0x988b
xorps %xmm0, %xmm0
movaps %xmm0, -0x700(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
movaps %xmm0, -0x100(%rbp)
movq $0x0, -0x40(%rbp)
leaq -0x678(%rbp), %rdi
leaq -0x100(%rbp), %rsi
movl $0xc8, %edx
callq 0x5530
jmp 0x9819
movl -0x1c(%rbp), %eax
movl %eax, -0x714(%rbp)
leaq -0x100(%rbp), %rax
movq %rax, -0x710(%rbp)
leaq -0x678(%rbp), %rdi
callq 0x5740
movq %rax, -0x708(%rbp)
jmp 0x9845
movq -0x708(%rbp), %rdx
movq -0x710(%rbp), %rsi
movl -0x714(%rbp), %edi
xorl %ecx, %ecx
callq 0x5200
movq %rax, -0x720(%rbp)
jmp 0x9869
movq -0x720(%rbp), %rax
cmpq $-0x1, %rax
jne 0x9886
leaq 0x991a(%rip), %rdi      # 0x13197
callq 0x5330
jmp 0x9884
jmp 0x988b
jmp 0x974e
leaq -0x678(%rbp), %rdi
callq 0x50d0
jmp 0x9899
movl -0x1c(%rbp), %edi
callq 0x5700
jmp 0x98a3
leaq -0x678(%rbp), %rdi
callq 0x5110
leaq -0x470(%rbp), %rdi
callq 0x5280
leaq -0x3a0(%rbp), %rdi
callq 0xa4b0
leaq -0x268(%rbp), %rdi
callq 0x5280
leaq -0x248(%rbp), %rdi
callq 0x5280
movl $0x0, -0x1f8(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0xa500
leaq -0x120(%rbp), %rdi
callq 0x5280
addq $0x730, %rsp            # imm = 0x730
popq %rbp
retq
leaq -0x470(%rbp), %rdi
callq 0x5280
leaq -0x3a0(%rbp), %rdi
callq 0xa4b0
leaq -0x268(%rbp), %rdi
callq 0x5280
leaq -0x248(%rbp), %rdi
callq 0x5280
leaq -0x1c8(%rbp), %rdi
callq 0xa500
leaq -0x120(%rbp), %rdi
callq 0x5280
movq -0x1f0(%rbp), %rdi
callq 0x5680
nop