// File: /workspace/github_cpp_makefile_star2_O1/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 0x6944(%rip), %r14      # 0xcfb8
leaq 0x2a5f(%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 0x295f(%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 0x628a(%rip), %rcx      # 0xc9e0
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 0x67aa(%rip), %rdx      # 0xcf78
leaq 0xc(%rsp), %rcx
movq %r12, %r8
leaq 0x29f3(%rip), %r9       # 0x91d0
callq 0x770b
movq %r14, %rdi
callq 0x789b
cmpl $-0x1, %eax
je 0x68b5
movq %r14, %rdi
callq 0x78d4
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x68ee
movslq %r13d, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x4060
movslq %ebx, %rdx
movl $0x4, %ecx
movq %r15, %rdi
leaq 0x28d2(%rip), %rsi      # 0x90fa
callq 0x4530
cmpq $-0x1, %rax
je 0x68cb
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 0x5100
leaq 0x200(%rsp), %rdi
movq %r13, %rsi
callq 0x4ae4
movl %eax, %r13d
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x689b
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 0x68d1
movl 0xc(%rsp), %edi
callq 0x45b0
movl $0x198, %ebp            # imm = 0x198
movl $0x1, %r13d
jmp 0x68d1
addl %r13d, %ebx
xorl %r13d, %r13d
movq 0x2d0(%rsp), %rdi
testq %rdi, %rdi
je 0x68e3
callq 0x7b18
testl %r13d, %r13d
je 0x6799
jmp 0x68fc
leaq 0x2800(%rip), %rdi      # 0x90f5
callq 0x42b0
jmp 0x68c3
cmpl $0x3, %r13d
jne 0x6e24
leaq 0x140(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x56ea
leaq 0x100(%rsp), %rdi
leaq 0x140(%rsp), %rdx
movq 0x10(%rsp), %rsi
callq 0x7925
movq 0x100(%rsp), %rdi
leaq 0x4d0(%rsp), %rsi
callq 0x42f0
testl %eax, %eax
jne 0x6f76
movq 0x500(%rsp), %r14
movl $0xf000, %eax           # imm = 0xF000
andl 0x4e8(%rsp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x6979
cmpl $0xc8, %ebp
je 0x6f60
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 0x606e(%rip), %rdx      # 0xca28
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 0x2648(%rip), %rsi      # 0x9046
leaq 0x2649(%rip), %rdx      # 0x904e
leaq 0xe0(%rsp), %rdi
callq 0x561a
leaq 0x160(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x480c
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x6a44
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x4370
movl %ebp, %ebx
negl %ebx
cmovsl %ebp, %ebx
movl $0x1, %r15d
cmpl $0xa, %ebx
jb 0x6a9f
movl $0x4, %r15d
movl $0xd1b71759, %eax       # imm = 0xD1B71759
movl %ebx, %ecx
cmpl $0x63, %ecx
jbe 0x6a96
cmpl $0x3e7, %ecx            # imm = 0x3E7
jbe 0x6a9c
cmpl $0x2710, %ecx           # imm = 0x2710
jb 0x6a9f
movl %ecx, %edx
imulq %rax, %rdx
shrq $0x2d, %rdx
addl $0x4, %r15d
cmpl $0x1869f, %ecx          # imm = 0x1869F
movl %edx, %ecx
ja 0x6a63
addl $-0x3, %r15d
jmp 0x6a9f
addl $-0x2, %r15d
jmp 0x6a9f
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 0x7b87
leaq 0x160(%rsp), %rdi
movq %r12, %rsi
callq 0x61d6
movq 0xc0(%rsp), %rdi
cmpq %r13, %rdi
je 0x6b0e
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x120(%rsp), %rdi
movl %ebp, %esi
callq 0x6538
leaq 0x160(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x61e0
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6b53
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x4370
cmpl $0xc8, %ebp
jne 0x6c78
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x259f(%rip), %rsi      # 0x910e
leaq 0x25a6(%rip), %rdx      # 0x911c
leaq 0x38(%rsp), %rdi
callq 0x561a
movq %r14, %rbx
negq %rbx
cmovsq %r14, %rbx
movl $0x1, %r15d
cmpq $0xa, %rbx
jb 0x6bea
movl $0x4, %r15d
movabsq $0x346dc5d63886594b, %rsi # imm = 0x346DC5D63886594B
movq %rbx, %rcx
cmpq $0x63, %rcx
jbe 0x6be1
cmpq $0x3e7, %rcx            # imm = 0x3E7
jbe 0x6be7
cmpq $0x2710, %rcx           # imm = 0x2710
jb 0x6bea
movq %rcx, %rax
mulq %rsi
shrq $0xb, %rdx
addl $0x4, %r15d
cmpq $0x1869f, %rcx          # imm = 0x1869F
movq %rdx, %rcx
ja 0x6ba9
addl $-0x3, %r15d
jmp 0x6bea
addl $-0x2, %r15d
jmp 0x6bea
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 0x7bf4
leaq 0x160(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x483a
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x48(%rsp), %rbx
je 0x6c61
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x6c78
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x80(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x4d6c
movl 0xc(%rsp), %edi
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41a0
cmpq $-0x1, %rax
je 0x6f8f
leaq 0x2c8(%rsp), %rdi
callq 0x43f0
cmpl $0x194, %ebp            # imm = 0x194
je 0x6ce1
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 0x6fa0
cmpl $0x194, %ebp            # imm = 0x194
jne 0x6eea
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 0x6d4b
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 0x6d7d
movq 0x1f0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x188(%rsp), %rbx
movq 0x38(%rbx), %rdi
leaq 0x1d0(%rsp), %rax
cmpq %rax, %rdi
je 0x6da6
movq 0x1d0(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x5cc3(%rip), %rax      # 0xca70
movq %rax, 0x160(%rsp)
movq %rbx, %rdi
callq 0x7c70
movq 0x168(%rsp), %rdi
leaq 0x178(%rsp), %rax
cmpq %rax, %rdi
je 0x6de2
movq 0x178(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6e03
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6e24
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 0x6e56
movq 0x290(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x260(%rsp), %rdi
leaq 0x270(%rsp), %rax
cmpq %rax, %rdi
je 0x6e7b
movq 0x270(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x5bee(%rip), %rax      # 0xca70
movq %rax, 0x200(%rsp)
leaq 0x228(%rsp), %rdi
callq 0x7c70
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0x6ebc
movq 0x218(%rsp), %rsi
incq %rsi
callq 0x4370
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x6ed8
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), %rbx
leaq 0x2c8(%rsp), %r14
movq 0x2c8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2e8(%rsp,%rax)
jne 0x6d07
movl $0xc8, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41d0
movl $0xc8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4430
movl 0xc(%rsp), %edi
movq 0x2d0(%rsp), %rdx
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x41a0
cmpq $-0x1, %rax
jne 0x6efa
leaq 0x21c7(%rip), %rdi      # 0x911d
callq 0x42b0
jmp 0x6d07
leaq 0x219d(%rip), %rdi      # 0x9104
callq 0x42b0
movl $0x194, %ebp            # imm = 0x194
jmp 0x6979
leaq 0x2182(%rip), %rdi      # 0x90ff
callq 0x42b0
movl $0x194, %ebp            # imm = 0x194
xorl %r14d, %r14d
jmp 0x695a
leaq 0x2187(%rip), %rdi      # 0x911d
callq 0x42b0
jmp 0x6cb2
leaq 0x217b(%rip), %rdi      # 0x9122
callq 0x42b0
movl $0x194, %ebp            # imm = 0x194
jmp 0x6cfb
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
je 0x6fdb
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x6fdb
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x70e3
movq 0x48(%rsp), %rsi
jmp 0x70db
jmp 0x7079
jmp 0x6ffc
movq %rax, %rbx
jmp 0x70c2
jmp 0x7079
movq %rax, %rbx
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x70e3
movq 0x130(%rsp), %rsi
jmp 0x70db
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r13, %rdi
je 0x70e3
movq 0xd0(%rsp), %rsi
jmp 0x70db
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x70e3
movq 0xf0(%rsp), %rsi
jmp 0x70db
jmp 0x7079
movq %rax, %rbx
jmp 0x7111
jmp 0x7134
movq %rax, %rbx
jmp 0x70e3
jmp 0x70b2
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x713c
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x713c
jmp 0x7139
jmp 0x7139
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
callq 0x40c0
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x70e3
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x160(%rsp), %rdi
callq 0x7980
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x7111
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x4370
leaq 0x150(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x714e
movq 0x150(%rsp), %rsi
incq %rsi
callq 0x4370
jmp 0x714e
movq %rax, %rbx
jmp 0x714e
movq %rax, %rbx
movq 0x2d0(%rsp), %rdi
testq %rdi, %rdi
je 0x714e
callq 0x7b18
leaq 0x200(%rsp), %rdi
callq 0x79f6
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x7177
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4370
movq %rbx, %rdi
callq 0x4550