// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
cmpl $-0x1, 0x8(%rdi)
jne 0x5377
movq %rbx, %rdi
callq 0x7b04
movq %rbx, %rdi
callq 0x79ce
movzwl %r15w, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x52a6
testb %al, %al
je 0x53d9
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
andq $0x0, 0x20(%rsi)
leaq -0x30(%rbp), %rdi
callq 0x604c
andl $0x0, 0x1f4(%rbx)
movb $0x0, -0x19(%rbp)
movl 0x8(%rbx), %edi
movq 0x1e0(%rbx), %rsi
pushq $0x10
popq %rdx
callq 0x31a0
cmpl $-0x1, %eax
je 0x53e0
movl $0x2, 0x1f4(%rbx)
movb $0x1, %bl
jmp 0x54ad
xorl %ebx, %ebx
jmp 0x54b6
callq 0x3030
cmpl $0x73, (%rax)
jne 0x5468
movl $0x1, 0x1f4(%rbx)
leaq 0x170(%rbx), %r15
movq %r15, %rdi
callq 0x41ca
leaq 0x1f8(%rbx), %r14
leaq 0xe7(%rip), %rax        # 0x54f8
leaq -0xa8(%rbp), %rsi
movq %rax, (%rsi)
andq $0x0, 0x8(%rsi)
leaq -0x19(%rbp), %rax
movq %rax, 0x10(%rsi)
movq %r14, 0x18(%rsi)
movq %rbx, 0x20(%rsi)
leaq -0x50(%rbp), %rdi
callq 0x607c
leaq 0x1c8(%rbx), %rdi
leaq -0x50(%rbp), %rsi
callq 0x606c
leaq -0x50(%rbp), %rdi
callq 0x3862
movq %r15, %rdi
callq 0x3180
cmpb $0x0, 0x1f0(%rbx)
je 0x5476
pushq $0x10
popq %rdi
jmp 0x5484
movl $0x3, 0x1f4(%rbx)
xorl %ebx, %ebx
jmp 0x54ad
movl $0x98, %edi
cmpb $0x1, 0x1f1(%rbx)
jne 0x548c
addq %rbx, %rdi
callq 0x7f4e
leaq -0x30(%rbp), %r15
cmpb $0x0, -0x19(%rbp)
jne 0x54a3
movq %r14, %rdi
movq %r15, %rsi
callq 0x3090
jmp 0x5490
cmpl $0x2, 0x1f4(%rbx)
sete %bl
leaq -0x30(%rbp), %rdi
callq 0x60c4
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x3862
jmp 0x54d7
movq %rax, %rbx
movq %r15, %rdi
callq 0x3180
jmp 0x54e6
jmp 0x54e3
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x60c4
movq %rbx, %rdi
callq 0x3310
nop