// File: /workspace/github_cpp_makefile_star2_O3/dentiny[P]HTTP-Cache-Proxy/Proxy.cpp
// Function: Proxy::handleConnect(int, int, int, Request const&)
void handleConnect(int client_id, int client_fd, int server_fd, const Request & request)
	{
		// send a 200 OK to client
		const char * okMsg = "200 OK";
		int len = send(client_fd, okMsg, strlen(okMsg) + 1, 0);
		if(len == -1)
		{
			throw ProxyException("Send 200 OK to client error");
		}

		// select and keep listening
		bool connectionEnd = false;
		fd_set rfds;
		int fds[] = { client_fd, server_fd };
		int max_fd = std::max(fds[0], fds[1]) + 1; // argument of select() is the maximum fd + 1
		std::vector<char> buffer(BUFFER_SIZE, '\0'); // buffer to store request or response
		while(true)
		{
			// check if break loop
			if(connectionEnd)
			{
				break;
			}

			// initialize the fd set
			FD_ZERO(&rfds);
			FD_SET(fds[0], &rfds);
			FD_SET(fds[1], &rfds);

			// listen to fds in a blocking way
			status = select(max_fd, &rfds, NULL, NULL, NULL); 
			if(status == -1)
			{
				throw ProxyException("Server or client select error");
			}

			// receive request or respond from the set fd
			// send to the other fd
			for(int i = 0; i < 2; ++i)
			{
				if(FD_ISSET(fds[i], &rfds))
				{
					// receive
					len = recv(fds[i], &buffer.data()[0], BUFFER_SIZE - 1, 0);
					if(len == -1)
					{
						throw ProxyException("Receive request or respond error");
					}

					// decide the end of CONNECT
					else if(len == 0)
					{
						connectionEnd = true;
						break;
					}
					buffer[len] = '\0';

					// send
					len = send(fds[1 - i], &buffer.data()[0], len, 0);
					if(len == -1)
					{
						throw ProxyException("Send to client or server error");
					}
				}
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %ecx, %r15d
movl %edx, %r12d
movq %rdi, 0x10(%rsp)
leaq 0x786a(%rip), %rsi      # 0xd251
movl $0x7, %edx
movl %r12d, %edi
xorl %ecx, %ecx
callq 0x31b0
cmpl $-0x1, %eax
je 0x5cc5
movl %r12d, 0x8(%rsp)
movl %r15d, 0xc(%rsp)
cmpl %r15d, %r12d
movl %r15d, %ebp
cmovgl %r12d, %ebp
movl $0x10000, %edi          # imm = 0x10000
callq 0x3330
movq %rax, %rbx
leaq 0xc(%rsp), %r13
incl %ebp
movl $0x10000, %edx          # imm = 0x10000
movq %rax, %rdi
xorl %esi, %esi
callq 0x31f0
leal 0x3f(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
movl %eax, %ecx
andl $-0x40, %ecx
subl %ecx, %r12d
movl $0x1, %edx
movl %r12d, %ecx
shlq %cl, %rdx
movq %rdx, 0x28(%rsp)
movl $0x1, %r12d
sarl $0x6, %eax
cltq
movq %rax, 0x20(%rsp)
leal 0x3f(%r15), %eax
testl %r15d, %r15d
cmovnsl %r15d, %eax
movl %eax, %ecx
andl $-0x40, %ecx
subl %ecx, %r15d
movl %r15d, %ecx
shlq %cl, %r12
sarl $0x6, %eax
cltq
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rsp), %rcx
orq %rax, 0xb0(%rsp,%rcx,8)
movq 0x18(%rsp), %rax
orq %r12, 0xb0(%rsp,%rax,8)
movl %ebp, %edi
leaq 0xb0(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3300
movq 0x10(%rsp), %rcx
movl %eax, 0x2b0(%rcx)
cmpl $-0x1, %eax
je 0x5c69
movl $0x1, %r14d
movb $0x1, %r15b
leaq 0x8(%rsp), %rax
movl (%rax), %edi
leal 0x3f(%rdi), %eax
testl %edi, %edi
cmovnsl %edi, %eax
sarl $0x6, %eax
cltq
movq 0xb0(%rsp,%rax,8), %rax
btq %rdi, %rax
jae 0x5b7b
movl $0xffff, %edx           # imm = 0xFFFF
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x3100
testl %eax, %eax
je 0x5b92
cmpl $-0x1, %eax
je 0x5bb1
movslq %eax, %rdx
movb $0x0, (%rbx,%rdx)
movl 0x8(%rsp,%r14,4), %edi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x31b0
cmpl $-0x1, %eax
je 0x5c0d
xorl %r14d, %r14d
movq %r13, %rax
testb $0x1, %r15b
movl $0x0, %r15d
jne 0x5b27
jmp 0x5a8d
movl $0x10000, %esi          # imm = 0x10000
movq %rbx, %rdi
callq 0x3340
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x28, %edi
callq 0x31a0
movq %rax, %r15
leaq 0x60(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x76c3(%rip), %rsi      # 0xd292
leaq 0x76dc(%rip), %rdx      # 0xd2b2
leaq 0x50(%rsp), %rdi
callq 0x3c80
movb $0x1, %bpl
leaq 0x50(%rsp), %rsi
movq %r15, %rdi
callq 0x6a7c
xorl %ebp, %ebp
leaq 0xc10f(%rip), %rsi      # 0x11d08
leaq 0xec6(%rip), %rdx       # 0x6ac6
movq %r15, %rdi
callq 0x34e0
jmp 0x5d24
movl $0x28, %edi
callq 0x31a0
movq %rax, %r15
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x7688(%rip), %rsi      # 0xd2b3
leaq 0x769f(%rip), %rdx      # 0xd2d1
leaq 0x30(%rsp), %rdi
callq 0x3c80
movb $0x1, %bpl
leaq 0x30(%rsp), %rsi
movq %r15, %rdi
callq 0x6a7c
xorl %ebp, %ebp
leaq 0xc0b3(%rip), %rsi      # 0x11d08
leaq 0xe6a(%rip), %rdx       # 0x6ac6
movq %r15, %rdi
callq 0x34e0
jmp 0x5d24
movl $0x28, %edi
callq 0x31a0
movq %rax, %r15
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x75ea(%rip), %rsi      # 0xd274
leaq 0x7600(%rip), %rdx      # 0xd291
leaq 0x70(%rsp), %rdi
callq 0x3c80
movb $0x1, %bpl
leaq 0x70(%rsp), %rsi
movq %r15, %rdi
callq 0x6a7c
xorl %ebp, %ebp
leaq 0xc054(%rip), %rsi      # 0x11d08
leaq 0xe0b(%rip), %rdx       # 0x6ac6
movq %r15, %rdi
callq 0x34e0
jmp 0x5d24
movl $0x28, %edi
callq 0x31a0
movq %rax, %rbx
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x7573(%rip), %rsi      # 0xd258
leaq 0x7587(%rip), %rdx      # 0xd273
leaq 0x90(%rsp), %rdi
callq 0x3c80
movb $0x1, %bpl
leaq 0x90(%rsp), %rsi
movq %rbx, %rdi
callq 0x6a7c
xorl %ebp, %ebp
leaq 0xbff3(%rip), %rsi      # 0x11d08
leaq 0xdaa(%rip), %rdx       # 0x6ac6
movq %rbx, %rdi
callq 0x34e0
movq %rax, %r14
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x5d44
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3340
testb %bpl, %bpl
jne 0x5d51
jmp 0x5dcf
movq %rax, %r14
movq %rbx, %rdi
callq 0x3250
jmp 0x5dcf
movq %rax, %r14
jmp 0x5dcf
movq %rax, %r14
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x5dab
movq 0x80(%rsp), %rsi
jmp 0x5da3
jmp 0x5db2
jmp 0x5dbf
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x5dab
movq 0x40(%rsp), %rsi
jmp 0x5da3
jmp 0x5db2
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %r12, %rdi
je 0x5dab
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3340
testb %bpl, %bpl
jne 0x5db5
jmp 0x5dc2
movq %rax, %r14
movq %r15, %rdi
callq 0x3250
jmp 0x5dc2
movq %rax, %r14
movl $0x10000, %esi          # imm = 0x10000
movq %rbx, %rdi
callq 0x3340
movq %r14, %rdi
callq 0x3510
nop