// File: lotuskip[P]yuxtapa/server/players.cpp
// Function: player_hello(unsigned short, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, sockaddr_storage&, bool)
unsigned short player_hello(const unsigned short id, string &passw,
	const string &nick, sockaddr_storage &sas, const bool is_bot)
{
	/* First check if this client is already registered, because the same
	 * client might send multiple hellos having lost the reply. We don't want
	 * to first accept them and then kick them out because they didn't hear
	 * our first reply. */
	list<Player>::const_iterator it = find_pl_by_sas(sas);
	if(it != cur_players.end())
	{
		// we have already replied; figure out what:
		if(it->stats_i->ID == id)
			return JOIN_OK_ID_OK;
		// else we assigned them a new id already
		passw.assign(it->stats_i->password);
		return it->stats_i->ID;
	}

	// Check if IP is banned:
	for(list<sockaddr_storage>::const_iterator sit = banned_ips.begin();
		sit != banned_ips.end(); ++sit)
	{
		if(equal_ip(&sas, &(*sit)))
			return IP_BANNED;
	}

	// Check server being at full capacity:
	if(cur_players.size() >= Config::int_settings[Config::IS_MAXPLAYERS])
	{
		// There are too many players, but some of them might be bots.
		if(!drop_a_bot()) // no bots to drop!
			return SERVER_FULL;
		// else a bot was dropped and there is room again, proceed:
	}

	if(id == 0xFFFF) // a new player
		return add_and_register_player(nick, passw, sas, is_bot);

	// Check if ID is already playing:
	for(it = cur_players.begin(); it != cur_players.end(); ++it)
	{
		if(it->stats_i->ID == id)
			return ID_STEAL;
	}

	// Check if we actually know this player like they claim:
	list<PlayerStats>::iterator si;
	for(si = known_players.begin(); si != known_players.end(); ++si)
	{
		if(si->ID == id) // it is a returning customer
		{
			if(passw != si->password) // received wrong password!
				return ID_STEAL;
			// else all is good:
			register_player(nick, sas, si, is_bot);
			return JOIN_OK_ID_OK;
		}
	}
	// If here, we have forgotten about this player, most likely
	return add_and_register_player(nick, passw, sas, is_bot);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movb %r8b, %al
movw %di, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
andb $0x1, %al
movb %al, -0x21(%rbp)
movq -0x20(%rbp), %rdi
callq 0x23230
movq %rax, -0x30(%rbp)
leaq 0x6d3a9(%rip), %rdi     # 0x90230
callq 0x189f0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x18a20
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x24f10
testb $0x1, %al
jne 0x22eb3
jmp 0x22f3b
leaq -0x30(%rbp), %rdi
callq 0x24f40
movq %rax, %rdi
addq $0x80, %rdi
callq 0x1b390
movzwl (%rax), %eax
movzwl -0x4(%rbp), %ecx
cmpl %ecx, %eax
jne 0x22ee1
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
jmp 0x23211
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x24f40
movq %rax, %rdi
addq $0x80, %rdi
callq 0x1b390
movq -0xd8(%rbp), %rdi
movq %rax, %rsi
addq $0x2, %rsi
callq 0x59c0
leaq -0x30(%rbp), %rdi
callq 0x24f40
movq %rax, %rdi
addq $0x80, %rdi
callq 0x1b390
movw (%rax), %ax
movw %ax, -0x2(%rbp)
jmp 0x23211
leaq 0x6d306(%rip), %rdi     # 0x90248
callq 0x24f70
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x24fa0
leaq 0x6d2e9(%rip), %rdi     # 0x90248
callq 0x24ff0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x24fa0
leaq -0x48(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x24fc0
testb $0x1, %al
jne 0x22f88
jmp 0x22fc9
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x25020
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0x232c0
testb $0x1, %al
jne 0x22fb1
jmp 0x22fbc
movw $0xfffd, -0x2(%rbp)     # imm = 0xFFFD
jmp 0x23211
jmp 0x22fbe
leaq -0x48(%rbp), %rdi
callq 0x25050
jmp 0x22f58
leaq 0x6d260(%rip), %rdi     # 0x90230
callq 0x25070
leaq 0x6ca34(%rip), %rcx     # 0x8fa10
movl (%rcx), %ecx
cmpq %rcx, %rax
jb 0x22ff9
callq 0x5d300
testb $0x1, %al
jne 0x22ff7
movw $0xfffc, -0x2(%rbp)     # imm = 0xFFFC
jmp 0x23211
jmp 0x22ff9
movzwl -0x4(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x23026
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x23360
movw %ax, -0x2(%rbp)
jmp 0x23211
leaq 0x6d203(%rip), %rdi     # 0x90230
callq 0x1b360
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x18a20
movq -0x68(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq 0x6d1de(%rip), %rdi     # 0x90230
callq 0x189f0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x18a20
leaq -0x30(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x24f10
testb $0x1, %al
jne 0x2307b
jmp 0x230b6
leaq -0x30(%rbp), %rdi
callq 0x24f40
movq %rax, %rdi
addq $0x80, %rdi
callq 0x1b390
movzwl (%rax), %eax
movzwl -0x4(%rbp), %ecx
cmpl %ecx, %eax
jne 0x230a9
movw $0xfffe, -0x2(%rbp)     # imm = 0xFFFE
jmp 0x23211
jmp 0x230ab
leaq -0x30(%rbp), %rdi
callq 0x250a0
jmp 0x2304b
leaq -0x88(%rbp), %rdi
callq 0x250c0
leaq 0x6d14f(%rip), %rdi     # 0x90218
callq 0x24e30
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq 0x6d12e(%rip), %rdi     # 0x90218
callq 0x24e90
movq %rax, -0x98(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x24e60
testb $0x1, %al
jne 0x23112
jmp 0x231f4
leaq -0x88(%rbp), %rdi
callq 0x1b390
movzwl (%rax), %eax
movzwl -0x4(%rbp), %ecx
cmpl %ecx, %eax
jne 0x231e1
movq -0x10(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x1b390
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
addq $0x2, %rsi
callq 0x250e0
testb $0x1, %al
jne 0x2315d
jmp 0x23168
movw $0xfffe, -0x2(%rbp)     # imm = 0xFFFE
jmp 0x23211
movq -0x18(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x5220
movq -0xf0(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x88(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rdx
movzbl -0x21(%rbp), %ecx
andl $0x1, %ecx
callq 0x23420
jmp 0x231ad
leaq -0xb8(%rbp), %rdi
callq 0x5a48
movw $0xffff, -0x2(%rbp)     # imm = 0xFFFF
jmp 0x23211
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x5a48
jmp 0x2321e
jmp 0x231e3
leaq -0x88(%rbp), %rdi
callq 0x24ef0
jmp 0x230e3
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movb -0x21(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x23360
movw %ax, -0x2(%rbp)
movw -0x2(%rbp), %ax
addq $0xf0, %rsp
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x5920
nopw (%rax,%rax)