// File: /workspace/github_c_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r15
leaq 0x110(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x68a4
movq %r14, %rdi
movq %r13, %rsi
callq 0x6c32
movq %rax, %rdi
callq 0x6f5d
leaq 0x89c1(%rip), %rsi      # 0xc4a8
movq %rbp, %rdi
callq 0x2160
testq %rax, %rax
je 0x3b0b
leaq 0xe0(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0xa234
testl %eax, %eax
je 0x3b2e
leaq 0x8738(%rip), %rdx      # 0xc24a
movq %r13, %rdi
movl $0x4, %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x6e50
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4233
leaq 0x8683(%rip), %rdi      # 0xc1b8
leaq 0x8685(%rip), %rsi      # 0xc1c1
xorl %edx, %edx
callq 0xafff
callq 0x2410
callq 0x2400
callq 0x2420
cmpl $-0x1, %eax
je 0x3be6
leaq 0x8678(%rip), %rdi      # 0xc1da
callq 0xada5
movq 0xd46a(%rip), %rbx      # 0x10fd8
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x20a0
leaq 0x78(%rsp), %r14
movq %r15, 0x20(%rsp)
leaq 0x28(%rsp), %r15
movl $0x98, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2060
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x58(%r15)
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
callq 0x8600
testl %eax, %eax
sete -0x8(%r12)
je 0x3c06
leaq 0x861e(%rip), %rdx      # 0xc1eb
movq %r13, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x6e50
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x41fb
leaq 0x85d7(%rip), %rdx      # 0xc1c4
movq %r13, %rdi
movl $0x4, %esi
xorl %eax, %eax
callq 0x6e50
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x4221
leaq 0x8660(%rip), %rsi      # 0xc26d
leaq 0x110(%rsp), %rdi
callq 0x7c95
testq %rax, %rax
movq %r13, 0xd8(%rsp)
movq %rbp, 0xd0(%rsp)
je 0x3cac
movq %rax, %r14
movapd 0x8536(%rip), %xmm0     # 0xc170
movupd %xmm0, 0xa8(%rsp)
movl $0x1, %edi
movl $0x1, %esi
callq 0xad43
movq %rax, 0x90(%rsp)
movq $0x0, 0x98(%rsp)
movq 0x10(%r14), %rax
movq %rax, 0xa0(%rsp)
leaq 0x110(%rsp), %r14
movq %r14, %rdi
callq 0x6c25
movq %rax, 0xb8(%rsp)
movb $0x1, 0x88(%rsp)
movq %r14, %rdi
callq 0x6c25
movq %rax, %rdi
callq 0xa99b
movq %r14, %rdi
callq 0x6bad
jmp 0x3cc2
leaq 0x854d(%rip), %rdx      # 0xc200
movq %r13, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x6e50
movq $0x0, 0x10(%rsp)
xorl %edi, %edi
callq 0x2180
callq 0x2080
movq (%rbx), %rdi
movl $0x1, %esi
callq 0x2140
leaq 0xf8(%rsp), %rbp
movq %rbp, %rdi
movl $0x1e, %esi
callq 0xa8c0
leaq 0x84a0(%rip), %r13      # 0xc1a0
movq %rbp, %rdi
callq 0xa8e6
cmpb $0x0, 0x28(%rsp)
jne 0x3da0
callq 0xada6
movq 0xd2a9(%rip), %rax      # 0x10fc8
movl (%rax), %eax
cmpl $0x29, %eax
movl $0x29, %ecx
cmovll %eax, %ecx
movl %ecx, 0x80(%rsp)
movq 0xd266(%rip), %rdx      # 0x10fa0
movl (%rdx), %edx
movl %edx, 0x84(%rsp)
subl %ecx, %eax
movl %eax, 0xb0(%rsp)
movl %edx, 0xb4(%rsp)
movl %ecx, 0xa8(%rsp)
cmpb $0x1, 0x88(%rsp)
jne 0x3d8c
movq 0x90(%rsp), %rdi
callq 0x28e4
movl 0xb0(%rsp), %edi
movl 0xb4(%rsp), %esi
callq 0xad43
movq %rax, 0x90(%rsp)
cmpb $0x1, 0x30(%rsp)
jne 0x3d9b
movq %r12, %rdi
callq 0x8aef
movb $0x1, 0x28(%rsp)
cmpb $0x1, 0x88(%rsp)
jne 0x3f38
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x83bb(%rip), %xmm1     # 0xc180
mulsd %xmm1, %xmm0
addsd 0x8347(%rip), %xmm0     # 0xc118
callq 0x2520
mulsd 0x83aa(%rip), %xmm0     # 0xc188
subsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
mulsd 0x838e(%rip), %xmm0     # 0xc180
callq 0x2190
movapd %xmm0, %xmm1
movsd 0x82ed(%rip), %xmm0     # 0xc0f0
addsd %xmm0, %xmm1
divsd 0x8381(%rip), %xmm1     # 0xc190
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x2190
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x2520
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x3a4e
movq %rax, %xmm0
movq 0x81f8(%rip), %xmm1     # 0xc060
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x81fc(%rip), %xmm1     # 0xc070
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x8264(%rip), %xmm1     # 0xc0f0
addsd 0xc0(%rsp), %xmm1
movapd %xmm1, 0xc0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x3a52
movq %rax, %xmm0
punpckldq 0x81a8(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x81b0(%rip), %xmm0     # 0xc070
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x821c(%rip), %xmm1     # 0xc0f0
addsd 0x8(%rsp), %xmm1
movq 0x90(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movaps 0xc0(%rsp), %xmm0
movsd 0x18(%rsp), %xmm2
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2aa6
movq 0x90(%rsp), %rdi
movq 0xb8(%rsp), %rdx
leaq 0xa8(%rsp), %rsi
callq 0xac72
movsd 0x98(%rsp), %xmm0
addsd 0x8269(%rip), %xmm0     # 0xc198
movsd %xmm0, 0x98(%rsp)
cmpb $0x1, 0x30(%rsp)
jne 0x3f47
movq %r12, %rdi
callq 0x8af4
movq (%rbx), %rdi
callq 0x2220
movq (%rbx), %rdi
callq 0x24f0
cmpl $0x101, %eax            # imm = 0x101
jle 0x3f97
cmpl $0x14f, %eax            # imm = 0x14F
jg 0x3fde
leal -0x102(%rax), %ecx
cmpl $0x5, %ecx
ja 0x4159
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %r12, %rdi
callq 0x8d6e
movq %r12, %rdi
callq 0x8a83
testb %al, %al
jne 0x3f7e
jmp 0x3d00
cmpl $0x63, %eax
jg 0x3fc1
cmpl $0x1f, %eax
jle 0x4057
cmpl $0x20, %eax
je 0x4009
cmpl $0x47, %eax
je 0x4147
cmpl $0x61, %eax
je 0x4065
jmp 0x4159
cmpl $0x72, %eax
jle 0x4037
cmpl $0x73, %eax
je 0x4009
cmpl $0x77, %eax
je 0x3ff7
cmpl $0x78, %eax
jne 0x4159
jmp 0x41f6
cmpl $0x156, %eax            # imm = 0x156
jg 0x401b
cmpl $0x150, %eax            # imm = 0x150
je 0x4009
cmpl $0x151, %eax            # imm = 0x151
jne 0x4159
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x88ed
jmp 0x3d00
movq %r12, %rdi
movl $0x1, %esi
callq 0x88ed
jmp 0x3d00
cmpl $0x157, %eax            # imm = 0x157
je 0x4082
cmpl $0x19a, %eax            # imm = 0x19A
jne 0x4159
movb $0x0, 0x28(%rsp)
jmp 0x3d00
cmpl $0x64, %eax
je 0x4082
cmpl $0x67, %eax
jne 0x4159
movq %r12, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0x88ed
jmp 0x3d00
cmpl $0xa, %eax
je 0x4082
cmpl $0x1b, %eax
jne 0x4159
movq %r12, %rdi
callq 0x8a83
testb %al, %al
je 0x3d00
movq %r12, %rdi
callq 0x8d6e
jmp 0x3d00
movq %r12, %rdi
callq 0x8d6e
movq %r12, %rdi
callq 0x8986
cmpl $0x2, %eax
jne 0x3d00
movq %r12, %rdi
callq 0x88c8
testq %rax, %rax
je 0x3d00
movq %rax, %r14
movq 0x18(%rax), %r15
testq %r15, %r15
je 0x3d00
movq %r15, %rdi
leaq 0x815c(%rip), %rsi      # 0xc222
callq 0x2380
testl %eax, %eax
je 0x41f6
leaq 0x110(%rsp), %rdi
movq %r15, %rsi
callq 0x7bb9
movq %rax, %r15
testq %rax, %rax
je 0x4103
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0xa3c1
testb %al, %al
je 0x4192
movq 0x18(%r14), %rdx
movq 0x20(%rsp), %rdi
leaq 0xe0(%rsp), %r13
movq %r13, %rsi
movq %rbp, %rcx
movq 0xd8(%rsp), %r14
movq %r14, %r8
callq 0x8e38
movq %r12, %rdi
testl %eax, %eax
je 0x41a8
leaq 0x8100(%rip), %rsi      # 0xc238
callq 0x8a7a
callq 0x2170
jmp 0x41e2
movq %r12, %rdi
movl $0x3e7, %esi            # imm = 0x3E7
callq 0x88ed
jmp 0x3d00
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x3d00
leal -0x31(%rax), %r14d
cmpl $0x30, %eax
movl $0x9, %eax
cmovel %eax, %r14d
movq %r12, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0x88ed
movq %r12, %rdi
movl %r14d, %esi
callq 0x88ed
jmp 0x3d00
movq %r12, %rdi
leaq 0x808f(%rip), %rsi      # 0xc22b
callq 0x8a7a
callq 0x2170
jmp 0x41e9
callq 0x8d6e
movq %r13, %rdi
movq 0xd0(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x4254
movq %rax, 0x10(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x6c25
movq %rax, %rdi
callq 0xa99b
movb $0x0, 0x28(%rsp)
leaq 0x7fb7(%rip), %r13      # 0xc1a0
movq %r15, %rdi
callq 0x2570
jmp 0x3d00
movq 0x10(%rsp), %rbx
cmpb $0x1, 0x88(%rsp)
jne 0x4212
movq 0x90(%rsp), %rdi
callq 0x28e4
cmpb $0x1, 0x30(%rsp)
jne 0x4221
movq %r12, %rdi
callq 0x8d7e
callq 0x2360
leaq 0xe0(%rsp), %rdi
callq 0xa513
leaq 0x110(%rsp), %rdi
callq 0x6c41
movl %ebx, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq