// File: /workspace/github_c_makefile_star2_O1/TurkeyMcMac[P]ts3d/src/main.c
// Function: main
int main(int argc, char *argv[])
{
	// 0 for EXIT_SUCCESS, -1 for EXIT_FAILURE:
	int ret = -1;
	// Indicates option parsing error:
	bool error = false;
	// Program name from invokation:
	const char *progname = argc > 0 ? argv[0] : "ts3d";
	// Data directory path, NULL for default:
	char *data_dir = NULL;
	// State file path, NULL for default:
	char *state_file = NULL;
	// Logger to be used by do_ts3d_game:
	struct logger log;
	// Default log destination file path, NULL until initialized:
	char *log_name_def = NULL;
	// Default log destination file, NULL until initialized:
	FILE *log_def = NULL;
	int opt;
	logger_init(&log);
	logger_set_output(&log, LOGGER_ALL, UNTOUCHED_MARKER, false);
	while ((opt = getopt(argc, argv, "d:hl:L:s:v")) >= 0) {
		switch (opt) {
		case 'd':
			free(data_dir);
			data_dir = str_dup(optarg);
			break;
		case 'h':
			print_help(progname);
			ret = 0;
			goto end;
		case 'l':
			if (add_log_dest(progname, &log, optarg)) goto end;
			break;
		case 'L':
			if (remove_log_dest(progname, &log, optarg)) goto end;
			break;
		case 's':
			free(state_file);
			state_file = str_dup(optarg);
			break;
		case 'v':
			print_version(progname);
			ret = 0;
			goto end;
		default:
			error = true;
			break;
		}
	}
	if (error) {
		print_usage(progname, stderr);
		goto end;
	}
	if (!data_dir) data_dir = default_file("data", "TS3D_DATA");
	if (!data_dir || ensure_file(data_dir, true)) {
		fprintf(stderr,
			"%s: No suitable \"data\" directory available: %s\n",
			progname, strerror(errno));
		goto end;
	}
	if (!state_file) state_file = default_file("state.json", "TS3D_STATE");
	if (!state_file || ensure_file(state_file, false)) {
		fprintf(stderr,
			"%s: No suitable \"state.json\" file available: %s\n",
			progname, strerror(errno));
		goto end;
	}
	if ((log_name_def = default_file("log", "TS3D_LOG"))
	 && (log_def = fopen(log_name_def, "w+"))) {
		// Replace UNTOUCHED_MARKER with log_def.
		if (logger_get_output(&log, LOGGER_INFO) == UNTOUCHED_MARKER)
			logger_set_output(&log, LOGGER_INFO, log_def, false);
		if (logger_get_output(&log, LOGGER_WARNING) == UNTOUCHED_MARKER)
			logger_set_output(&log, LOGGER_WARNING, log_def, false);
		if (logger_get_output(&log, LOGGER_ERROR) == UNTOUCHED_MARKER)
			logger_set_output(&log, LOGGER_ERROR, log_def, false);
	}
	ret = do_ts3d_game(data_dir, state_file, &log);
	if (ret < 0) {
		FILE *err_log = logger_get_output(&log, LOGGER_ERROR);
		if (err_log) {
			fflush(err_log);
			rewind(err_log);
			fprintf(stderr, "%s: An error occurred.\n", progname);
			// Copy the error log to stderr:
			int c;
			while ((c = getc(err_log)) != EOF) {
				putc(c, stderr);
			}
		} else {
			fprintf(stderr,
				"%s: An error occurred and was not logged.\n",
				progname);
		}
		goto end;
	}
end:
	free(data_dir);
	free(state_file);
	logger_free(&log);
	free(log_name_def);
	if (log_def) fclose(log_def);
	exit(ret >= 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r15
movl %edi, %r12d
testl %edi, %edi
jle 0x7049
movq (%r15), %rax
jmp 0x7050
leaq 0x561d(%rip), %rax      # 0xc66d
movq %rax, (%rsp)
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x6d70
movq 0x9f68(%rip), %rax      # 0x10fd0
movq (%rax), %rdx
xorl %r13d, %r13d
movq %rbx, %rdi
movl $0x7, %esi
xorl %ecx, %ecx
callq 0x6dd1
leaq 0x55ee(%rip), %rdx      # 0xc672
movl %r12d, %edi
movq %r15, %rsi
callq 0x2240
testl %eax, %eax
js 0x72e9
movq 0x9f42(%rip), %rbx      # 0x10fe0
leaq 0x55cd(%rip), %rbp      # 0xc672
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
movq %r15, 0x50(%rsp)
movl %r12d, 0x24(%rsp)
cmpl $0x6b, %eax
jg 0x70e9
cmpl $0x4c, %eax
je 0x710f
cmpl $0x64, %eax
jne 0x719d
movq %r14, %rdi
callq 0x2570
movq (%rbx), %rdi
callq 0xaec0
movq %rax, %r14
jmp 0x7293
cmpl $0x6c, %eax
je 0x712d
cmpl $0x73, %eax
jne 0x71a7
movq %r13, %rdi
callq 0x2570
movq (%rbx), %rdi
callq 0xaec0
movq %rax, %r13
jmp 0x7293
movq (%rbx), %rdx
movq (%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x7673
testl %eax, %eax
je 0x7293
jmp 0x7626
movq %r14, 0x8(%rsp)
movq %rbx, %rbp
movq (%rbx), %rbx
movq %rbx, %rdi
movl $0x3d, %esi
callq 0x22c0
testq %rax, %rax
je 0x7438
movq %rax, %r14
cmpb $0x0, 0x1(%rax)
movq %r13, 0x18(%rsp)
je 0x71bc
leaq 0x1(%r14), %r12
movq %r12, %rdi
leaq 0x5099(%rip), %rsi      # 0xc203
callq 0x2160
testq %rax, %rax
je 0x72ab
movq %r12, %rdi
leaq 0x5a87(%rip), %rsi      # 0xcc09
movq %rax, %rdx
callq 0x23f0
testq %rax, %rax
je 0x72ab
movq %rax, %r15
movb $0x1, %r13b
movb $0x1, %al
jmp 0x71cb
cmpl $0x68, %eax
jne 0x71b0
jmp 0x7610
cmpl $0x76, %eax
je 0x75f9
movb $0x1, %al
movq %rax, 0x10(%rsp)
jmp 0x7293
movq 0x9e25(%rip), %rax      # 0x10fe8
movq (%rax), %r15
movb $0x1, %al
xorl %r13d, %r13d
testb %al, %al
je 0x7630
subq %rbx, %r14
leaq 0x1(%r14), %r12
leaq 0x5a80(%rip), %rdi      # 0xcc61
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2350
testl %eax, %eax
je 0x723d
leaq 0x5a6f(%rip), %rdi      # 0xcc66
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2350
testl %eax, %eax
je 0x7245
leaq 0x5a5f(%rip), %rdi      # 0xcc6c
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2350
testl %eax, %eax
je 0x724d
leaq 0x5a52(%rip), %rdi      # 0xcc75
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2350
movl $0x4, %r12d
testl %eax, %eax
je 0x7253
jmp 0x746c
movl $0x7, %r12d
jmp 0x7253
movl $0x1, %r12d
jmp 0x7253
movl $0x2, %r12d
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x7782
movzbl %r13b, %ecx
movq %rbx, %rdi
movl %r12d, %esi
movq %r15, %rdx
callq 0x6dd1
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %r14
movq 0x50(%rsp), %r15
movl 0x24(%rsp), %r12d
movq %rbp, %rbx
leaq 0x53df(%rip), %rbp      # 0xc672
movl %r12d, %edi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2240
testl %eax, %eax
jns 0x70be
jmp 0x72f0
movq 0x9d36(%rip), %rax      # 0x10fe8
movq (%rax), %r15
callq 0x2320
movl (%rax), %edi
callq 0x2260
xorl %r13d, %r13d
movq %r15, %rdi
leaq 0x593e(%rip), %rsi      # 0xcc0c
movq (%rsp), %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x24b0
xorl %r15d, %r15d
xorl %eax, %eax
jmp 0x71cb
xorl %r14d, %r14d
xorl %eax, %eax
jmp 0x72f5
movq 0x10(%rsp), %rax
testb $0x1, %al
je 0x731d
movq 0x9ce8(%rip), %rax      # 0x10fe8
movq (%rax), %rdi
leaq 0x59cc(%rip), %rsi      # 0xccd6
xorl %r12d, %r12d
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x24b0
jmp 0x73db
testq %r14, %r14
jne 0x7338
leaq 0x5354(%rip), %rdi      # 0xc67d
leaq 0x5352(%rip), %rsi      # 0xc682
callq 0xb01e
movq %rax, %r14
testq %r14, %r14
je 0x73aa
movq %r14, %rdi
movl $0x1, %esi
callq 0xaf70
testl %eax, %eax
jne 0x73aa
testq %r13, %r13
jne 0x7369
leaq 0x5362(%rip), %rdi      # 0xc6bc
leaq 0x5366(%rip), %rsi      # 0xc6c7
callq 0xb01e
movq %rax, %r13
testq %r13, %r13
je 0x7419
movq %r13, %rdi
xorl %esi, %esi
callq 0xaf70
testl %eax, %eax
jne 0x7419
leaq 0x5378(%rip), %rdi      # 0xc703
leaq 0x5375(%rip), %rsi      # 0xc707
callq 0xb01e
xorl %ebx, %ebx
testq %rax, %rax
jne 0x74a0
movq %rax, %r12
jmp 0x74bb
movq 0x9c37(%rip), %rax      # 0x10fe8
movq (%rax), %rbx
callq 0x2320
movl (%rax), %edi
callq 0x2260
leaq 0x52c5(%rip), %rsi      # 0xc68c
xorl %r12d, %r12d
movq %rbx, %rdi
movq (%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x24b0
movl $0x1, %ebx
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x2570
movq %r13, %rdi
callq 0x2570
leaq 0x28(%rsp), %rdi
callq 0x6fa4
movq %r12, %rdi
callq 0x2570
testq %r15, %r15
je 0x7412
movq %r15, %rdi
callq 0x23e0
movl %ebx, %edi
callq 0x2110
movq 0x9bc8(%rip), %rax      # 0x10fe8
movq (%rax), %rbx
callq 0x2320
movl (%rax), %edi
callq 0x2260
leaq 0x529c(%rip), %rsi      # 0xc6d2
jmp 0x73c7
movq 0x9ba9(%rip), %rax      # 0x10fe8
movq (%rax), %rdi
leaq 0x57e6(%rip), %rsi      # 0xcc2f
xorl %r12d, %r12d
movq (%rsp), %rdx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x24b0
movl $0x1, %ebx
xorl %r15d, %r15d
movq 0x8(%rsp), %r14
jmp 0x73e3
movq 0x9b75(%rip), %rax      # 0x10fe8
movq (%rax), %rdi
leaq 0x57ff(%rip), %rsi      # 0xcc7c
xorl %r12d, %r12d
movq (%rsp), %rdx
movl %r14d, %ecx
movq %rbx, %r8
xorl %eax, %eax
callq 0x24b0
movl $0x1, %ebx
xorl %r15d, %r15d
movq 0x18(%rsp), %r13
jmp 0x7462
leaq 0x5269(%rip), %rsi      # 0xc710
movq %rax, %r12
movq %rax, %rdi
callq 0x2160
testq %rax, %rax
jne 0x7560
xorl %r15d, %r15d
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x3a1c
testl %eax, %eax
jns 0x73e3
leaq 0x28(%rsp), %rdi
movl $0x4, %esi
callq 0x6d87
testq %rax, %rax
jne 0x7508
movq 0x9af7(%rip), %rax      # 0x10fe8
movq (%rax), %rdi
leaq 0x5230(%rip), %rsi      # 0xc72b
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x24b0
jmp 0x7556
movq %rax, %rbx
movq %r14, %rbp
movq %rax, %rdi
callq 0x2550
movq %rbx, %rdi
callq 0x2280
movq 0x9ac3(%rip), %r14      # 0x10fe8
movq (%r14), %rdi
leaq 0x51e4(%rip), %rsi      # 0xc713
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x24b0
movq %rbx, %rdi
callq 0x2560
cmpl $-0x1, %eax
je 0x7553
movq (%r14), %rsi
movl %eax, %edi
callq 0x22a0
jmp 0x753a
movq %rbp, %r14
movl $0x1, %ebx
jmp 0x73e3
movq %rax, %r15
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
callq 0x6d87
movq 0x9a57(%rip), %rcx      # 0x10fd0
cmpq (%rcx), %rax
jne 0x7592
leaq 0x28(%rsp), %rdi
movl $0x1, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6dd1
leaq 0x28(%rsp), %rdi
movl $0x2, %esi
callq 0x6d87
movq 0x9a28(%rip), %rcx      # 0x10fd0
cmpq (%rcx), %rax
jne 0x75c1
leaq 0x28(%rsp), %rdi
movl $0x2, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6dd1
leaq 0x28(%rsp), %rdi
movl $0x4, %esi
callq 0x6d87
movq 0x99f9(%rip), %rcx      # 0x10fd0
cmpq (%rcx), %rax
jne 0x74be
leaq 0x28(%rsp), %rdi
movl $0x4, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x6dd1
jmp 0x74be
leaq 0x56c3(%rip), %rdi      # 0xccc3
xorl %r12d, %r12d
movq (%rsp), %rsi
xorl %eax, %eax
callq 0x2040
jmp 0x761c
movq (%rsp), %rdi
callq 0x764a
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
jmp 0x73e3
movl $0x1, %ebx
xorl %r15d, %r15d
jmp 0x7642
movl $0x1, %ebx
xorl %r15d, %r15d
movq 0x18(%rsp), %r13
movq 0x8(%rsp), %r14
xorl %r12d, %r12d
jmp 0x73e3