// File: nightmeng[P]net/examples/http/httptest.cpp
// Function: main
int main(){
	socket sock;

#if 1
	if(!sock.connect("www.baidu.com", 80)){
		std::cout << "connected failed" << std::endl;
		return -1;
	}
	std::cout << "connected successfully" << std::endl;
	
	std::string get = "GET / HTTP/1.1\r\n"
		"Host: www.baidu.com\r\n"
		"Connection: keep-alive\r\n\r\n";

	char buff[2049] = {0};
	sock.async_write(get.c_str(), get.size(), [&sock, &buff](int ec, int t){
		sock.async_read(buff, 2048, std::bind(handle_read, std::placeholders::_1,
				std::placeholders::_2, std::ref(sock)));
	});
#else
	char buff[2049] = {0};
	sock.async_connect("www.baidu.com", 80, [&sock, &buff](int ec){
		if(ec == 0){
			std::cout << "connect successfully" << std::endl;

			std::string get = "GET / HTTP/1.1\r\n"
				"Host: www.baidu.com\r\n"
				"Connection: keep-alive\r\n\r\n";

			sock.async_write(get.c_str(), get.size(), [&sock, &buff](int ec, int t){
				std::cout << "write ec = " << ec << ", t = " << t << std::endl;
				sock.async_read(buff, 2048, std::bind(handle_read, std::placeholders::_1,
					std::placeholders::_2, std::ref(sock)));
			});
		}
		else{
			std::cout << "connect failed" << std::endl;
		}
	});
#endif

	char c;
	std::cin >> c;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb20, %rsp            # imm = 0xB20
movl $0x0, -0x4(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x4bc0
leaq -0x251(%rbp), %rdi
movq %rdi, -0xad0(%rbp)
callq 0x3340
movq -0xad0(%rbp), %rdx
leaq 0x10991(%rip), %rsi     # 0x14030
leaq -0x250(%rbp), %rdi
callq 0x3260
jmp 0x36ad
leaq -0x230(%rbp), %rdi
leaq -0x250(%rbp), %rsi
movl $0x50, %edx
callq 0x55c0
movb %al, -0xad1(%rbp)
jmp 0x36cd
movb -0xad1(%rbp), %al
xorb $-0x1, %al
movb %al, -0xad2(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3140
leaq -0x251(%rbp), %rdi
callq 0x3220
movb -0xad2(%rbp), %al
testb $0x1, %al
jne 0x3702
jmp 0x37a3
movq 0x1d8bf(%rip), %rdi     # 0x20fc8
leaq 0x1092e(%rip), %rsi     # 0x1403e
callq 0x31f0
movq %rax, -0xae0(%rbp)
jmp 0x371e
movq -0xae0(%rbp), %rdi
movq 0x1d874(%rip), %rsi     # 0x20fa0
callq 0x3210
jmp 0x3733
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x268(%rbp)
jmp 0x3959
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
jmp 0x377b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3140
leaq -0x251(%rbp), %rdi
callq 0x3220
jmp 0x3971
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
jmp 0x3971
movq 0x1d81e(%rip), %rdi     # 0x20fc8
leaq 0x1089e(%rip), %rsi     # 0x1404f
callq 0x31f0
movq %rax, -0xae8(%rbp)
jmp 0x37bf
movq -0xae8(%rbp), %rdi
movq 0x1d7d3(%rip), %rsi     # 0x20fa0
callq 0x3210
jmp 0x37d4
leaq -0x289(%rbp), %rdi
movq %rdi, -0xaf0(%rbp)
callq 0x3340
movq -0xaf0(%rbp), %rdx
leaq 0x10871(%rip), %rsi     # 0x14066
leaq -0x288(%rbp), %rdi
callq 0x3260
jmp 0x3803
leaq -0x289(%rbp), %rdi
callq 0x3220
leaq -0xa90(%rbp), %rdi
movq %rdi, -0xb18(%rbp)
xorl %esi, %esi
movl $0x801, %edx            # imm = 0x801
callq 0x3120
leaq -0x288(%rbp), %rdi
movq %rdi, -0xb20(%rbp)
callq 0x30e0
movq -0xb20(%rbp), %rdi
movq %rax, -0xb08(%rbp)
callq 0x3160
movq %rax, %rcx
movq -0xb18(%rbp), %rax
movq %rcx, -0xb00(%rbp)
leaq -0x230(%rbp), %rcx
movq %rcx, -0xb10(%rbp)
movq %rcx, -0xac0(%rbp)
movq %rax, -0xab8(%rbp)
leaq -0xab0(%rbp), %rdi
movq %rdi, -0xaf8(%rbp)
leaq -0xac0(%rbp), %rsi
callq 0x3990
movq -0xb10(%rbp), %rdi
movq -0xb08(%rbp), %rsi
movq -0xb00(%rbp), %rdx
movq -0xaf8(%rbp), %rcx
callq 0x64c0
jmp 0x38b9
leaq -0xab0(%rbp), %rdi
callq 0x3f50
movq 0x1d70c(%rip), %rdi     # 0x20fd8
leaq -0xac1(%rbp), %rsi
callq 0x3240
jmp 0x38da
movl $0x0, -0x4(%rbp)
movl $0x1, -0x268(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x3140
jmp 0x3959
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
leaq -0x289(%rbp), %rdi
callq 0x3220
jmp 0x3971
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
leaq -0xab0(%rbp), %rdi
callq 0x3f50
jmp 0x394b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x3140
jmp 0x3971
leaq -0x230(%rbp), %rdi
callq 0x53b0
movl -0x4(%rbp), %eax
addq $0xb20, %rsp            # imm = 0xB20
popq %rbp
retq
leaq -0x230(%rbp), %rdi
callq 0x53b0
movq -0x260(%rbp), %rdi
callq 0x3330
nopl (%rax)