// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x68, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
cmpl $-0x1, 0x8(%rdi)
jne 0x57c6
movq %rbx, %rdi
callq 0x7dd8
movq %rbx, %rdi
callq 0x7c8e
movzwl %r15w, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x56ca
testb %al, %al
je 0x5842
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rax
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq $0x0, 0x20(%rax)
leaq -0x30(%rbp), %r14
movq %rax, (%r14)
xorl %r15d, %r15d
movb %r15b, 0x8(%r14)
movq %r14, %rdi
callq 0x6e2a
movb $0x1, 0x8(%r14)
movl $0x0, 0x1f4(%rbx)
movb %r15b, -0x19(%rbp)
movl 0x8(%rbx), %edi
movq 0x1e0(%rbx), %rsi
movl $0x10, %edx
callq 0x31b0
cmpl $-0x1, %eax
je 0x5849
movl $0x2, 0x1f4(%rbx)
movb $0x1, %bl
jmp 0x5958
xorl %ebx, %ebx
jmp 0x5961
callq 0x3040
cmpl $0x73, (%rax)
jne 0x594c
movl $0x1, 0x1f4(%rbx)
leaq 0x170(%rbx), %r14
movq %r14, %rdi
callq 0x32a0
testl %eax, %eax
jne 0x596e
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movl $0x28, %edi
callq 0x3200
leaq 0x1f8(%rbx), %r15
leaq 0x1c8(%rbx), %rdi
leaq 0x11c(%rip), %rcx       # 0x59be
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
leaq -0x19(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq %r15, 0x18(%rax)
movq %rbx, 0x20(%rax)
leaq -0x50(%rbp), %rdx
movq %rax, (%rdx)
leaq 0x1649(%rip), %rax      # 0x6f14
movq %rax, 0x18(%rdx)
leaq 0x1668(%rip), %rax      # 0x6f3e
movq %rax, 0x10(%rdx)
movq %rdi, %rsi
callq 0x6ea2
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x58f9
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq %r14, %rdi
callq 0x3180
movl $0x10, %edi
cmpb $0x0, 0x1f0(%rbx)
jne 0x591d
movl $0x98, %edi
cmpb $0x1, 0x1f1(%rbx)
jne 0x5925
addq %rbx, %rdi
callq 0x82aa
cmpb $0x0, -0x19(%rbp)
jne 0x5940
leaq -0x30(%rbp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x30a0
cmpb $0x1, -0x19(%rbp)
jne 0x592f
cmpl $0x2, 0x1f4(%rbx)
sete %bl
jmp 0x5958
movl $0x3, 0x1f4(%rbx)
xorl %ebx, %ebx
leaq -0x30(%rbp), %rdi
callq 0x6ac0
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x3140
jmp 0x5993
movq %rax, %rbx
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x599e
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x599e
movq %rax, %rdi
callq 0x3a3f
movq %rax, %rbx
movq %r14, %rdi
callq 0x3180
jmp 0x59ad
jmp 0x59aa
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x6ac0
movq %rbx, %rdi
callq 0x3330