// File: nightmeng[P]net/src/socket.cpp
// Function: socket::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned short)
bool socket::connect(const std::string &ip, unsigned short port){
	if(-1 == sock){
		open();
		register_epoll_req();
	}

	if(!update_addr(ip, port)){
		return false;
	}
	/*if(nullptr == addr){
		addr = std::shared_ptr<struct sockaddr_in>(new struct sockaddr_in);
	}

	addr->sin_family = AF_INET;
	addr->sin_port = htons(port);
	addr->sin_addr.s_addr = inet_addr(ip.c_str());*/

	std::mutex mutex;
	std::unique_lock<std::mutex> locker(mutex);

	connect_status = PRECONNECT;
	bool processed = false;
	if(-1 == ::connect(sock, 
				reinterpret_cast<struct sockaddr*>(addr.get()), 
				sizeof(struct sockaddr))){
		if(EINPROGRESS == errno){
			connect_status = CONNECTING;
			{
				std::lock_guard<std::mutex> locker(conn_mutex);
				conn_request.push_back(
						std::bind(&socket::sync_conn_action, this, std::ref(connect_notify), std::ref(processed)));
			}
			if(rdable){
				rd_worker.active();
			}
			else if(wrable){
				wr_worker.active();
			}

			connect_notify.wait(locker, [&processed]()->bool{return processed;});
			return CONNECTED == connect_status;
		}
		connect_status = CONNECTERR;
		return false;
	}
	connect_status = CONNECTED;
	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movw %dx, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movw %ax, -0x1a(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x100(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x8(%rcx), %eax
jne 0xabb7
movq -0x100(%rbp), %rdi
callq 0x139a0
movq -0x100(%rbp), %rdi
callq 0x13760
movq -0x100(%rbp), %rdi
movq -0x18(%rbp), %rsi
movzwl -0x1a(%rbp), %edx
callq 0xaa30
testb $0x1, %al
jne 0xabd8
movb $0x0, -0x1(%rbp)
jmp 0xae84
leaq -0x48(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0xc2d0
movq -0x110(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0xc4b0
movq -0x100(%rbp), %rdi
movl $0x0, 0x1f4(%rdi)
movb $0x0, -0x59(%rbp)
movl 0x8(%rdi), %eax
movl %eax, -0x108(%rbp)
addq $0x1e0, %rdi            # imm = 0x1E0
callq 0x68f0
movl -0x108(%rbp), %edi
movq %rax, %rsi
movl $0x10, %edx
callq 0x41c0
movl %eax, -0x104(%rbp)
jmp 0xac3d
movl -0x104(%rbp), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0xae4f
callq 0x4030
movq %rax, %rcx
movl $0x73, %eax
cmpl (%rcx), %eax
jne 0xae2e
movq -0x100(%rbp), %rsi
movl $0x1, 0x1f4(%rsi)
addq $0x170, %rsi            # imm = 0x170
leaq -0x78(%rbp), %rdi
callq 0x7330
jmp 0xac88
movq -0x100(%rbp), %rdi
movq %rdi, %rax
addq $0x1c8, %rax            # imm = 0x1C8
movq %rax, -0x118(%rbp)
leaq 0x1fa(%rip), %rax       # 0xaea0
movq %rax, -0xd0(%rbp)
movq $0x0, -0xc8(%rbp)
movq %rdi, -0xd8(%rbp)
addq $0x1f8, %rdi            # imm = 0x1F8
callq 0xc580
movq %rax, -0xe0(%rbp)
leaq -0x59(%rbp), %rdi
callq 0xc5b0
movq %rax, -0xe8(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
leaq -0xe0(%rbp), %rcx
leaq -0xe8(%rbp), %r8
callq 0xc530
jmp 0xad0c
leaq -0x98(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xc5e0
jmp 0xad21
movq -0x118(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xc4f0
jmp 0xad36
leaq -0x98(%rbp), %rdi
callq 0x6b50
leaq -0x78(%rbp), %rdi
callq 0x73e0
movq -0x100(%rbp), %rax
testb $0x1, 0x1f0(%rax)
je 0xadb4
movq -0x100(%rbp), %rdi
addq $0x10, %rdi
callq 0x144e0
jmp 0xad6d
jmp 0xaddd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xae79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xada6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x6b50
leaq -0x78(%rbp), %rdi
callq 0x73e0
jmp 0xae79
movq -0x100(%rbp), %rax
testb $0x1, 0x1f1(%rax)
je 0xaddb
movq -0x100(%rbp), %rdi
addq $0x98, %rdi
callq 0x144e0
jmp 0xadd9
jmp 0xaddb
jmp 0xaddd
movq -0x100(%rbp), %rdi
addq $0x1f8, %rdi            # imm = 0x1F8
leaq -0x59(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rdx
leaq -0x58(%rbp), %rsi
callq 0xaf30
jmp 0xae08
movq -0x100(%rbp), %rcx
movl $0x2, %eax
cmpl 0x1f4(%rcx), %eax
sete %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0xf4(%rbp)
jmp 0xae6e
movq -0x100(%rbp), %rax
movl $0x3, 0x1f4(%rax)
movb $0x0, -0x1(%rbp)
movl $0x1, -0xf4(%rbp)
jmp 0xae6e
movq -0x100(%rbp), %rax
movl $0x2, 0x1f4(%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0xf4(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xc680
jmp 0xae84
leaq -0x58(%rbp), %rdi
callq 0xc680
jmp 0xae92
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x4360
nopl (%rax,%rax)