// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x98b0
movq -0x18(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x9dc0
movq %rax, %rdi
callq 0xa3c0
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
callq 0x51d0
cmpl $0x0, %eax
je 0x4c34
jmp 0x51b3
leaq 0xc4e5(%rip), %rdi      # 0x11120
leaq 0xc4e7(%rip), %rsi      # 0x11129
xorl %edx, %edx
callq 0x10a30
callq 0x2420
callq 0x2410
callq 0x2430
cmpl $-0x1, %eax
jne 0x4c79
movq -0x18(%rbp), %rdi
movl $0x4, %esi
leaq 0xc4bf(%rip), %rdx      # 0x1112c
movb $0x0, %al
callq 0xa260
jmp 0x51a2
leaq 0xc4c2(%rip), %rdi      # 0x11142
callq 0x10680
movq 0x1134c(%rip), %rax     # 0x15fd8
movq (%rax), %rdi
xorl %esi, %esi
callq 0x20a0
leaq -0x188(%rbp), %rdi
xorl %esi, %esi
movl $0x98, %edx
callq 0x2060
leaq -0x188(%rbp), %rdi
addq $0x8, %rdi
movq -0x8(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x5250
cmpl $0x0, %eax
je 0x4ce2
movq -0x18(%rbp), %rdi
movl $0x4, %esi
leaq 0xc47d(%rip), %rdx      # 0x11153
movb $0x0, %al
callq 0xa260
jmp 0x5196
leaq -0x188(%rbp), %rdi
addq $0x60, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x52d0
cmpl $0x0, %eax
je 0x4d17
movq -0x18(%rbp), %rdi
movl $0x2, %esi
leaq 0xc45a(%rip), %rdx      # 0x11168
movb $0x0, %al
callq 0xa260
jmp 0x4d37
leaq -0xd8(%rbp), %rdi
callq 0x9d80
movq %rax, %rdi
callq 0xffa0
leaq -0xd8(%rbp), %rdi
callq 0x9cf0
movl $0x0, -0x1c(%rbp)
xorl %edi, %edi
callq 0x2180
callq 0x2080
movq 0x11287(%rip), %rax     # 0x15fd8
movq (%rax), %rdi
movl $0x1, %esi
callq 0x2140
leaq -0x1a0(%rbp), %rdi
movl $0x1e, %esi
callq 0xfe10
leaq -0x178(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0xfe50
leaq -0x188(%rbp), %rdi
callq 0x53a0
movq 0x1123c(%rip), %rax     # 0x15fd8
movq (%rax), %rdi
callq 0x2500
movl %eax, -0x1a4(%rbp)
movl %eax, %ecx
movl %ecx, -0x1c8(%rbp)
subl $0xa, %eax
je 0x4f0f
jmp 0x4dbd
movl -0x1c8(%rbp), %eax
subl $0x1b, %eax
je 0x507a
jmp 0x4dce
movl -0x1c8(%rbp), %eax
subl $0x20, %eax
je 0x50b3
jmp 0x4ddf
movl -0x1c8(%rbp), %eax
subl $0x47, %eax
je 0x50df
jmp 0x4df0
movl -0x1c8(%rbp), %eax
subl $0x61, %eax
je 0x507a
jmp 0x4e01
movl -0x1c8(%rbp), %eax
subl $0x64, %eax
je 0x4f0f
jmp 0x4e12
movl -0x1c8(%rbp), %eax
subl $0x67, %eax
je 0x50c9
jmp 0x4e23
movl -0x1c8(%rbp), %eax
subl $0x73, %eax
je 0x50b3
jmp 0x4e34
movl -0x1c8(%rbp), %eax
subl $0x77, %eax
je 0x509d
jmp 0x4e45
movl -0x1c8(%rbp), %eax
subl $0x78, %eax
je 0x5119
jmp 0x4e56
movl -0x1c8(%rbp), %eax
subl $0x102, %eax            # imm = 0x102
je 0x50b3
jmp 0x4e69
movl -0x1c8(%rbp), %eax
subl $0x103, %eax            # imm = 0x103
je 0x509d
jmp 0x4e7c
movl -0x1c8(%rbp), %eax
subl $0x104, %eax            # imm = 0x104
je 0x507a
jmp 0x4e8f
movl -0x1c8(%rbp), %eax
subl $0x105, %eax            # imm = 0x105
je 0x4f0f
jmp 0x4e9e
movl -0x1c8(%rbp), %eax
subl $0x106, %eax            # imm = 0x106
je 0x50f5
jmp 0x4eb1
movl -0x1c8(%rbp), %eax
subl $0x107, %eax            # imm = 0x107
je 0x509d
jmp 0x4ec4
movl -0x1c8(%rbp), %eax
subl $0x150, %eax            # imm = 0x150
je 0x50b3
jmp 0x4ed7
movl -0x1c8(%rbp), %eax
subl $0x151, %eax            # imm = 0x151
je 0x509d
jmp 0x4eea
movl -0x1c8(%rbp), %eax
subl $0x157, %eax            # imm = 0x157
je 0x4f0f
jmp 0x4ef9
movl -0x1c8(%rbp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0x511b
jmp 0x5124
movq -0x1b0(%rbp), %rdi
callq 0xd410
movq -0x1b0(%rbp), %rdi
callq 0xce30
cmpl $0x2, %eax
jne 0x5075
movq -0x1b0(%rbp), %rdi
callq 0xcbd0
movq %rax, -0x1b8(%rbp)
cmpq $0x0, -0x1b8(%rbp)
je 0x4f5b
movq -0x1b8(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x4f60
jmp 0x518f
movq -0x1b8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq 0xc218(%rip), %rsi      # 0x1118a
callq 0x2380
cmpl $0x0, %eax
jne 0x4f81
jmp 0x5194
movq -0x1b8(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0xd8(%rbp), %rdi
callq 0xb610
movq %rax, -0x1c0(%rbp)
cmpq $0x0, -0x1c0(%rbp)
je 0x4fdd
movq -0x1c0(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
callq 0xf580
testb $0x1, %al
jne 0x4fdd
movq -0x1b0(%rbp), %rdi
leaq 0xc1c5(%rip), %rsi      # 0x11193
callq 0xcff0
callq 0x2170
jmp 0x5069
movq -0x8(%rbp), %rdi
movq -0x1b8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x18(%rbp), %r8
leaq -0xf0(%rbp), %rsi
leaq -0x1a0(%rbp), %rcx
callq 0xd560
cmpl $0x0, %eax
je 0x5022
movq -0x1b0(%rbp), %rdi
leaq 0xc18a(%rip), %rsi      # 0x111a0
callq 0xcff0
callq 0x2170
jmp 0x5067
movq -0x1b0(%rbp), %rdi
callq 0xd410
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x1b0(%rbp), %rcx
leaq -0xf0(%rbp), %rdi
callq 0x5710
movl %eax, -0x1c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x9d80
movq %rax, %rdi
callq 0xffa0
movb $0x0, -0x188(%rbp)
jmp 0x5069
movq -0x1c0(%rbp), %rdi
callq 0x2580
jmp 0x518f
movq -0x1b0(%rbp), %rdi
callq 0xd020
testb $0x1, %al
jne 0x508c
jmp 0x5098
movq -0x1b0(%rbp), %rdi
callq 0xd410
jmp 0x518f
movq -0x1b0(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xcc40
jmp 0x518f
movq -0x1b0(%rbp), %rdi
movl $0x1, %esi
callq 0xcc40
jmp 0x518f
movq -0x1b0(%rbp), %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0xcc40
jmp 0x518f
movq -0x1b0(%rbp), %rdi
movl $0x3e7, %esi            # imm = 0x3E7
callq 0xcc40
jmp 0x518f
jmp 0x50f7
movq -0x1b0(%rbp), %rdi
callq 0xd020
testb $0x1, %al
jne 0x5109
jmp 0x5117
movq -0x1b0(%rbp), %rdi
callq 0xd410
jmp 0x50f7
jmp 0x518f
jmp 0x5194
movb $0x0, -0x188(%rbp)
jmp 0x518f
cmpl $0x30, -0x1a4(%rbp)
jl 0x518d
cmpl $0x39, -0x1a4(%rbp)
jg 0x518d
cmpl $0x30, -0x1a4(%rbp)
jne 0x514c
movl $0x9, %eax
movl %eax, -0x1cc(%rbp)
jmp 0x515e
movl -0x1a4(%rbp), %eax
subl $0x30, %eax
subl $0x1, %eax
movl %eax, -0x1cc(%rbp)
movl -0x1cc(%rbp), %eax
movl %eax, -0x1c4(%rbp)
movq -0x1b0(%rbp), %rdi
movl $0xfffffc19, %esi       # imm = 0xFFFFFC19
callq 0xcc40
movq -0x1b0(%rbp), %rdi
movl -0x1c4(%rbp), %esi
callq 0xcc40
jmp 0x518f
jmp 0x4d6f
jmp 0x5196
leaq -0x188(%rbp), %rdi
callq 0x5820
callq 0x2360
leaq -0xf0(%rbp), %rdi
callq 0xf760
leaq -0xd8(%rbp), %rdi
callq 0x9df0
movl -0x1c(%rbp), %eax
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
nopl (%rax,%rax)