// File: /workspace/github_c_makefile_star2_O2/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, %r15
movq %rsi, %r13
movq %rdi, %r14
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x639c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x670d
movq %rax, %rdi
callq 0x6a11
leaq 0x7b79(%rip), %rsi      # 0xb478
movq %r13, %rdi
callq 0x2160
testq %rax, %rax
je 0x3923
leaq 0xe0(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x9a90
testl %eax, %eax
je 0x3942
leaq 0x78f0(%rip), %rdx      # 0xb21a
pushq $0x4
popq %rsi
movq %r15, %rdi
movq %r13, %rcx
xorl %eax, %eax
callq 0x691c
pushq $-0x1
popq %rbx
jmp 0x40bc
leaq 0x783f(%rip), %rdi      # 0xb188
leaq 0x7841(%rip), %rsi      # 0xb191
xorl %edx, %edx
callq 0xa75b
callq 0x2420
callq 0x2410
callq 0x2430
cmpl $-0x1, %eax
je 0x39fa
leaq 0x7834(%rip), %rdi      # 0xb1aa
callq 0xa552
movq 0xd656(%rip), %rbp      # 0x10fd8
movq (%rbp), %rdi
xorl %esi, %esi
callq 0x20a0
leaq 0x88(%rsp), %rbx
movq %r14, 0x30(%rsp)
leaq 0x38(%rsp), %r14
movl $0x98, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2060
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x58(%r14)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x7ff0
testl %eax, %eax
sete -0x8(%r12)
je 0x3a16
pushq $-0x1
popq %rbx
leaq 0x77d3(%rip), %rdx      # 0xb1bb
pushq $0x4
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x691c
jmp 0x4084
leaq 0x7793(%rip), %rdx      # 0xb194
pushq $0x4
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x691c
pushq $-0x1
popq %rbx
jmp 0x40aa
leaq 0x7820(%rip), %rsi      # 0xb23d
leaq 0x110(%rsp), %rdi
callq 0x76c0
testq %rax, %rax
movq %r15, 0x28(%rsp)
movq %r13, 0x20(%rsp)
je 0x3aae
movq %rax, %rbx
movapd 0x76fc(%rip), %xmm0     # 0xb140
movupd %xmm0, 0xb8(%rsp)
pushq $0x1
popq %rdi
movl %edi, %esi
callq 0xa4f2
movq %rax, 0xa0(%rsp)
andq $0x0, 0xa8(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0xb0(%rsp)
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
callq 0x6700
movq %rax, 0xc8(%rsp)
movb $0x1, 0x98(%rsp)
movq %rbx, %rdi
callq 0x6700
movq %rax, %rdi
callq 0xa180
movq %rbx, %rdi
callq 0x668a
jmp 0x3ac2
leaq 0x771b(%rip), %rdx      # 0xb1d0
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x691c
xorl %edi, %edi
callq 0x2180
callq 0x2080
movq (%rbp), %rdi
pushq $0x1
popq %r15
movl %r15d, %esi
callq 0x2140
leaq 0xf8(%rsp), %r13
pushq $0x1e
popq %rsi
movq %r13, %rdi
callq 0xa0ac
movl $0x0, 0x14(%rsp)
movq %r13, %rdi
callq 0xa0d2
cmpb $0x0, 0x38(%rsp)
jne 0x3b97
callq 0xa553
movq 0xd4b0(%rip), %rax      # 0x10fc8
movl (%rax), %eax
cmpl $0x29, %eax
pushq $0x29
popq %rcx
cmovll %eax, %ecx
movl %ecx, 0x90(%rsp)
movq 0xd46f(%rip), %rdx      # 0x10fa0
movl (%rdx), %edx
movl %edx, 0x94(%rsp)
subl %ecx, %eax
movl %eax, 0xc0(%rsp)
movl %edx, 0xc4(%rsp)
movl %ecx, 0xb8(%rsp)
cmpb $0x1, 0x98(%rsp)
jne 0x3b83
movq 0xa0(%rsp), %rdi
callq 0x28e5
movl 0xc0(%rsp), %edi
movl 0xc4(%rsp), %esi
callq 0xa4f2
movq %rax, 0xa0(%rsp)
cmpb $0x1, 0x40(%rsp)
jne 0x3b92
movq %r12, %rdi
callq 0x84a0
movb $0x1, 0x38(%rsp)
cmpb $0x1, 0x98(%rsp)
jne 0x3d2f
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x7594(%rip), %xmm1     # 0xb150
mulsd %xmm1, %xmm0
addsd 0x7510(%rip), %xmm0     # 0xb0d8
callq 0x2530
mulsd 0x7583(%rip), %xmm0     # 0xb158
subsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
mulsd 0x7567(%rip), %xmm0     # 0xb150
callq 0x2190
movapd %xmm0, %xmm1
movsd 0x74c6(%rip), %xmm0     # 0xb0c0
addsd %xmm0, %xmm1
divsd 0x755a(%rip), %xmm1     # 0xb160
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x2190
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x18(%rsp), %xmm0
callq 0x2530
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x37f2
movq %rax, %xmm0
movq 0x73d1(%rip), %xmm1     # 0xb030
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x73d5(%rip), %xmm1     # 0xb040
subpd %xmm1, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x743d(%rip), %xmm1     # 0xb0c0
addsd 0xd0(%rsp), %xmm1
movapd %xmm1, 0xd0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x37f6
movq %rax, %xmm0
punpckldq 0x7381(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7389(%rip), %xmm0     # 0xb040
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x73f5(%rip), %xmm1     # 0xb0c0
addsd 0x8(%rsp), %xmm1
movq 0xa0(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movaps 0xd0(%rsp), %xmm0
movsd 0x18(%rsp), %xmm2
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x29ff
movq 0xa0(%rsp), %rdi
movq 0xc8(%rsp), %rdx
leaq 0xb8(%rsp), %rsi
callq 0xa41d
movsd 0xa8(%rsp), %xmm0
addsd 0x7442(%rip), %xmm0     # 0xb168
movsd %xmm0, 0xa8(%rsp)
cmpb $0x1, 0x40(%rsp)
jne 0x3d3e
movq %r12, %rdi
callq 0x84a5
movq (%rbp), %rdi
callq 0x2220
movq (%rbp), %rdi
callq 0x2500
leal -0x102(%rax), %ecx
cmpl $0x5, %ecx
jbe 0x3e11
cmpl $0xa, %eax
je 0x3e4a
cmpl $0x1b, %eax
je 0x3f09
cmpl $0x20, %eax
je 0x3ef9
cmpl $0x47, %eax
je 0x3f26
cmpl $0x61, %eax
je 0x3f09
cmpl $0x64, %eax
je 0x3e4a
cmpl $0x67, %eax
je 0x3f38
cmpl $0x73, %eax
je 0x3ef9
cmpl $0x77, %eax
je 0x3e21
cmpl $0x150, %eax            # imm = 0x150
je 0x3ef9
cmpl $0x151, %eax            # imm = 0x151
je 0x3e21
cmpl $0x157, %eax            # imm = 0x157
je 0x3e4a
cmpl $0x19a, %eax            # imm = 0x19A
je 0x3f4a
cmpl $0x78, %eax
je 0x4080
leal -0x30(%rax), %ecx
cmpl $0x9, %ecx
ja 0x3af9
leal -0x31(%rax), %ebx
cmpl $0x30, %eax
pushq $0x9
popq %rax
cmovel %eax, %ebx
movq %r12, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0x82b9
movq %r12, %rdi
movl %ebx, %esi
callq 0x82b9
jmp 0x3af9
leaq 0x7358(%rip), %rdx      # 0xb170
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movq %r12, %rdi
pushq $-0x1
popq %rsi
callq 0x82b9
jmp 0x3af9
movq %r12, %rdi
callq 0x8700
movq %r12, %rdi
callq 0x8444
testb %al, %al
jne 0x3e31
jmp 0x3af9
movq %r12, %rdi
callq 0x8700
movq %r12, %rdi
callq 0x8350
cmpl $0x2, %eax
jne 0x3af9
movq %r12, %rdi
callq 0x8294
testq %rax, %rax
je 0x3af9
movq %rax, %r14
movq 0x18(%rax), %rbx
testq %rbx, %rbx
je 0x3af9
movq %rbx, %rdi
leaq 0x7364(%rip), %rsi      # 0xb1f2
callq 0x2390
testl %eax, %eax
je 0x4080
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
callq 0x75e6
movq %rax, %r15
testq %rax, %rax
je 0x3ecb
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0x9bf4
testb %al, %al
je 0x3f54
movq 0x18(%r14), %rdx
movq 0x30(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
movq %r13, %rcx
movq 0x28(%rsp), %r8
callq 0x87c0
movq %r12, %rdi
testl %eax, %eax
je 0x3f6d
leaq 0x7311(%rip), %rsi      # 0xb208
jmp 0x3f5e
movq %r12, %rdi
movl %r15d, %esi
callq 0x82b9
jmp 0x3af9
movq %r12, %rdi
callq 0x8444
testb %al, %al
je 0x3af9
movq %r12, %rdi
callq 0x8700
jmp 0x3af9
movq %r12, %rdi
movl $0x3e7, %esi            # imm = 0x3E7
callq 0x82b9
jmp 0x3af9
movq %r12, %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0x82b9
jmp 0x3af9
movb $0x0, 0x38(%rsp)
jmp 0x3af9
movq %r12, %rdi
leaq 0x729d(%rip), %rsi      # 0xb1fb
callq 0x843b
callq 0x2170
jmp 0x406f
callq 0x8700
movq 0x20(%rsp), %rdi
leaq 0x72c5(%rip), %rsi      # 0xb243
callq 0x2160
movq %rax, %r14
testq %rax, %rax
je 0x3fa3
leaq 0xe0(%rsp), %rdi
movq %r14, %rsi
callq 0x9c02
testl %eax, %eax
je 0x4045
callq 0x2330
movl (%rax), %eax
movl %eax, 0x8(%rsp)
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
pushq $0x4
popq %rsi
callq 0x6820
testq %rax, %rax
je 0x4023
movl 0x8(%rsp), %edi
movq %rax, 0x18(%rsp)
callq 0x2260
movq %rbx, %rdi
pushq $0x4
popq %rsi
leaq 0x7267(%rip), %rdx      # 0xb245
movq %r14, 0x8(%rsp)
movq 0x20(%rsp), %r14
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x691c
movq %rbx, %rdi
pushq $0x4
popq %rsi
leaq 0x7270(%rip), %rdx      # 0xb272
movq %r14, %rcx
movq 0x8(%rsp), %r14
xorl %eax, %eax
callq 0x691c
leaq 0xe0(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x9c02
movq %r12, %rdi
leaq 0x729d(%rip), %rsi      # 0xb2ca
callq 0x843b
callq 0x2170
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
testq %r14, %r14
jne 0x404d
jmp 0x4055
movl $0x0, 0x14(%rsp)
movq %r14, %rdi
callq 0x23f0
leaq 0x110(%rsp), %rdi
callq 0x6700
movq %rax, %rdi
callq 0xa180
movb $0x0, 0x38(%rsp)
movq %r15, %rdi
callq 0x2580
pushq $0x1
popq %r15
jmp 0x3af9
movl 0x14(%rsp), %ebx
cmpb $0x1, 0x98(%rsp)
jne 0x409b
movq 0xa0(%rsp), %rdi
callq 0x28e5
cmpb $0x1, 0x40(%rsp)
jne 0x40aa
movq %r12, %rdi
callq 0x8710
callq 0x2370
leaq 0xe0(%rsp), %rdi
callq 0x9d37
leaq 0x110(%rsp), %rdi
callq 0x671c
movl %ebx, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)