// File: /workspace/github_cpp_makefile_star2_O2/m1c0l[P]http-client-server/web-client.cpp
// Function: processRequest(sockaddr*, HttpRequest*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int processRequest(sockaddr *serverAddr, HttpRequest* req, string filename) {

	// create a socket using TCP IP
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd == -1) {
		perror("socket");
		return -1;
	}

	// connect to the server
	future<int> connectFut = async(launch::async, connect, sockfd,
					serverAddr, sizeof(*serverAddr));
	if (waitFor(connectFut) == -1) {
		close(sockfd);
		return -1;
	}
	if (connectFut.get() == -1) {
		perror("connect");
		return -1;
	}

	// send request
	string msg = req->encode();
	future<ssize_t> sendFut = async(launch::async, send, sockfd,
					msg.c_str(), msg.size(), 0);
	if (waitFor(sendFut) == -1) {
		close(sockfd);
		return -1;
	}
	if (sendFut.get() == -1) {
		perror("send");
		close(sockfd);
		return -1;
	}

	char buf[BUFFER_SIZE + 1] = {0};
	string resBuf;
	HttpResponse* res = new HttpResponse();

	const string eoh = "\r\n\r\n"; // end of header
	int bytesRead = 0;

	// get response's header
	while (true) {
		memset(buf, '\0', sizeof(buf));

		future<ssize_t> recvFut = async(launch::async, recv, sockfd,
						buf, BUFFER_SIZE, 0);
		if (waitFor(recvFut) == -1) {
			close(sockfd);
			return -1;
		}
		bytesRead = recvFut.get();
		if (bytesRead == -1) {
			perror("recv");
			return -1;
		}
		resBuf.append(buf, bytesRead);

		// when the entire header has been read, decode it
		size_t pos = resBuf.find(eoh);
		if (pos != string::npos) {
			size_t end = pos + eoh.size();

			// decode the header
			res->decode(resBuf.substr(0, end));

			// discard the header but keep the rest of the buffer
			resBuf = resBuf.substr(end, resBuf.size());
			break;
		}

		// if end of header is not detected but no incoming data
		if (bytesRead == 0) {
			cerr << "Incomplete HTTP response" << '\n';
			close(sockfd);
			return 7;
		}
	}

	// output the status and description
	cout << req->getHeader("Host") << " " << req->getUrl() << ": " + res->getStatus() + " " + res->getDescription() << '\n';

	// 200 OK
	if (res->getStatus() == "200") {
		// get file name
		string filenameCopy = filename.c_str();
		string base = basename((char*)filenameCopy.c_str());
		if (filename[filename.size() - 1] == '/') {
			base = "index.html";
		}

		// save retrieved file
		ofstream of;
		of.open(base);

		// leftover data from reading header
		of.write(resBuf.c_str(), resBuf.size());

		while (true) {
			memset(buf, '\0', sizeof(buf));

			future<ssize_t> recvFut = async(launch::async, recv,
					sockfd, buf, BUFFER_SIZE, 0);
			if (waitFor(recvFut) == -1) {
				close(sockfd);
				return -1;
			}

			bytesRead = recvFut.get();
			if (bytesRead == -1) {
				perror("recv");
				close(sockfd);
				of.close();
				return 8;
			}
			if (bytesRead == 0) {
				of.close();
				close(sockfd);
				return 0;
			}

			of.write(buf, bytesRead);
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, 0xa8(%rsp)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x50a0
movl %eax, 0x8(%rsp)
cmpl $-0x1, %eax
je 0x73f9
leaq 0x1b0(%rsp), %r9
movq $0x10, (%r9)
movq 0xb1cd(%rip), %rdx      # 0x11f98
leaq 0x98(%rsp), %rbx
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %rcx
leaq 0xa8(%rsp), %r8
movq %rbx, %rdi
callq 0x7a95
movq %rbx, %rdi
callq 0x7bd6
cmpl $-0x1, %eax
je 0x6fca
leaq 0x98(%rsp), %rdi
callq 0x7c12
cmpl $-0x1, %eax
je 0x742d
leaq 0x110(%rsp), %rdi
movq %r15, %rsi
callq 0x5a4a
movq 0x110(%rsp), %rax
movq 0x118(%rsp), %rcx
leaq 0x1b0(%rsp), %r8
movq %rax, (%r8)
leaq 0x3b0(%rsp), %r9
movq %rcx, (%r9)
leaq 0x30(%rsp), %rax
andl $0x0, (%rax)
movq %rax, (%rsp)
movq 0xb11d(%rip), %rdx      # 0x11f78
leaq 0x50(%rsp), %rdi
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %rcx
callq 0x7c43
leaq 0x50(%rsp), %rdi
callq 0x7d8f
cmpl $-0x1, %eax
je 0x744a
leaq 0x50(%rsp), %rdi
callq 0x7dcc
cmpq $-0x1, %rax
je 0x743e
leaq 0x3b0(%rsp), %rdi
movl $0xc9, %edx
xorl %esi, %esi
callq 0x51c0
leaq 0x40(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl $0xa0, %edi
callq 0x5310
movq %rax, %rbx
movq %r15, 0x88(%rsp)
movq %r14, 0x80(%rsp)
movl $0xa0, %edx
movq %rax, %rdi
xorl %esi, %esi
callq 0x51c0
movq %rbx, 0x90(%rsp)
movq %rbx, %rdi
callq 0x7e02
leaq 0x41d3(%rip), %rsi      # 0xb0cf
leaq 0xf0(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
callq 0x5bba
leaq 0x3b0(%rsp), %rbp
leaq 0x10(%rsp), %r15
leaq 0x8(%rsp), %r13
leaq 0x30(%rsp), %rbx
movl $0xc9, %edx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x51c0
andl $0x0, 0x1b0(%rsp)
leaq 0x1b0(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
pushq $0x1
popq %rsi
movq 0xb010(%rip), %rdx      # 0x11f68
movq %r13, %rcx
movq %rbp, %r8
leaq 0x41eb(%rip), %r9       # 0xb150
callq 0x7e49
movq %r15, %rdi
callq 0x7d8f
cmpl $-0x1, %eax
je 0x6fdb
movq %r15, %rdi
callq 0x7dcc
movq %rax, %r12
cmpl $-0x1, %r12d
je 0x748e
movslq %r12d, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5060
movq %rbx, %rdi
leaq 0xf0(%rsp), %rsi
xorl %edx, %edx
callq 0x5580
cmpq $-0x1, %rax
jne 0x6feb
testl %r12d, %r12d
je 0x73ac
leaq 0x18(%rsp), %rdi
callq 0x81f8
jmp 0x6f28
movl 0x8(%rsp), %edi
callq 0x5560
pushq $-0x1
popq %rbp
jmp 0x746d
movl 0x8(%rsp), %edi
callq 0x5560
pushq $-0x1
jmp 0x73d5
movq %rax, %r14
addq 0xf8(%rsp), %r14
leaq 0x190(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x5350
leaq 0x190(%rsp), %rsi
movq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x58ca
movq 0x88(%rsp), %r12
leaq 0x190(%rsp), %rdi
callq 0x5140
leaq 0x30(%rsp), %rsi
movq 0x8(%rsi), %rcx
leaq 0x1b0(%rsp), %rdi
movq %r14, %rdx
callq 0x5350
leaq 0x30(%rsp), %rdi
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rsi
callq 0x53a0
movq %rbx, %rdi
callq 0x5140
leaq 0x18(%rsp), %rdi
callq 0x81f8
leaq 0x4071(%rip), %rsi      # 0xb0f2
leaq 0x170(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x5bba
leaq 0x1b0(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %r12, %rsi
callq 0x5722
movq 0xaefe(%rip), %rdi      # 0x11fb0
leaq 0x1b0(%rsp), %rsi
callq 0x52b0
leaq 0x4046(%rip), %rsi      # 0xb10c
movq %rax, %rdi
callq 0x5300
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x6256
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x52b0
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
movq %r15, %rsi
callq 0x68ce
leaq 0x4006(%rip), %rsi      # 0xb10b
leaq 0xd0(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x7f95
leaq 0x3feb(%rip), %rdx      # 0xb10c
leaq 0xb0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x5c30
leaq 0x130(%rsp), %rdi
movq %r15, %rsi
callq 0x68e0
leaq 0x60(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0x130(%rsp), %rdx
callq 0x668e
leaq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x52b0
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x5380
leaq 0x60(%rsp), %rdi
callq 0x5140
leaq 0x130(%rsp), %rdi
callq 0x5140
leaq 0xb0(%rsp), %rdi
callq 0x5140
leaq 0xd0(%rsp), %rdi
callq 0x5140
leaq 0x150(%rsp), %rdi
callq 0x5140
leaq 0x10(%rsp), %rdi
callq 0x5140
leaq 0x1b0(%rsp), %rdi
callq 0x5140
leaq 0x170(%rsp), %rdi
callq 0x5140
leaq 0x1b0(%rsp), %rdi
movq %r15, %rsi
callq 0x68ce
leaq 0x3f06(%rip), %rsi      # 0xb0f7
leaq 0x1b0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6677
movl %eax, %r14d
movq %rbx, %rdi
callq 0x5140
xorl %ebp, %ebp
testb %r14b, %r14b
je 0x73e0
movq 0x80(%rsp), %rbx
movq (%rbx), %rsi
leaq 0x10(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
callq 0x5bba
movq 0x10(%rsp), %rdi
callq 0x54f0
leaq 0x60(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
movq %rax, %rsi
callq 0x5bba
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
cmpb $0x2f, -0x1(%rax,%rcx)
jne 0x7272
leaq 0x3e93(%rip), %rsi      # 0xb0fb
leaq 0x60(%rsp), %rdi
callq 0x5530
leaq 0x1b0(%rsp), %rdi
callq 0x54e0
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x5160
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x1b0(%rsp), %rdi
callq 0x53f0
leaq 0x3b0(%rsp), %rbx
leaq 0xb0(%rsp), %r15
pushq $0x1
popq %r14
leaq 0x8(%rsp), %r12
movl $0xc9, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x51c0
andl $0x0, 0xd0(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
movl %r14d, %esi
movq 0xac74(%rip), %rdx      # 0x11f68
movq %r12, %rcx
movq %rbx, %r8
leaq 0x3e4f(%rip), %r9       # 0xb150
callq 0x7e49
movq %r15, %rdi
callq 0x7d8f
cmpl $-0x1, %eax
je 0x733c
movq %r15, %rdi
callq 0x7dcc
testl %eax, %eax
je 0x734d
cmpl $-0x1, %eax
je 0x7383
movslq %eax, %rdx
movb $0x1, %r13b
leaq 0x1b0(%rsp), %rdi
movq %rbx, %rsi
callq 0x53f0
jmp 0x7368
movl 0x8(%rsp), %edi
callq 0x5560
xorl %r13d, %r13d
pushq $-0x1
popq %rbp
jmp 0x7368
leaq 0x1b0(%rsp), %rdi
callq 0x5450
xorl %r13d, %r13d
movl 0x8(%rsp), %edi
callq 0x5560
xorl %ebp, %ebp
leaq 0xb8(%rsp), %rdi
callq 0x81f8
testb %r13b, %r13b
jne 0x72c4
jmp 0x740a
leaq 0x3d4a(%rip), %rdi      # 0xb0d4
callq 0x5280
movl 0x8(%rsp), %edi
callq 0x5560
leaq 0x1b0(%rsp), %rdi
callq 0x5450
xorl %r13d, %r13d
pushq $0x8
jmp 0x734a
movq 0xac3d(%rip), %rdi      # 0x11ff0
leaq 0x3d1f(%rip), %rsi      # 0xb0d9
callq 0x5300
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x5380
movl 0x8(%rsp), %edi
callq 0x5560
pushq $0x7
popq %rbp
leaq 0x18(%rsp), %rdi
callq 0x81f8
leaq 0xf0(%rsp), %rdi
callq 0x5140
leaq 0x30(%rsp), %rdi
callq 0x5140
jmp 0x7456
leaq 0x3cbb(%rip), %rdi      # 0xb0bb
callq 0x5280
pushq $-0x1
popq %rbp
jmp 0x747a
leaq 0x1b0(%rsp), %rdi
callq 0x5480
leaq 0x60(%rsp), %rdi
callq 0x5140
leaq 0x10(%rsp), %rdi
callq 0x5140
jmp 0x73e0
leaq 0x3c8e(%rip), %rdi      # 0xb0c2
callq 0x5280
jmp 0x6fd3
leaq 0x3c85(%rip), %rdi      # 0xb0ca
callq 0x5280
movl 0x8(%rsp), %edi
callq 0x5560
pushq $-0x1
popq %rbp
leaq 0x58(%rsp), %rdi
callq 0x81f8
leaq 0x110(%rsp), %rdi
callq 0x5140
leaq 0xa0(%rsp), %rdi
callq 0x81f8
movl %ebp, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3c3f(%rip), %rdi      # 0xb0d4
callq 0x5280
jmp 0x6fe4
jmp 0x74a1
movq %rax, %rbx
jmp 0x75b6
movq %rax, %rbx
jmp 0x75c0
jmp 0x7594
jmp 0x758a
jmp 0x7594
movq %rax, %rbx
jmp 0x751b
movq %rax, %rbx
jmp 0x7528
movq %rax, %rbx
jmp 0x7535
movq %rax, %rbx
jmp 0x7542
jmp 0x74d6
movq %rax, %rbx
jmp 0x754f
jmp 0x755b
movq %rax, %rbx
jmp 0x756b
jmp 0x7594
jmp 0x75cc
movq %rax, %rbx
leaq 0x190(%rsp), %rdi
callq 0x5140
jmp 0x75cf
jmp 0x75cc
jmp 0x75cc
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x5140
leaq 0x130(%rsp), %rdi
callq 0x5140
leaq 0xb0(%rsp), %rdi
callq 0x5140
leaq 0xd0(%rsp), %rdi
callq 0x5140
leaq 0x150(%rsp), %rdi
callq 0x5140
leaq 0x10(%rsp), %rdi
callq 0x5140
jmp 0x755e
movq %rax, %rbx
leaq 0x1b0(%rsp), %rdi
callq 0x5140
leaq 0x170(%rsp), %rdi
jmp 0x75c5
jmp 0x75cc
jmp 0x7579
movq %rax, %rbx
jmp 0x75e6
movq %rax, %rbx
jmp 0x75fa
jmp 0x758f
movq %rax, %rbx
jmp 0x75f0
movq %rax, %rbx
jmp 0x75a9
movq %rax, %rbx
jmp 0x7607
movq %rax, %rbx
jmp 0x75d9
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x81f8
leaq 0x1b0(%rsp), %rdi
callq 0x5480
leaq 0x60(%rsp), %rdi
callq 0x5140
leaq 0x10(%rsp), %rdi
callq 0x5140
jmp 0x75d9
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x81f8
leaq 0xf0(%rsp), %rdi
callq 0x5140
leaq 0x30(%rsp), %rdi
callq 0x5140
leaq 0x58(%rsp), %rdi
callq 0x81f8
leaq 0x110(%rsp), %rdi
callq 0x5140
leaq 0xa0(%rsp), %rdi
callq 0x81f8
movq %rbx, %rdi
callq 0x5500