// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
leaq 0x2bc(%rsp), %rbx
movq %rdi, -0x4(%rbx)
movq %rsi, 0x4(%rbx)
leaq 0xc(%rsp), %rax
movl %ecx, (%rax)
xorps %xmm0, %xmm0
leaq 0x2a0(%rsp), %r15
movaps %xmm0, (%r15)
movzwl %di, %edi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x10, %ecx
callq 0x43b0
movq 0x6970(%rip), %r14      # 0xcfb8
leaq 0x2a8b(%rip), %rsi      # 0x90da
movl $0x1a, %edx
movq %r14, %rdi
callq 0x4400
movq %r15, %rdi
callq 0x4170
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4400
leaq 0x298b(%rip), %rsi      # 0x9004
movl $0x1, %edx
movq %r14, %rdi
callq 0x4400
movzwl -0x2(%rbx), %eax
rolw $0x8, %ax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x4230
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4240
leaq 0x560(%rsp), %r12
xorl %ebx, %ebx
movl $0xc9, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x41d0
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq 0x218(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq 0x258(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq 0x62ee(%rip), %rcx      # 0xca18
addq $0x10, %rcx
movq %rcx, 0x78(%rsp)
movq %rcx, -0x58(%rax)
leaq 0x270(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
leaq 0x290(%rsp), %rax
movq %rax, -0x10(%rax)
movq %rbx, -0x8(%rax)
movb %bl, (%rax)
movl $0xc8, %ebp
leaq 0x2c8(%rsp), %r14
leaq 0x18(%rsp), %r15
movl $0xc9, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x41d0
movl $0x0, 0x160(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
movl $0x1, %esi
movq 0x67d6(%rip), %rdx      # 0xcf78
leaq 0xc(%rsp), %rcx
movq %r12, %r8
leaq 0x2a1f(%rip), %r9       # 0x91d0
callq 0x7695
movq %r14, %rdi
callq 0x780f
cmpl $-0x1, %eax
je 0x6889
movq %r14, %rdi
callq 0x7848
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x68c2
movslq %r13d, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x4060
movslq %ebx, %rdx
movl $0x4, %ecx
movq %r15, %rdi
leaq 0x28fe(%rip), %rsi      # 0x90fa
callq 0x4530
cmpq $-0x1, %rax
je 0x689f
addq $0x5, %rax
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x40f0
leaq 0x68(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r13
movq %r13, %rdi
callq 0x50d2
leaq 0x200(%rsp), %rdi
movq %r13, %rsi
callq 0x4ad4
movl %eax, %r13d
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x686f
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4370
cmpl $0xc8, %ebp
movl %ebp, %eax
cmovel %r13d, %eax
testl %r13d, %r13d
cmovnel %eax, %ebp
movl $0x3, %r13d
jmp 0x68a5
movl 0xc(%rsp), %edi
callq 0x45b0
movl $0x198, %ebp            # imm = 0x198
movl $0x1, %r13d
jmp 0x68a5
addl %r13d, %ebx
xorl %r13d, %r13d
movq 0x2d0(%rsp), %rdi
testq %rdi, %rdi
je 0x68b7
callq 0x7a7c
testl %r13d, %r13d
je 0x676d
jmp 0x68d0
leaq 0x282c(%rip), %rdi      # 0x90f5
callq 0x42b0
jmp 0x6897
cmpl $0x3, %r13d
jne 0x6e0d
leaq 0x140(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x56be
leaq 0x100(%rsp), %rdi
leaq 0x140(%rsp), %rdx
movq 0x10(%rsp), %rsi
callq 0x7899
movq 0x100(%rsp), %rdi
leaq 0x4d0(%rsp), %rsi
callq 0x42f0
testl %eax, %eax
jne 0x694e
movq 0x500(%rsp), %r14
movl $0xf000, %eax           # imm = 0xF000
andl 0x4e8(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x6962
cmpl $0xc8, %ebp
jne 0x6962
leaq 0x27b8(%rip), %rdi      # 0x9104
jmp 0x6958
leaq 0x27aa(%rip), %rdi      # 0x90ff
xorl %r14d, %r14d
callq 0x42b0
movl $0x194, %ebp            # imm = 0x194
leaq 0x178(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x1b8(%rsp), %rcx
movq %rcx, -0x30(%rcx)
movq $0x1, -0x28(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rcx)
movl $0x3f800000, -0x10(%rcx) # imm = 0x3F800000
movups %xmm0, -0x8(%rcx)
leaq 0x60bd(%rip), %rdx      # 0xca60
addq $0x10, %rdx
movq %rdx, 0x10(%rsp)
movq %rdx, -0x58(%rcx)
leaq 0x1d0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x1f0(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0xf0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x265f(%rip), %rsi      # 0x9046
leaq 0x2660(%rip), %rdx      # 0x904e
leaq 0xe0(%rsp), %rdi
callq 0x55e8
leaq 0x160(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x47fc
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x6a2d
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x4370
movl %ebp, %ebx
negl %ebx
cmovsl %ebp, %ebx
movl $0x1, %r15d
cmpl $0xa, %ebx
jb 0x6a88
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebx, %ecx
cmpl $0x63, %ecx
jbe 0x6a7f
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x6a85
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x6a88
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x6a4c
addl $-0x3, %r15d
jmp 0x6a88
addl $-0x2, %r15d
jmp 0x6a88
decl %r15d
movl %ebp, %r12d
shrl $0x1f, %r12d
leal (%r15,%r12), %esi
leaq 0xd0(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0xc0(%rsp), %rdi
movl $0x2d, %edx
callq 0x4410
movl %r12d, %edi
leaq 0xc0(%rsp), %r12
addq (%r12), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x7aeb
leaq 0x160(%rsp), %rdi
movq %r12, %rsi
callq 0x61aa
movq 0xc0(%rsp), %rdi
cmpq %r13, %rdi
je 0x6af7
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x120(%rsp), %rdi
movl %ebp, %esi
callq 0x650c
leaq 0x160(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x61b4
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6b3c
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x4370
cmpl $0xc8, %ebp
jne 0x6c61
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x25b6(%rip), %rsi      # 0x910e
leaq 0x25bd(%rip), %rdx      # 0x911c
leaq 0x38(%rsp), %rdi
callq 0x55e8
movq %r14, %rbx
negq %rbx
cmovsq %r14, %rbx
movl $0x1, %r15d
cmpq $0xa, %rbx
jb 0x6bd3
movl $0x4, %r15d
movabsq $0x346dc5d63886594b, %rsi # imm = 0x346DC5D63886594B
movq %rbx, %rcx
cmpq $0x63, %rcx
jbe 0x6bca
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x6bd0
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x6bd3
movq %rcx, %rax
mulq %rsi
shrq $0xb, %rdx
addl $0x4, %r15d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x6b92
addl $-0x3, %r15d
jmp 0x6bd3
addl $-0x2, %r15d
jmp 0x6bd3
decl %r15d
shrq $0x3f, %r14
leal (%r14,%r15), %esi
leaq 0xb0(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0xa0(%rsp), %rdi
movl $0x2d, %edx
callq 0x4410
leaq 0xa0(%rsp), %r12
addq (%r12), %r14
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x7b58
leaq 0x160(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x482a
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x48(%rsp), %rbx
je 0x6c4a
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6c61
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x80(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x4d3e
movl 0xc(%rsp), %edi
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41a0
cmpq $-0x1, %rax
je 0x6f41
leaq 0x2c8(%rsp), %rdi
callq 0x43f0
cmpl $0x194, %ebp            # imm = 0x194
je 0x6cca
leaq 0x2c8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
movl $0x8, %edx
callq 0x42d0
movq 0x2c8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2e8(%rsp,%rax)
jne 0x6f52
cmpl $0x194, %ebp            # imm = 0x194
jne 0x6ed3
leaq 0x2c8(%rsp), %rdi
callq 0x4090
movl 0xc(%rsp), %edi
callq 0x45b0
leaq 0x2c8(%rsp), %rdi
callq 0x40c0
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6d34
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x10(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x1e0(%rsp), %rdi
leaq 0x1f0(%rsp), %rax
cmpq %rax, %rdi
je 0x6d66
movq 0x1f0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x188(%rsp), %rbx
movq 0x38(%rbx), %rdi
leaq 0x1d0(%rsp), %rax
cmpq %rax, %rdi
je 0x6d8f
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x5d12(%rip), %rax      # 0xcaa8
movq %rax, 0x160(%rsp)
movq %rbx, %rdi
callq 0x7bd4
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x6dcb
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6dec
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6e0d
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x78(%rsp), %rax
movq %rax, 0x200(%rsp)
movq 0x280(%rsp), %rdi
leaq 0x290(%rsp), %rax
cmpq %rax, %rdi
je 0x6e3f
movq 0x290(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x260(%rsp), %rdi
leaq 0x270(%rsp), %rax
cmpq %rax, %rdi
je 0x6e64
movq 0x270(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x5c3d(%rip), %rax      # 0xcaa8
movq %rax, 0x200(%rsp)
leaq 0x228(%rsp), %rdi
callq 0x7bd4
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0x6ea5
movq 0x218(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6ec1
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4370
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x560(%rsp), %r14
leaq 0x2c8(%rsp), %r15
leaq 0x2233(%rip), %rbx      # 0x911d
movq 0x2c8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2e8(%rsp,%rax)
jne 0x6cf0
movl $0xc8, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x41d0
movl $0xc8, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x4430
movl 0xc(%rsp), %edi
movq 0x2d0(%rsp), %rdx
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x41a0
cmpq $-0x1, %rax
jne 0x6eea
jmp 0x6f59
leaq 0x21d5(%rip), %rdi      # 0x911d
callq 0x42b0
jmp 0x6c9b
leaq 0x21c9(%rip), %rbx      # 0x9122
movq %rbx, %rdi
callq 0x42b0
jmp 0x6cf0
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
je 0x6f8b
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x6f8b
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x7093
movq 0x48(%rsp), %rsi
jmp 0x708b
jmp 0x7029
jmp 0x6fac
movq %rax, %rbx
jmp 0x7072
jmp 0x7029
movq %rax, %rbx
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7093
movq 0x130(%rsp), %rsi
jmp 0x708b
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r13, %rdi
je 0x7093
movq 0xd0(%rsp), %rsi
jmp 0x708b
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x7093
movq 0xf0(%rsp), %rsi
jmp 0x708b
jmp 0x7029
movq %rax, %rbx
jmp 0x70c1
jmp 0x70e4
movq %rax, %rbx
jmp 0x7093
jmp 0x7062
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x70ec
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x70ec
jmp 0x70e9
jmp 0x70e9
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
callq 0x40c0
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7093
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x160(%rsp), %rdi
callq 0x78f4
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x70c1
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x70fe
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x70fe
movq %rax, %rbx
jmp 0x70fe
movq %rax, %rbx
movq 0x2d0(%rsp), %rdi
testq %rdi, %rdi
je 0x70fe
callq 0x7a7c
leaq 0x200(%rsp), %rdi
callq 0x796a
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x7127
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4370
movq %rbx, %rdi
callq 0x4550