// File: /workspace/github_cpp_makefile_star2_O2/lotuskip[P]yuxtapa/client/network.cpp
// Function: Network::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool Network::connect(string &errors)
{
	// determine ip and port
	string sip = Config::get_server_ip(); // this is just what was read from the config; could be garbage!
	string port = "", ip = "";
	size_t i;
	if((i = sip.rfind(':')) != string::npos && i > 1 && i != sip.size()-1)
	{
		port = sip.substr(i+1);
		ip = sip.substr(0, i);
		if(ip[0] == '[') // this would indicate IPv6
		{
			ip.erase(0,1);
			if(ip.empty())
			{
				errors = "Not a valid address: " + sip;
				return false;
			}
			ip.erase(ip.size()-1,1); // erase assumed ']'
		}
	}
	else
	{
		errors = "Not a valid address: " + sip;
		return false;
	}

	// create the socket
	struct addrinfo hints;
	int rv;
	bzero(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	// get server info:
	if((rv = getaddrinfo(ip.c_str(), port.c_str(), &hints, &servinfo)))
	{
		errors = string("Error in getaddrinfo: ") + gai_strerror(rv);
		return false;
	}
	// initialize the_serv and make the socket based on its protocol info:
	for(the_serv = servinfo; the_serv; the_serv = the_serv->ai_next)
	{
		if((s_me = socket(the_serv->ai_family, the_serv->ai_socktype, the_serv->ai_protocol)) != -1)
			break;
	}
	if(!the_serv)
	{
		errors = "Failed to create socket!";
		freeaddrinfo(servinfo);
		return false;
	}
	// set socket to nonblock mode:
	fcntl(s_me, F_SETFL, O_NONBLOCK);

	last_sent_axn.update(); // must init this at some point

	add_msg("[Connecting to " + sip + "...]", 15);
	// construct a hello message:
	send_buffer.add((unsigned char)MID_HELLO);
	send_buffer.add(INTR_VERSION);
	add_id_info_for_server(sip);
	send_buffer.add(Config::get_nick());
	
	// send hello message (repeatedly) and wait for a reply
	int k;
	for(char j = 0; j < CONNECTION_ATTEMPTS; ++j)
	{
		if(do_send())
		{
			errors = "Problem sending connection request.";
			close(s_me);
			freeaddrinfo(servinfo);
			return false;
		}
		// Wait for the reply (the timeout on the getch() call will delay):
		for(k = 0; k < REPLY_WAIT_MS/GETCH_TIMEOUT; ++k)
		{
			if(do_receive() != -1)
			{
				// got *some* message
				unsigned char mid = recv_buffer.read_ch();
				switch(mid)
				{
				case MID_HELLO:
					Base::print_str("(identified)", 7, 0, 5, STAT_WIN, false);
					return true;
				case MID_HELLO_FULL:
				case MID_HELLO_BANNED:
				case MID_HELLO_VERSION:
				case MID_HELLO_STEAL:
					errors = hello_errors[mid - MID_HELLO_FULL];
					close(s_me);
					freeaddrinfo(servinfo);
					return false;
				case MID_HELLO_NEWID:
				{
					cur_id = recv_buffer.read_sh();
					string passw;
					recv_buffer.read_str(passw);
					store_id_for_server(cur_id, sip, passw);
					Base::print_str("(new id)", 7, 0, 5, STAT_WIN, false);
					return true;
				}
				default: break; // ignore other messages
				}
			} // received something
			if(getch() == KEYCODE_INT)
			{
				errors = "Aborted by user.";
				return false;
			}
		} // listen for connections loop
	} // send connection requests loop
	errors = "Did not get a reply from the server.";
	close(s_me);
	freeaddrinfo(servinfo);
	return false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x538, %rsp            # imm = 0x538
movq %rdi, %rbx
callq 0xb9a5
leaq -0x60(%rbp), %rdi
movq %rax, %rsi
callq 0x4520
leaq 0x48ca(%rip), %rsi      # 0xe489
leaq -0x120(%rbp), %rdi
leaq -0x328(%rbp), %rdx
callq 0x69bc
leaq 0x48b0(%rip), %rsi      # 0xe489
leaq -0xc0(%rbp), %rdi
leaq -0x328(%rbp), %rdx
callq 0x69bc
leaq -0x60(%rbp), %rdi
pushq $0x3a
popq %rsi
pushq $-0x1
popq %rdx
callq 0x4710
movq %rax, %r14
addq $-0x2, %rax
cmpq $-0x4, %rax
ja 0x9c14
movq -0x58(%rbp), %rax
decq %rax
cmpq %rax, %r14
jne 0x9c7c
leaq 0x4b3d(%rip), %rsi      # 0xe758
leaq -0x328(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xab81
leaq -0x328(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4480
movq %r14, %rdi
callq 0x4748
xorl %ebx, %ebx
leaq -0xc0(%rbp), %rdi
callq 0x4748
leaq -0x120(%rbp), %rdi
callq 0x4748
leaq -0x60(%rbp), %rdi
callq 0x4748
movl %ebx, %eax
addq $0x538, %rsp            # imm = 0x538
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1(%r14), %rdx
leaq -0x328(%rbp), %rdi
leaq -0x60(%rbp), %rsi
pushq $-0x1
popq %rcx
callq 0x43c0
leaq -0x120(%rbp), %rdi
leaq -0x328(%rbp), %r15
movq %r15, %rsi
callq 0x4480
movq %r15, %rdi
callq 0x4748
leaq -0x328(%rbp), %rdi
leaq -0x60(%rbp), %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x43c0
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x4480
movq %r15, %rdi
callq 0x4748
movq (%r14), %rdi
cmpb $0x5b, (%rdi)
jne 0x9d22
leaq -0xc0(%rbp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
callq 0x40e0
movq -0xb8(%rbp), %rsi
testq %rsi, %rsi
je 0x9dee
decq %rsi
leaq -0xc0(%rbp), %rdi
pushq $0x1
popq %rdx
callq 0x40e0
movq -0xc0(%rbp), %rdi
xorps %xmm0, %xmm0
leaq -0x560(%rbp), %rdx
movaps %xmm0, (%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movl $0x2, 0x8(%rdx)
movq -0x120(%rbp), %rsi
leaq 0xe4e4(%rip), %r14      # 0x18230
movq %r14, %rcx
callq 0x44a0
movl %eax, %r15d
testl %eax, %eax
je 0x9dda
leaq 0x4a0c(%rip), %rsi      # 0xe76e
leaq -0x528(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x69bc
movl %r15d, %edi
callq 0x4530
leaq -0x328(%rbp), %rdi
leaq -0x528(%rbp), %rsi
movq %rax, %rdx
callq 0x6a23
leaq -0x328(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4480
movq %r14, %rdi
callq 0x4748
leaq -0x528(%rbp), %rdi
jmp 0x9c40
movl 0x4(%rax), %edi
movl 0x8(%rax), %esi
movl 0xc(%rax), %edx
callq 0x40f0
movl %eax, 0xe476(%rip)      # 0x18240
movq 0xe467(%rip), %r14      # 0x18238
cmpl $-0x1, %eax
jne 0x9e0a
addq $0x28, %r14
movq (%r14), %rax
movq %rax, 0xe454(%rip)      # 0x18238
testq %rax, %rax
jne 0x9db6
jmp 0x9f78
leaq 0x4963(%rip), %rsi      # 0xe758
leaq -0x328(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xab81
jmp 0x9c2b
testq %r14, %r14
je 0x9f78
pushq $0x4
popq %rsi
movl %eax, %edi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x4230
leaq 0xe41d(%rip), %rdi      # 0x18248
callq 0xcbf8
leaq 0x4967(%rip), %rsi      # 0xe79e
leaq -0x528(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xab81
leaq 0x4960(%rip), %rdx      # 0xe7ae
leaq -0x328(%rbp), %rdi
leaq -0x528(%rbp), %rsi
callq 0x6a23
leaq -0x328(%rbp), %rdi
pushq $0xf
popq %rsi
callq 0x86ae
leaq -0x328(%rbp), %rdi
callq 0x4748
leaq -0x528(%rbp), %rdi
callq 0x4748
leaq 0xdae9(%rip), %rdi      # 0x17978
xorl %esi, %esi
callq 0xcc5c
leaq 0xdadb(%rip), %rdi      # 0x17978
pushq $0x3
popq %rsi
callq 0xcc70
orw $-0x1, 0xe3ab(%rip)     # 0x18258
callq 0xba08
leaq 0xe2cf(%rip), %rdx      # 0x18188
leaq -0x528(%rbp), %rdi
movq %rax, %rsi
callq 0xabf7
movq -0x528(%rbp), %rsi
leaq -0x328(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x4680
leaq -0x528(%rbp), %r14
movq %r14, %rdi
callq 0x4748
leaq -0x518(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq -0x328(%rbp), %r15
leaq -0x80(%rbp), %r13
leaq -0x60(%rbp), %r12
movq -0x328(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x308(%rbp,%rax)
jne 0x9f98
movq %r15, %rdi
movq %r14, %rsi
callq 0x44b0
movq %r15, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4570
movq %r15, %rdi
movq %r13, %rsi
callq 0x44b0
movq %r14, %rdi
movq %r12, %rsi
callq 0x84f3
testb %al, %al
je 0x9f1f
movzwl -0xa0(%rbp), %eax
movw %ax, 0xe2e2(%rip)       # 0x18258
jmp 0x9f9f
leaq 0x4806(%rip), %rsi      # 0xe785
movq %rbx, %rdi
callq 0x46d0
movq 0xe2a2(%rip), %rdi      # 0x18230
callq 0x44f0
jmp 0x9c45
movzwl 0xe2b9(%rip), %eax      # 0x18258
movzwl %ax, %esi
leaq 0xd9cf(%rip), %rdi      # 0x17978
callq 0xcc70
cmpw $-0x1, 0xe2a2(%rip)     # 0x18258
je 0x9fc8
leaq 0xd9b9(%rip), %rdi      # 0x17978
leaq -0x80(%rbp), %rsi
callq 0xcc9a
leaq -0x80(%rbp), %rdi
callq 0x4748
leaq -0x528(%rbp), %rdi
callq 0x4748
leaq -0x328(%rbp), %rdi
callq 0x4110
callq 0xb9b2
leaq 0xd983(%rip), %rdi      # 0x17978
movq %rax, %rsi
callq 0xcc9a
xorl %r15d, %r15d
movq 0xbfb9(%rip), %r13      # 0x15fc0
leaq 0xd15a(%rip), %r14      # 0x17168
cmpb $0x3, %r15b
je 0xa35c
callq 0xa4fc
testb %al, %al
jne 0xa37b
pushq $0x33
popq %r12
decl %r12d
je 0xa069
callq 0xa534
cmpw $-0x1, %ax
je 0xa059
movq %r14, %rdi
callq 0xcd50
movzbl %al, %eax
leal -0x10(%rax), %ecx
cmpl $0x4, %ecx
jb 0xa06e
testl %eax, %eax
je 0xa249
cmpl $0x14, %eax
je 0xa0af
movq (%r13), %rdi
callq 0x44c0
cmpl $0x3, %eax
jne 0xa029
jmp 0xa09b
incb %r15b
jmp 0xa00e
shll $0x5, %eax
leaq 0xe138(%rip), %rcx      # 0x181b0
leaq (%rax,%rcx), %rsi
addq $-0x200, %rsi           # imm = 0xFE00
movq %rbx, %rdi
callq 0x4220
movl 0xe1af(%rip), %edi      # 0x18240
callq 0x4700
jmp 0x9f87
leaq 0x474b(%rip), %rsi      # 0xe7ed
movq %rbx, %rdi
callq 0x46d0
jmp 0x9c45
leaq 0xd0b2(%rip), %rdi      # 0x17168
callq 0xcd38
movw %ax, 0xe196(%rip)       # 0x18258
leaq -0xf0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0xd08c(%rip), %rdi      # 0x17168
leaq -0x100(%rbp), %rsi
callq 0xcd62
movzwl 0xe169(%rip), %ebx      # 0x18258
leaq -0xa0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
pushq $0x20
popq %rdx
callq 0xac58
leaq -0xe0(%rbp), %rdi
movl %ebx, %esi
callq 0xd516
leaq -0x528(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x764e
leaq -0x328(%rbp), %rdi
leaq -0x528(%rbp), %rsi
pushq $0x20
popq %rdx
callq 0x76bf
leaq -0x80(%rbp), %rdi
leaq -0x328(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0xac2f
leaq -0x328(%rbp), %rdi
callq 0x4748
leaq -0x528(%rbp), %rdi
callq 0x4748
leaq -0xe0(%rbp), %rdi
callq 0x4748
leaq -0xa0(%rbp), %rdi
callq 0x4748
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
callq 0xba08
leaq 0xdfe9(%rip), %rdx      # 0x18188
leaq -0x528(%rbp), %rdi
movq %rax, %rsi
callq 0xabf7
movq -0x528(%rbp), %rsi
leaq -0x328(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x4680
leaq -0x528(%rbp), %rdi
callq 0x4748
leaq -0x90(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movb $0x1, %r12b
leaq -0x328(%rbp), %rbx
leaq -0xa0(%rbp), %r14
leaq -0x60(%rbp), %r15
leaq -0x40(%rbp), %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4090
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xa26a
testb $0x1, %r12b
je 0xa23c
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4720
testq %rax, %rax
jne 0xa23c
movq %r14, %rdi
leaq -0x80(%rbp), %rsi
callq 0x4220
xorl %r12d, %r12d
movq %r13, %rdi
movq %r14, %rsi
callq 0x8312
jmp 0xa1fc
movb $0x1, %bl
leaq 0x4585(%rip), %rdi      # 0xe7d7
pushq $0x7
popq %rsi
pushq $0x5
popq %rcx
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x61ca
jmp 0x9c47
leaq -0x328(%rbp), %rdi
callq 0x40a0
testb $0x1, %r12b
je 0xa289
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x8312
callq 0xba08
leaq 0xdef3(%rip), %rdx      # 0x18188
leaq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0xabf7
movq -0xe0(%rbp), %rsi
leaq -0x528(%rbp), %rdi
pushq $0x10
popq %rdx
callq 0x4400
leaq -0xe0(%rbp), %rdi
callq 0x4748
leaq -0x528(%rbp), %rbx
leaq -0x40(%rbp), %r14
movq -0x40(%rbp), %rsi
cmpq -0x38(%rbp), %rsi
je 0xa2f9
movq %rbx, %rdi
callq 0x4340
movq %rax, %rdi
callq 0x41f0
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x834e
jmp 0xa2d1
leaq -0x528(%rbp), %rdi
callq 0x4560
leaq -0xa0(%rbp), %rdi
callq 0x4748
leaq -0x328(%rbp), %rdi
callq 0x4110
leaq -0x40(%rbp), %rdi
callq 0x69f8
leaq -0x80(%rbp), %rdi
callq 0x4748
leaq 0x44ae(%rip), %rdi      # 0xe7e4
pushq $0x7
popq %rsi
pushq $0x5
popq %rcx
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x61ca
leaq -0x100(%rbp), %rdi
callq 0x4748
movb $0x1, %bl
jmp 0x9c47
leaq 0x449b(%rip), %rsi      # 0xe7fe
movq %rbx, %rdi
callq 0x46d0
movl 0xdecf(%rip), %edi      # 0x18240
callq 0x4700
jmp 0x9f87
leaq 0x4431(%rip), %rsi      # 0xe7b3
movq %rbx, %rdi
callq 0x46d0
movl 0xdeb0(%rip), %edi      # 0x18240
callq 0x4700
jmp 0x9f87
jmp 0xa4a0
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x4748
jmp 0xa45e
movq %rax, %rbx
leaq -0x528(%rbp), %rdi
callq 0x4748
jmp 0xa476
movq %rax, %rbx
leaq -0x328(%rbp), %rdi
callq 0x4748
jmp 0xa3db
movq %rax, %rbx
leaq -0x528(%rbp), %rdi
callq 0x4748
jmp 0xa3ec
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x4748
jmp 0xa3fd
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
jmp 0xa483
jmp 0xa45b
jmp 0xa45b
movq %rax, %rbx
jmp 0xa476
jmp 0xa4a0
movq %rax, %rbx
jmp 0xa488
jmp 0xa43c
movq %rax, %rbx
leaq -0x328(%rbp), %rdi
callq 0x4748
jmp 0xa43f
jmp 0xa43c
jmp 0xa4a0
jmp 0xa4a0
jmp 0xa4a0
jmp 0xa4af
jmp 0xa4a0
jmp 0xa44a
jmp 0xa4a0
movq %rax, %rbx
leaq -0x528(%rbp), %rdi
jmp 0xa48f
jmp 0xa4a0
movq %rax, %rbx
leaq -0x528(%rbp), %rdi
callq 0x4560
jmp 0xa45e
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x4748
leaq -0x328(%rbp), %rdi
callq 0x4110
leaq -0x40(%rbp), %rdi
callq 0x69f8
leaq -0x80(%rbp), %rdi
callq 0x4748
leaq -0x100(%rbp), %rdi
callq 0x4748
jmp 0xa4d3
jmp 0xa4a0
jmp 0xa4a0
jmp 0xa4a0
jmp 0xa4a0
jmp 0xa4a0
movq %rax, %rbx
jmp 0xa4d3
movq %rax, %rbx
jmp 0xa4df
movq %rax, %rbx
jmp 0xa4eb
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x4748
leaq -0x528(%rbp), %rdi
callq 0x4748
leaq -0x328(%rbp), %rdi
callq 0x4110
leaq -0xc0(%rbp), %rdi
callq 0x4748
leaq -0x120(%rbp), %rdi
callq 0x4748
leaq -0x60(%rbp), %rdi
callq 0x4748
movq %rbx, %rdi
callq 0x4650