// File: /workspace/github_c_makefile_star2_O1/TurkeyMcMac[P]ts3d/src/do-ts3d-game.c
// Function: do_ts3d_game
int do_ts3d_game(const char *data_dir, const char *state_file,
	struct logger *log)
{
	int ret = -1;
	struct loader ldr;
	loader_init(&ldr, data_dir);
	logger_free(loader_set_logger(&ldr, log));
	struct save_state save;
	if (load_save_state(&save, state_file, log)) goto error_save_state;
	// ncurses reads ESCDELAY and waits that many ms after an ESC key press.
	// This here is lowered from "1000":
	try_setenv("ESCDELAY", STRINGIFY(FRAME_DELAY), 0);
	initscr();
	cbreak();
	if (start_color() == ERR) {
		logger_printf(log, LOGGER_ERROR, "start_color() failed\n");
		goto error_color;
	}
	set_application_title("Thing Shooter 3D");
	timeout(0); // No delay for key presses.
	// The screen state including the menu and title screensaver:
	struct screen_state screen_state = SCREEN_STATE_PARTIAL_INITIALIZER;
	if (load_menu_state(&screen_state.menu, data_dir, log)) {
		logger_printf(log, LOGGER_ERROR, "Failed to load menu\n");
		goto error_menu;
	}
	if (load_title_state(&screen_state.title, &ldr)) {
		logger_printf(log, LOGGER_WARNING,
			"Failed to load title screensaver\n");
	} else {
		color_map_apply(loader_color_map(&ldr));
		loader_print_summary(&ldr);
	}
	ret = 0; // Things are successful by default when exiting after this.
	curs_set(0); // Hide the cursor.
	noecho(); // Hide input characters.
	keypad(stdscr, TRUE); // Automatically detect arrow keys and so on.
	struct ticker timer; // The ticker used throughout the game.
	ticker_init(&timer, FRAME_DELAY);
	int key; // Input key.
	for (;;) {
		// Alias for the menu, to make the code easier to read:
		struct menu *menu = &screen_state.menu.menu;
		// The selected menu item (used later):
		struct menu_item *selected;
		// The map prerequisite found (used later):
		char *prereq;
		tick(&timer);
		do_screen_tick(&screen_state);
		switch (key = getch()) {
		case 'd':
		case '\n':
		case KEY_ENTER:
		case KEY_RIGHT:
			menu_clear_message(menu);
			if (menu_enter(menu) == ACTION_TAG) {
				selected = menu_get_selected(menu);
				if (!selected || !selected->tag) break;
				if (!strcmp(selected->tag, "act/quit"))
					goto end;
				// By default, a tag loads a map by its name:
				prereq = map_prereq(&ldr, selected->tag);
				if (prereq
				 && !save_state_is_complete(&save, prereq)) {
					menu_set_message(menu, "Level locked");
					beep();
				} else if (play_level(data_dir, &save,
					selected->tag, &timer, log))
				{
					menu_set_message(menu,
						"Error loading map");
					beep();
				} else {
					menu_clear_message(menu);
					// Save the progress to the disk:
					ret = write_save_state(&save,
						state_file, log, menu);
					// Reset colors:
					color_map_apply(loader_color_map(&ldr));
					// Resize the screen state in case the
					// screen changed size during the level:
					screen_state.sized = false;
				}
				free(prereq);
			}
			break;
		case 'a':
		case ESC:
		case KEY_LEFT:
			// Leave submenu.
			if (menu_escape(menu)) menu_clear_message(menu);
			break;
		case 'w':
		case KEY_UP:
		case KEY_BACKSPACE:
		case KEY_SR:
			// Scroll up.
			menu_scroll(menu, -1);
			break;
		case 's':
		case ' ':
		case KEY_DOWN:
		case KEY_SF:
			// Scroll down.
			menu_scroll(menu, 1);
			break;
		case 'g':
			// Scroll to beginning.
			menu_scroll(menu, -999);
			break;
		case 'G':
			// Scroll to end.
			menu_scroll(menu, 999);
			break;
		case KEY_HOME:
			// Return to root menu.
			while (menu_escape(menu)) {
				menu_clear_message(menu);
			}
			break;
		case 'x':
			// Quit shortcut.
			goto end;
		case KEY_RESIZE:
			screen_state.sized = false;
			break;
		default:
			if (key >= '0' && key <= '9') {
				// Goto nth menu item.
				int to = key == '0' ? 9 : key - '0' - 1;
				menu_scroll(menu, -999);
				menu_scroll(menu, to);
			}
			break;
		}
	}
end:
error_menu:
	destroy_screen_state(&screen_state);
error_color:
	endwin();
	save_state_destroy(&save);
error_save_state:
	loader_free(&ldr);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r13
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x68d4
movq %r15, %rdi
movq %rbx, %rsi
callq 0x6c62
movq %rax, %rdi
callq 0x6fa4
leaq 0x89d5(%rip), %rsi      # 0xc438
movq %r14, %rdi
callq 0x2160
testq %rax, %rax
je 0x3a87
leaq 0xe8(%rsp), %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0xa330
testl %eax, %eax
je 0x3aaa
leaq 0x874c(%rip), %rdx      # 0xc1da
movq %rbx, %rdi
movl $0x4, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x6e97
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3bb9
leaq 0x8697(%rip), %rdi      # 0xc148
leaq 0x8699(%rip), %rsi      # 0xc151
xorl %edx, %edx
callq 0xb124
callq 0x2410
callq 0x2400
callq 0x2420
cmpl $-0x1, %eax
je 0x3b8c
leaq 0x868c(%rip), %rdi      # 0xc16a
callq 0xaebb
movq 0xd4ee(%rip), %rax      # 0x10fd8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x20a0
leaq 0x68(%rsp), %r15
leaq 0x18(%rsp), %r12
movl $0x98, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x2060
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x58(%r12)
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x8698
testl %eax, %eax
sete -0x8(%r12)
je 0x3bda
leaq 0x8634(%rip), %rdx      # 0xc17b
movq %rbx, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x6e97
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, 0x8(%rsp)
cmpb $0x1, 0x78(%rsp)
jne 0x3b74
movq 0x80(%rsp), %rdi
callq 0x28fb
cmpb $0x1, 0x20(%rsp)
movq 0x8(%rsp), %rbx
jne 0x3ba7
leaq 0x28(%rsp), %rdi
callq 0x8e44
jmp 0x3ba7
leaq 0x85c1(%rip), %rdx      # 0xc154
movq %rbx, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x6e97
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x2360
leaq 0xe8(%rsp), %rdi
callq 0xa610
leaq 0x100(%rsp), %rdi
callq 0x6c71
movl %ebx, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x861c(%rip), %rsi      # 0xc1fd
leaq 0x100(%rsp), %rdi
callq 0x7ce9
testq %rax, %rax
movq %r13, 0xc8(%rsp)
movq %r14, 0xc0(%rsp)
je 0x3c7e
movq %rax, %r12
movapd 0x84f2(%rip), %xmm0     # 0xc100
movupd %xmm0, 0x98(%rsp)
movl $0x1, %edi
movl $0x1, %esi
callq 0xae59
movq %rax, 0x80(%rsp)
movq $0x0, 0x88(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x90(%rsp)
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x6c55
movq %rax, 0xa8(%rsp)
movb $0x1, 0x78(%rsp)
movq %r15, %rdi
callq 0x6c55
movq %rax, %rdi
callq 0xaab4
movq %r15, %rdi
callq 0x6bdd
jmp 0x3c94
leaq 0x850b(%rip), %rdx      # 0xc190
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x6e97
movq $0x0, 0x8(%rsp)
xorl %edi, %edi
callq 0x2180
callq 0x2080
movq 0xd328(%rip), %r15      # 0x10fd8
movq (%r15), %rdi
movl $0x1, %esi
callq 0x2140
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movl $0x1e, %esi
callq 0xa9d8
leaq 0x28(%rsp), %rbp
leaq 0x98(%rsp), %r14
movq %r13, %rdi
callq 0xa9fe
cmpb $0x0, 0x18(%rsp)
jne 0x3d76
callq 0xaebc
movq 0xd2ca(%rip), %rax      # 0x10fc8
movl (%rax), %eax
cmpl $0x29, %eax
movl $0x29, %ecx
cmovll %eax, %ecx
movl %ecx, 0x70(%rsp)
movq 0xd28a(%rip), %rdx      # 0x10fa0
movl (%rdx), %edx
movl %edx, 0x74(%rsp)
subl %ecx, %eax
movl %eax, 0xa0(%rsp)
movl %edx, 0xa4(%rsp)
movl %ecx, 0x98(%rsp)
cmpb $0x1, 0x78(%rsp)
jne 0x3d62
movq 0x80(%rsp), %rdi
callq 0x28fb
movl 0xa0(%rsp), %edi
movl 0xa4(%rsp), %esi
callq 0xae59
movq %rax, 0x80(%rsp)
cmpb $0x1, 0x20(%rsp)
jne 0x3d71
movq %rbp, %rdi
callq 0x8bb5
movb $0x1, 0x18(%rsp)
cmpb $0x1, 0x78(%rsp)
jne 0x3eff
movsd 0x88(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x8379(%rip), %xmm1     # 0xc110
mulsd %xmm1, %xmm0
addsd 0x82ad(%rip), %xmm0     # 0xc050
callq 0x2520
mulsd 0x8368(%rip), %xmm0     # 0xc118
subsd (%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
mulsd 0x834d(%rip), %xmm0     # 0xc110
callq 0x2190
movapd %xmm0, %xmm1
movsd 0x8254(%rip), %xmm0     # 0xc028
addsd %xmm0, %xmm1
divsd 0x8340(%rip), %xmm1     # 0xc120
mulsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2190
mulsd (%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2520
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movq 0x90(%rsp), %rdi
callq 0x39cb
movq %rax, %xmm0
movq 0x823b(%rip), %xmm1     # 0xc070
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x823f(%rip), %xmm1     # 0xc080
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x81cf(%rip), %xmm1     # 0xc028
addsd 0xb0(%rsp), %xmm1
movapd %xmm1, 0xb0(%rsp)
movq 0x90(%rsp), %rdi
callq 0x39cf
movq %rax, %xmm0
punpckldq 0x81eb(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x81f3(%rip), %xmm0     # 0xc080
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x8187(%rip), %xmm1     # 0xc028
addsd (%rsp), %xmm1
movq 0x80(%rsp), %rdi
movq 0x90(%rsp), %rsi
movaps 0xb0(%rsp), %xmm0
movsd 0x10(%rsp), %xmm2
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2a2e
movq 0x80(%rsp), %rdi
movq 0xa8(%rsp), %rdx
movq %r14, %rsi
callq 0xad88
movsd 0x88(%rsp), %xmm0
addsd 0x8232(%rip), %xmm0     # 0xc128
movsd %xmm0, 0x88(%rsp)
cmpb $0x1, 0x20(%rsp)
jne 0x3f0e
movq %rbp, %rdi
callq 0x8bba
movq (%r15), %rdi
callq 0x2220
movq (%r15), %rdi
callq 0x24f0
cmpl $0x101, %eax            # imm = 0x101
jg 0x3f53
cmpl $0x63, %eax
jg 0x3f9a
cmpl $0x1f, %eax
jle 0x4015
cmpl $0x20, %eax
je 0x3fde
cmpl $0x47, %eax
je 0x411b
cmpl $0x61, %eax
je 0x4023
jmp 0x4128
cmpl $0x14f, %eax            # imm = 0x14F
jg 0x3fbb
leal -0x102(%rax), %ecx
cmpl $0x5, %ecx
ja 0x4128
leaq 0x81c0(%rip), %rdx      # 0xc130
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %rbp, %rdi
callq 0x8b4b
movb $0x1, %r12b
testb %al, %al
je 0x3fee
movq %rbp, %rdi
callq 0x8e34
movq %rbp, %rdi
callq 0x8b4b
jmp 0x3f84
cmpl $0x72, %eax
jle 0x403c
cmpl $0x73, %eax
je 0x3fde
cmpl $0x77, %eax
je 0x3fd4
cmpl $0x78, %eax
jne 0x4128
xorl %r12d, %r12d
jmp 0x3fee
cmpl $0x156, %eax            # imm = 0x156
jg 0x3ffc
cmpl $0x150, %eax            # imm = 0x150
je 0x3fde
cmpl $0x151, %eax            # imm = 0x151
jne 0x4128
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
jmp 0x3fe6
movq %rbp, %rdi
movl $0x1, %esi
callq 0x89b8
movb $0x1, %r12b
testb %r12b, %r12b
jne 0x3cdf
jmp 0x3b60
cmpl $0x157, %eax            # imm = 0x157
je 0x4054
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x4128
movb $0x0, 0x18(%rsp)
jmp 0x3feb
cmpl $0xa, %eax
je 0x4054
cmpl $0x1b, %eax
jne 0x4128
movq %rbp, %rdi
callq 0x8b4b
movb $0x1, %r12b
testb %al, %al
je 0x3fee
movq %rbp, %rdi
callq 0x8e34
jmp 0x3fee
cmpl $0x64, %eax
je 0x4054
cmpl $0x67, %eax
jne 0x4128
movq %rbp, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
jmp 0x3fe6
movq %rbp, %rdi
callq 0x8e34
movq %rbp, %rdi
callq 0x8a4e
movb $0x1, %r12b
cmpl $0x2, %eax
jne 0x3fee
movq %rbp, %rdi
callq 0x8993
testq %rax, %rax
je 0x4175
movq %rax, %r13
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x416e
movq %r15, %rdi
leaq 0x811b(%rip), %rsi      # 0xc1b2
callq 0x2380
testl %eax, %eax
je 0x416b
leaq 0x100(%rsp), %rdi
movq %r15, %rsi
callq 0x7c0d
testq %rax, %rax
leaq 0xe8(%rsp), %r15
movq %rax, (%rsp)
je 0x40d8
movq %r15, %rdi
movq %rax, %rsi
callq 0xa4b3
testb %al, %al
je 0x418a
movq 0x18(%r13), %rdx
movq 0xc8(%rsp), %rdi
movq %r15, %rsi
leaq 0xd0(%rsp), %r13
movq %r13, %rcx
movq %rbx, %r8
callq 0x8f00
movq %rbp, %rdi
testl %eax, %eax
je 0x41af
leaq 0x80bc(%rip), %rsi      # 0xc1c8
callq 0x8b42
callq 0x2170
jmp 0x41e9
movq %rbp, %rdi
movl $0x3e7, %esi            # imm = 0x3E7
jmp 0x3fe6
leal -0x30(%rax), %ecx
movb $0x1, %r12b
cmpl $0x9, %ecx
ja 0x3fee
leal -0x31(%rax), %r15d
cmpl $0x30, %eax
movl $0x9, %eax
cmovel %eax, %r15d
movq %rbp, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0x89b8
movq %rbp, %rdi
movl %r15d, %esi
movq 0xce77(%rip), %r15      # 0x10fd8
callq 0x89b8
jmp 0x3fee
xorl %r12d, %r12d
movq 0xce63(%rip), %r15      # 0x10fd8
leaq 0xd0(%rsp), %r13
leaq 0x98(%rsp), %r14
jmp 0x3fee
movq %rbp, %rdi
leaq 0x8027(%rip), %rsi      # 0xc1bb
callq 0x8b42
callq 0x2170
movq 0xce33(%rip), %r15      # 0x10fd8
leaq 0xd0(%rsp), %r13
jmp 0x41f0
callq 0x8e34
movq %r15, %rdi
movq 0xc0(%rsp), %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0x4206
movq %rax, 0x8(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x6c55
movq %rax, %rdi
callq 0xaab4
movb $0x0, 0x18(%rsp)
movq 0xcde8(%rip), %r15      # 0x10fd8
leaq 0x98(%rsp), %r14
movq (%rsp), %rdi
callq 0x2570
jmp 0x3fee