// File: /workspace/github_cpp_makefile_star2_O3/lotuskip[P]yuxtapa/server/server.cpp
// Function: main
int main(int argc, char *argv[])
{
#ifdef MAPTEST
	unsigned short size;
	if(argc < 2 || (size = atoi(argv[1])) < MIN_MAP_SIZE || size > MAX_MAP_SIZE)
	{
		std::cout << "Try: ./yuxtapa_sv [mapsize, " << MIN_MAP_SIZE
			<< "--" << MAX_MAP_SIZE << ']' << std::endl;
		std::cout << "Third argument, if present, is a filename to save the map as "
			"(it will be saved in the working directory)." << std::endl;
		return 1;
	}
	srandom(time(NULL));
	Map a_map(size, 0, 10); // will generate and print the map
	if(argc > 2)
	{
		if(!a_map.save_to_file(argv[2]))
			std::cout << "Failed to save the map as \'" << argv[2] << "\'!" << std::endl;
	}
#else
	setlocale(LC_ALL, "");
	string str = "";
	if(argc >= 2)
		str = argv[1];
	
	if(str == "--help" || str == "-h")
	{
		std::cout << "Recognised options are: \'-v\' (print version) and \'"
			<< print_stats_handle << "\' (print player stats)" << std::endl;
		return 0;
	}
	if(str == "-v" || str == "--version")
	{
		std::cout << PACKAGE << " server v." << VERSION << std::endl;
		return 0;
	}

	using namespace Config;
	if(str == print_stats_handle)
	{
		init_known_players(true);
		output_stats();
		return 0;
	}
	// else going to start the server for real
	std::cout << "(__)ux+apa " << VERSION << std::endl;
	std::cout << " ._)" << std::endl << std::endl;
	if(!str.empty())
		std::cout << "Uknown argument \"" << str << '\"' << std::endl;
	reg_sig_handlers();
	read_config();
	init_known_players(false);

	if(!Network::startup())
		return 1;

	srandom(time(NULL));

	msTimer test_clock;
	time(&last_time);
	time_t last_minute = time(NULL);
	interm_over = last_time + int_settings[IS_INTERM_SECS];
	tm *loctime;
	bool no_traffic;
	while(!any_signs())
	{
		no_traffic = Network::receive_n_handle();

		if(intermission)
		{
			Game::init_game();
			if(time(NULL) >= interm_over) // time to end intermission
				goto_next_map();
			else if(no_traffic)
				usleep(500000); // half a second
		}
		else // not intermission
		{
			// high-res (ms-scale) time checks: check if a turn has passed
			if(test_clock.update() - last_clock > int_settings[IS_TURNMS])
			{
				// Lag check:
				turns_lagged += (test_clock - last_clock)/int_settings[IS_TURNMS]-1;
				last_clock.update();
				if(Game::process_turn())
					goto_intermission("");
			}
			else if(no_traffic) // micro-hibernation
				usleep(int_settings[IS_TURNMS]*10); // sleep for 1/100th of a turn (2.5ms on normal config)
		}

		// low-res (second scale) time checks:
		if(time(NULL) - last_time > 5) // 5 seconds have passed
		{
			last_time = time(NULL);
			// Send clock update every 5 seconds
			if(intermission)
				Network::clocksync(interm_over - time(NULL), 0);
			else
				Game::send_times(cur_players.end());
		}
		if(time(NULL) - last_minute > 60) // a minute has passed
		{
			last_minute = time(NULL);
			loctime = localtime(&last_minute);
			if(!(loctime->tm_min % 30)) // usage check every 30 minutes
			{
				if(check_date_change(loctime->tm_mday)) // the day has changed
					usage_report();
				else
					usage_update();
			}
		} // minute passed
	} // for eva

	std::cout << "Quitting..." << std::endl;
	Network::shutdown();
	timed_log("Shutdown");
	usage_report();
	to_log("---------------------------------");
	store_known_players();
#endif
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x1e91a(%rip), %r15     # 0x2c2f0
movl $0x6, %edi
movq %r15, %rsi
callq 0x4380
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x68(%rbp), %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0xe01c
cmpl $0x2, %r14d
jl 0xda27
movq 0x8(%rbx), %rbx
leaq -0x68(%rbp), %r14
movq 0x8(%r14), %r15
movq %rbx, %rdi
callq 0x41a0
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbx, %rcx
movq %rax, %r8
callq 0x4630
leaq 0x1e6a2(%rip), %rsi     # 0x2c0d0
leaq -0x68(%rbp), %rdi
callq 0x4120
testl %eax, %eax
je 0xde01
leaq 0x1e691(%rip), %rsi     # 0x2c0d7
leaq -0x68(%rbp), %rdi
callq 0x4120
testl %eax, %eax
je 0xde01
leaq 0x1e6c6(%rip), %rsi     # 0x2c124
leaq -0x68(%rbp), %rdi
callq 0x4120
testl %eax, %eax
je 0xde60
leaq 0x1e6b1(%rip), %rsi     # 0x2c127
leaq -0x68(%rbp), %rdi
callq 0x4120
testl %eax, %eax
je 0xde60
movq -0x60(%rbp), %rdx
cmpq 0x33006(%rip), %rdx     # 0x40a98
jne 0xdab5
testq %rdx, %rdx
je 0xdfb6
movq 0x32fec(%rip), %rsi     # 0x40a90
movq -0x68(%rbp), %rdi
callq 0x4400
testl %eax, %eax
je 0xdfb6
movq 0x324fc(%rip), %rdi     # 0x3ffb8
leaq 0x1e684(%rip), %rsi     # 0x2c147
movl $0xb, %edx
callq 0x4460
movq 0x324e4(%rip), %rbx     # 0x3ffb8
leaq 0x1e669(%rip), %rsi     # 0x2c144
movl $0x2, %edx
movq %rbx, %rdi
callq 0x4460
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x43d0
movsbl %al, %esi
movq 0x324b2(%rip), %rdi     # 0x3ffb8
callq 0x4040
movq %rax, %rdi
callq 0x4250
movq 0x3249e(%rip), %rbx     # 0x3ffb8
leaq 0x1e632(%rip), %rsi     # 0x2c153
movl $0x4, %edx
movq %rbx, %rdi
callq 0x4460
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x43d0
movsbl %al, %esi
movq 0x3246c(%rip), %rdi     # 0x3ffb8
callq 0x4040
movq %rax, %rdi
callq 0x4250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x43d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4250
cmpq $0x0, -0x60(%rbp)
je 0xdbd2
movq 0x32427(%rip), %rdi     # 0x3ffb8
leaq 0x1e5c0(%rip), %rsi     # 0x2c158
movl $0x11, %edx
callq 0x4460
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq 0x32407(%rip), %rdi     # 0x3ffb8
callq 0x4460
leaq -0x48(%rbp), %rsi
movb $0x22, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x4460
movq %rax, %rdi
callq 0x41b0
callq 0x12334
callq 0x1131a
xorl %edi, %edi
callq 0x12649
callq 0xe0bc
movl $0x1, %ebx
testb %al, %al
je 0xded4
xorl %edi, %edi
callq 0x43b0
movl %eax, %edi
callq 0x40d0
leaq 0x32ea6(%rip), %rdi     # 0x40ab0
callq 0x43b0
xorl %edi, %edi
callq 0x43b0
leaq -0x70(%rbp), %rcx
movq %rax, (%rcx)
leaq 0x34c7c(%rip), %r15     # 0x428a0
movl 0x18(%r15), %eax
addq 0x32e81(%rip), %rax     # 0x40ab0
movq %rax, 0x32e82(%rip)     # 0x40ab8
leaq 0x32e83(%rip), %r14     # 0x40ac0
movl $0x8888888, %ebx        # imm = 0x8888888
leaq -0x80(%rbp), %r12
callq 0x12324
testb %al, %al
jne 0xdefe
callq 0xe420
movl %eax, %r13d
cmpb $0x1, 0x327a6(%rip)     # 0x40408
jne 0xdc90
callq 0x1a9a4
xorl %edi, %edi
callq 0x43b0
cmpq 0x32e41(%rip), %rax     # 0x40ab8
jge 0xdd22
movl $0x7a120, %edi          # imm = 0x7A120
testb %r13b, %r13b
jne 0xdd4b
jmp 0xdd50
movq %r12, %rdi
callq 0x2aebc
movq %rax, %rdi
movq %r14, %rsi
callq 0x2aed6
movl 0x14(%r15), %ecx
cmpl %ecx, %eax
jbe 0xdd41
movq %r12, %rdi
movq %r14, %rsi
callq 0x2aed6
xorl %edx, %edx
divl 0x14(%r15)
addw 0x32e09(%rip), %ax      # 0x40ad0
decl %eax
movw %ax, 0x32e00(%rip)      # 0x40ad0
movq %r14, %rdi
callq 0x2aebc
callq 0x1b2d6
testb %al, %al
je 0xdd50
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %r13
movq %r13, %rdi
leaq 0x1e5f9(%rip), %rdx     # 0x2c2f0
movq %rdx, %rsi
callq 0xe01c
movq %r13, %rdi
callq 0xd802
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xdd50
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43f0
jmp 0xdd50
movb $0x0, 0x326df(%rip)     # 0x40408
movw $0x0, 0x32d9e(%rip)     # 0x40ad0
callq 0x1af3d
movq %r14, %rdi
callq 0x2aebc
jmp 0xdd50
testb %r13b, %r13b
je 0xdd50
addl %ecx, %ecx
leal (%rcx,%rcx,4), %edi
callq 0x45b0
xorl %edi, %edi
callq 0x43b0
subq 0x32d52(%rip), %rax     # 0x40ab0
cmpq $0x6, %rax
jl 0xdda5
xorl %edi, %edi
callq 0x43b0
movq %rax, 0x32d3e(%rip)     # 0x40ab0
cmpb $0x1, 0x3268f(%rip)     # 0x40408
jne 0xdd99
movl 0x32d36(%rip), %r13d    # 0x40ab8
xorl %edi, %edi
callq 0x43b0
subl %eax, %r13d
movzwl %r13w, %edi
xorl %esi, %esi
callq 0xf73d
jmp 0xdda5
leaq 0x35320(%rip), %rdi     # 0x430c0
callq 0x1b056
xorl %edi, %edi
callq 0x43b0
subq -0x70(%rbp), %rax
cmpq $0x3d, %rax
jl 0xdc46
xorl %edi, %edi
callq 0x43b0
movq %rax, -0x70(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x42b0
imull $0xeeeeeeef, 0x4(%rax), %ecx # imm = 0xEEEEEEEF
addl %ebx, %ecx
rorl %ecx
cmpl %ebx, %ecx
ja 0xdc46
movl 0xc(%rax), %edi
callq 0x1525c
testb %al, %al
je 0xddf7
callq 0x13dc6
jmp 0xdc46
callq 0x13ab3
jmp 0xdc46
movq 0x321b0(%rip), %rdi     # 0x3ffb8
leaq 0x1e2cb(%rip), %rsi     # 0x2c0da
movl $0x32, %edx
callq 0x4460
movq 0x32c70(%rip), %rsi     # 0x40a90
movq 0x32c71(%rip), %rdx     # 0x40a98
movq 0x3218a(%rip), %rdi     # 0x3ffb8
callq 0x4460
movq %rax, %rbx
leaq 0x1e2d0(%rip), %rsi     # 0x2c10d
movl $0x16, %edx
movq %rax, %rdi
callq 0x4460
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x43d0
jmp 0xdebf
movq 0x32151(%rip), %rdi     # 0x3ffb8
leaq 0x1e2c3(%rip), %rsi     # 0x2c131
movl $0x7, %edx
callq 0x4460
movq 0x32139(%rip), %rdi     # 0x3ffb8
leaq 0x1e2b3(%rip), %rsi     # 0x2c139
movl $0xa, %edx
callq 0x4460
movq 0x32121(%rip), %rbx     # 0x3ffb8
leaq 0x1e2a6(%rip), %rsi     # 0x2c144
movl $0x2, %edx
movq %rbx, %rdi
callq 0x4460
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x43d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4250
xorl %ebx, %ebx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xdeed
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43f0
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x320b3(%rip), %rdi     # 0x3ffb8
leaq 0x1e25e(%rip), %rsi     # 0x2c16a
movl $0xb, %edx
callq 0x4460
movq 0x3209b(%rip), %rdi     # 0x3ffb8
callq 0x41b0
callq 0xe373
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
movq %rax, (%rdi)
leaq 0x1e23d(%rip), %rsi     # 0x2c176
leaq 0x1e23e(%rip), %rdx     # 0x2c17e
callq 0xe01c
leaq -0x48(%rbp), %rdi
callq 0x15128
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xdf67
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43f0
callq 0x13dc6
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
movq %rax, (%rdi)
leaq 0x1e201(%rip), %rsi     # 0x2c17f
leaq 0x1e21b(%rip), %rdx     # 0x2c1a0
callq 0xe01c
leaq -0x48(%rbp), %rdi
callq 0x14f24
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xdfac
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43f0
callq 0x12d38
jmp 0xded2
movl $0x1, %edi
callq 0x12649
callq 0x13f23
jmp 0xded2
jmp 0xdfd2
jmp 0xdff8
jmp 0xdfd2
jmp 0xdff8
movq %rax, %rbx
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xdffb
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43f0
jmp 0xdffb
jmp 0xdff8
jmp 0xdff8
jmp 0xdff8
jmp 0xdff8
movq %rax, %rbx
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xe014
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43f0
movq %rbx, %rdi
callq 0x4740