// File: 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
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq 0x1103b(%rip), %rax     # 0x19239
movq %rax, -0x28(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x1a4(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4160
movl -0x1a4(%rbp), %edi
movq -0x1a0(%rbp), %rsi
movq %rax, %rdx
addq $0x1, %rdx
xorl %ecx, %ecx
callq 0x41c0
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x82ff
movb $0x1, -0x65(%rbp)
movl $0x28, %edi
callq 0x41b0
movq %rax, -0x1c0(%rbp)
leaq -0x51(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x4650
movq -0x1b8(%rbp), %rdx
leaq 0x10fc6(%rip), %rsi     # 0x19240
leaq -0x50(%rbp), %rdi
callq 0x4bf0
jmp 0x8285
movq -0x1c0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x9240
jmp 0x8297
movq -0x1c0(%rbp), %rdi
movb $0x0, -0x65(%rbp)
leaq 0x1ba4f(%rip), %rsi     # 0x23cf8
leaq 0x1000(%rip), %rdx      # 0x92b0
callq 0x4610
jmp 0x88b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x82dd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4250
leaq -0x51(%rbp), %rdi
callq 0x4420
testb $0x1, -0x65(%rbp)
jne 0x82ee
jmp 0x82fa
movq -0x1c0(%rbp), %rdi
callq 0x42d0
jmp 0x88af
movb $0x0, -0x66(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xf0(%rbp)
leaq -0xec(%rbp), %rsi
movl -0x14(%rbp), %eax
movl %eax, -0xec(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xb260
movl (%rax), %eax
incl %eax
movl %eax, -0xf4(%rbp)
movb $0x0, -0x111(%rbp)
leaq -0x112(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x4650
movq -0x1c8(%rbp), %rcx
leaq -0x110(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
leaq -0x111(%rbp), %rdx
callq 0x9390
jmp 0x836d
leaq -0x112(%rbp), %rdi
callq 0x4420
jmp 0x837b
testb $0x1, -0x66(%rbp)
je 0x83a3
jmp 0x888e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x112(%rbp), %rdi
callq 0x4420
jmp 0x88af
jmp 0x83a5
leaq -0xe8(%rbp), %rax
movq %rax, -0x120(%rbp)
movl $0x0, -0x118(%rbp)
movl -0x118(%rbp), %eax
cmpq $0x10, %rax
jae 0x83ef
movq -0x120(%rbp), %rax
movl -0x118(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x118(%rbp)
jmp 0x83bd
jmp 0x83f1
jmp 0x83f3
movl -0xf0(%rbp), %ecx
movl %ecx, %edx
addl $0x3f, %edx
testl %ecx, %ecx
movl %ecx, %eax
cmovsl %edx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movb %cl, %sil
sarl $0x6, %eax
cltq
movq -0xe8(%rbp,%rax,8), %rcx
movb %sil, %dl
btsq %rdx, %rcx
movq %rcx, -0xe8(%rbp,%rax,8)
movl -0xec(%rbp), %ecx
movl %ecx, %edx
addl $0x3f, %edx
testl %ecx, %ecx
movl %ecx, %eax
cmovsl %edx, %eax
movl %eax, %edx
andl $-0x40, %edx
subl %edx, %ecx
movb %cl, %sil
sarl $0x6, %eax
cltq
movq -0xe8(%rbp,%rax,8), %rcx
movb %sil, %dl
btsq %rdx, %rcx
movq %rcx, -0xe8(%rbp,%rax,8)
movl -0xf4(%rbp), %edi
xorl %eax, %eax
movl %eax, %r8d
leaq -0xe8(%rbp), %rsi
movq %r8, %rdx
movq %r8, %rcx
callq 0x43a0
movl %eax, -0x1cc(%rbp)
jmp 0x8488
movq -0x1b0(%rbp), %rax
movl -0x1cc(%rbp), %ecx
movl %ecx, 0x2b0(%rax)
cmpl $-0x1, 0x2b0(%rax)
jne 0x8589
movb $0x1, -0x142(%rbp)
movl $0x28, %edi
callq 0x41b0
movq %rax, -0x1e0(%rbp)
leaq -0x141(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x4650
movq -0x1d8(%rbp), %rdx
leaq 0x10d7b(%rip), %rsi     # 0x1925c
leaq -0x140(%rbp), %rdi
callq 0x4bf0
jmp 0x84ef
movq -0x1e0(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x9240
jmp 0x8504
movq -0x1e0(%rbp), %rdi
movb $0x0, -0x142(%rbp)
leaq 0x1b7df(%rip), %rsi     # 0x23cf8
leaq 0xd90(%rip), %rdx       # 0x92b0
callq 0x4610
jmp 0x88b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x88a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x8561
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x140(%rbp), %rdi
callq 0x4250
leaq -0x141(%rbp), %rdi
callq 0x4420
testb $0x1, -0x142(%rbp)
jne 0x8578
jmp 0x8584
movq -0x1e0(%rbp), %rdi
callq 0x42d0
jmp 0x88a3
movl $0x0, -0x148(%rbp)
cmpl $0x2, -0x148(%rbp)
jge 0x8889
movslq -0x148(%rbp), %rax
movl -0xf0(%rbp,%rax,4), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xe8(%rbp,%rax,8), %rax
movq %rax, -0x1e8(%rbp)
movslq -0x148(%rbp), %rax
movl -0xf0(%rbp,%rax,4), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1e8(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x8873
movslq -0x148(%rbp), %rax
movl -0xf0(%rbp,%rax,4), %eax
movl %eax, -0x1f4(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x9410
movl -0x1f4(%rbp), %edi
movq %rax, %rsi
movl $0xffff, %edx           # imm = 0xFFFF
xorl %ecx, %ecx
callq 0x40f0
movq %rax, -0x1f0(%rbp)
jmp 0x863c
movq -0x1f0(%rbp), %rax
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x8720
movb $0x1, -0x16a(%rbp)
movl $0x28, %edi
callq 0x41b0
movq %rax, -0x208(%rbp)
leaq -0x169(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x4650
movq -0x200(%rbp), %rdx
leaq 0x10bf1(%rip), %rsi     # 0x1927a
leaq -0x168(%rbp), %rdi
callq 0x4bf0
jmp 0x8697
movq -0x208(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x9240
jmp 0x86ac
movq -0x208(%rbp), %rdi
movb $0x0, -0x16a(%rbp)
leaq 0x1b637(%rip), %rsi     # 0x23cf8
leaq 0xbe8(%rip), %rdx       # 0x92b0
callq 0x4610
jmp 0x88b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x86f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x4250
leaq -0x169(%rbp), %rdi
callq 0x4420
testb $0x1, -0x16a(%rbp)
jne 0x870f
jmp 0x871b
movq -0x208(%rbp), %rdi
callq 0x42d0
jmp 0x88a3
cmpl $0x0, -0x2c(%rbp)
jne 0x872f
movb $0x1, -0x66(%rbp)
jmp 0x8889
jmp 0x8731
movslq -0x2c(%rbp), %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x9430
movq -0x220(%rbp), %rdi
movb $0x0, (%rax)
movl -0x148(%rbp), %ecx
movl $0x1, %eax
subl %ecx, %eax
cltq
movl -0xf0(%rbp,%rax,4), %eax
movl %eax, -0x214(%rbp)
callq 0x9410
movl -0x214(%rbp), %edi
movq %rax, %rsi
movslq -0x2c(%rbp), %rdx
xorl %ecx, %ecx
callq 0x41c0
movq %rax, -0x210(%rbp)
jmp 0x8790
movq -0x210(%rbp), %rax
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x8871
movb $0x1, -0x192(%rbp)
movl $0x28, %edi
callq 0x41b0
movq %rax, -0x230(%rbp)
leaq -0x191(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x4650
movq -0x228(%rbp), %rdx
leaq 0x10abe(%rip), %rsi     # 0x1929b
leaq -0x190(%rbp), %rdi
callq 0x4bf0
jmp 0x87eb
movq -0x230(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x9240
jmp 0x8800
movq -0x230(%rbp), %rdi
movb $0x0, -0x192(%rbp)
leaq 0x1b4e3(%rip), %rsi     # 0x23cf8
leaq 0xa94(%rip), %rdx       # 0x92b0
callq 0x4610
jmp 0x88b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x884c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x4250
leaq -0x191(%rbp), %rdi
callq 0x4420
testb $0x1, -0x192(%rbp)
jne 0x8863
jmp 0x886f
movq -0x230(%rbp), %rdi
callq 0x42d0
jmp 0x88a3
jmp 0x8873
jmp 0x8875
movl -0x148(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x148(%rbp)
jmp 0x8593
jmp 0x8379
leaq -0x110(%rbp), %rdi
callq 0x6130
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
leaq -0x110(%rbp), %rdi
callq 0x6130
movq -0x60(%rbp), %rdi
callq 0x4640
nopl (%rax,%rax)