// File: /workspace/github_cpp_makefile_star2_O3/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 0x5734
movq %rbx, %rdi
callq 0x7ce0
movq %rbx, %rdi
callq 0x7b96
movzwl %r15w, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5636
testb %al, %al
je 0x57b0
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 0x6d68
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 0x57b7
movl $0x2, 0x1f4(%rbx)
movb $0x1, %bl
jmp 0x58c3
xorl %ebx, %ebx
jmp 0x58cc
callq 0x3040
cmpl $0x73, (%rax)
jne 0x58b7
movl $0x1, 0x1f4(%rbx)
leaq 0x170(%rbx), %r14
movq %r14, %rdi
callq 0x32a0
testl %eax, %eax
jne 0x58d9
movq $0x0, -0x48(%rbp)
movl $0x28, %edi
callq 0x3200
leaq 0x1f8(%rbx), %r15
leaq 0x1c8(%rbx), %rdi
leaq 0x11d(%rip), %rcx       # 0x592a
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 0x161c(%rip), %rax      # 0x6e52
movq %rax, 0x18(%rdx)
leaq 0x163b(%rip), %rax      # 0x6e7c
movq %rax, 0x10(%rdx)
movq %rdi, %rsi
callq 0x6de0
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x5864
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 0x5888
movl $0x98, %edi
cmpb $0x1, 0x1f1(%rbx)
jne 0x5890
addq %rbx, %rdi
callq 0x819c
cmpb $0x0, -0x19(%rbp)
jne 0x58ab
leaq -0x30(%rbp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x30a0
cmpb $0x1, -0x19(%rbp)
jne 0x589a
cmpl $0x2, 0x1f4(%rbx)
sete %bl
jmp 0x58c3
movl $0x3, 0x1f4(%rbx)
xorl %ebx, %ebx
leaq -0x30(%rbp), %rdi
callq 0x6a04
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x3140
jmp 0x58fe
movq %rax, %rbx
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x5909
leaq -0x50(%rbp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
jmp 0x5909
movq %rax, %rdi
callq 0x3a01
movq %rax, %rbx
movq %r14, %rdi
callq 0x3180
jmp 0x5918
jmp 0x5915
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x6a04
movq %rbx, %rdi
callq 0x3330
nop