// File: mdpkh[P]retro6k/src/main.cpp
// Function: InitPaths()
int InitPaths()
{
	{ // find executable file location
#ifdef WINDOWS
		TCHAR rtnbuf[MAX_PATH];
		DWORD rtnlen = GetModuleFileName(NULL, rtnbuf, MAX_PATH);
		if (rtnlen < 0)
		{
			rtnbuf[0] = '.';
			rtnbuf[1] = 0;
		}
		else if (rtnlen < MAX_PATH)
		{
			rtnbuf[rtnlen] = 0;
		}
		rtnbuf[MAX_PATH - 1] = 0;
#else // *nix
		char rtnbuf[4096];
		ssize_t rtnlen = readlink("/proc/self/exe", rtnbuf, 4096);
		if (rtnlen < 0)
		{
			rtnbuf[0] = '.';
			rtnbuf[1] = 0;
		}
		else if (rtnlen < 4096)
		{
			rtnbuf[rtnlen] = 0;
		}
		rtnbuf[4095] = 0;
#endif
		config.system.exepath = std::filesystem::canonical(rtnbuf).remove_filename();
	}
	{ // find user home directory
#ifdef WINDOWS
		char homedrive[MAX_PATH], homedir[MAX_PATH];
		size_t homedrivelen;
		size_t homedirlen;
		getenv_s(&homedrivelen, homedrive, MAX_PATH, "HOMEDRIVE");
		if (homedrivelen < 0)
		{
			homedrive[0] = 'C';
			homedrive[1] = ':';
			homedrive[2] = 0;
		}
		else if (homedrivelen < MAX_PATH)
		{
			homedrive[homedrivelen] = 0;
		}
		homedrive[MAX_PATH - 1] = 0;
		getenv_s(&homedirlen, homedir, MAX_PATH, "HOMEPATH");
		if (homedirlen < 0)
		{
			homedir[0] = '\\';
			homedir[1] = 0;
		}
		else if (homedirlen < MAX_PATH)
		{
			homedir[homedirlen] = 0;
		}
		homedir[MAX_PATH - 1] = 0;
		config.system.homepath = std::filesystem::canonical(
			std::filesystem::path(homedrive)
			/ std::filesystem::path(homedir));
#else // *nix
		config.system.homepath = std::filesystem::canonical(std::getenv("HOME"));
#endif
	}
	{ // possible places to find configuration file
#ifdef WINDOWS
		config.system.configloc.push_back("Windows Registry");
#else // *nix
		config.system.configloc.push_back("/etc/.retro6k-config");
#endif
		config.system.configloc.push_back(
			config.system.homepath
			/ std::filesystem::path(".retro6k-config"));
		config.system.configloc.push_back(
			config.system.exepath / std::filesystem::path(".retro6k-config"));
	}
	// fallback paths in case of failure to load config
	config.path.rompath.push_back(config.system.exepath / std::filesystem::path("../rom"));
	config.path.rompath.push_back(std::filesystem::path("./../rom"));
	config.path.rompath.push_back(config.system.exepath);
	config.path.cartpath.push_back(config.system.exepath / std::filesystem::path("../cart"));
	config.path.cartpath.push_back(std::filesystem::path("./../cart"));
	config.path.cartpath.push_back(config.system.exepath);
	config.path.savepath.push_back(config.system.homepath / std::filesystem::path("Documents"));
	config.path.savepath.push_back(config.system.homepath);
	config.path.screencappath.push_back(config.system.homepath / std::filesystem::path("Pictures"));
	config.path.screencappath.push_back(config.system.homepath);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1390, %rsp           # imm = 0x1390
leaq -0x1000(%rbp), %rsi
leaq 0x14ac7(%rip), %rdi     # 0x23900
movl $0x1000, %edx           # imm = 0x1000
callq 0x74a0
movq %rax, -0x1008(%rbp)
cmpq $0x0, -0x1008(%rbp)
jge 0xee64
movb $0x2e, -0x1000(%rbp)
movb $0x0, -0xfff(%rbp)
jmp 0xee82
cmpq $0x1000, -0x1008(%rbp) # imm = 0x1000
jge 0xee80
movq -0x1008(%rbp), %rax
movb $0x0, -0x1000(%rbp,%rax)
jmp 0xee82
movb $0x0, -0x1(%rbp)
leaq -0x1058(%rbp), %rdi
movq %rdi, -0x1320(%rbp)
leaq -0x1000(%rbp), %rsi
movl $0x2, %edx
callq 0x143f0
movq -0x1320(%rbp), %rsi
leaq -0x1030(%rbp), %rdi
callq 0x7280
jmp 0xeeba
leaq -0x1030(%rbp), %rdi
callq 0x7270
movq %rax, -0x1328(%rbp)
jmp 0xeecf
movq -0x1328(%rbp), %rsi
leaq 0x1e543(%rip), %rdi     # 0x2d420
callq 0x7660
jmp 0xeee4
leaq -0x1030(%rbp), %rdi
callq 0x12ed0
leaq -0x1058(%rbp), %rdi
callq 0x12ed0
leaq 0x14a0c(%rip), %rdi     # 0x2390f
callq 0x7500
movq %rax, -0x10c0(%rbp)
leaq -0x10b8(%rbp), %rdi
movq %rdi, -0x1330(%rbp)
leaq -0x10c0(%rbp), %rsi
movl $0x2, %edx
callq 0x144d0
movq -0x1330(%rbp), %rsi
leaq -0x1090(%rbp), %rdi
callq 0x7280
jmp 0xef43
leaq 0x1e4fe(%rip), %rdi     # 0x2d448
leaq -0x1090(%rbp), %rsi
movq %rsi, -0x1340(%rbp)
callq 0x12e60
movq -0x1340(%rbp), %rdi
callq 0x12ed0
leaq -0x10b8(%rbp), %rdi
callq 0x12ed0
leaq 0x14998(%rip), %rsi     # 0x23914
leaq -0x10e8(%rbp), %rdi
movq %rdi, -0x1338(%rbp)
movl $0x2, %edx
callq 0x145e0
movq -0x1338(%rbp), %rsi
leaq 0x1e4ce(%rip), %rdi     # 0x2d470
callq 0x145b0
jmp 0xefa9
leaq -0x10e8(%rbp), %rdi
callq 0x12ed0
leaq 0x1495d(%rip), %rsi     # 0x23919
leaq -0x1138(%rbp), %rdi
movq %rdi, -0x1348(%rbp)
movl $0x2, %edx
callq 0x14740
movq -0x1348(%rbp), %rdx
leaq 0x1e466(%rip), %rsi     # 0x2d448
leaq -0x1110(%rbp), %rdi
callq 0x146c0
jmp 0xeff0
leaq 0x1e479(%rip), %rdi     # 0x2d470
leaq -0x1110(%rbp), %rsi
callq 0x145b0
jmp 0xf005
leaq -0x1110(%rbp), %rdi
callq 0x12ed0
leaq -0x1138(%rbp), %rdi
callq 0x12ed0
leaq 0x148f5(%rip), %rsi     # 0x23919
leaq -0x1188(%rbp), %rdi
movq %rdi, -0x1350(%rbp)
movl $0x2, %edx
callq 0x14740
movq -0x1350(%rbp), %rdx
leaq 0x1e3d6(%rip), %rsi     # 0x2d420
leaq -0x1160(%rbp), %rdi
callq 0x146c0
jmp 0xf058
leaq 0x1e411(%rip), %rdi     # 0x2d470
leaq -0x1160(%rbp), %rsi
callq 0x145b0
jmp 0xf06d
leaq -0x1160(%rbp), %rdi
callq 0x12ed0
leaq -0x1188(%rbp), %rdi
callq 0x12ed0
leaq 0x1489f(%rip), %rsi     # 0x2392b
leaq -0x11d8(%rbp), %rdi
movq %rdi, -0x1358(%rbp)
movl $0x2, %edx
callq 0x14820
movq -0x1358(%rbp), %rdx
leaq 0x1e36e(%rip), %rsi     # 0x2d420
leaq -0x11b0(%rbp), %rdi
callq 0x146c0
jmp 0xf0c0
leaq 0x1e3c1(%rip), %rdi     # 0x2d488
leaq -0x11b0(%rbp), %rsi
callq 0x145b0
jmp 0xf0d5
leaq -0x11b0(%rbp), %rdi
callq 0x12ed0
leaq -0x11d8(%rbp), %rdi
callq 0x12ed0
leaq 0x14835(%rip), %rsi     # 0x23929
leaq -0x1200(%rbp), %rdi
movq %rdi, -0x1360(%rbp)
movl $0x2, %edx
callq 0x14900
movq -0x1360(%rbp), %rsi
leaq 0x1e36e(%rip), %rdi     # 0x2d488
callq 0x145b0
jmp 0xf121
leaq -0x1200(%rbp), %rdi
callq 0x12ed0
leaq 0x1e354(%rip), %rdi     # 0x2d488
leaq 0x1e2e5(%rip), %rsi     # 0x2d420
movq %rsi, -0x1370(%rbp)
callq 0x13e60
leaq 0x147e6(%rip), %rsi     # 0x23934
leaq -0x1250(%rbp), %rdi
movq %rdi, -0x1368(%rbp)
movl $0x2, %edx
callq 0x149e0
movq -0x1370(%rbp), %rsi
movq -0x1368(%rbp), %rdx
leaq -0x1228(%rbp), %rdi
callq 0x146c0
jmp 0xf182
leaq 0x1e317(%rip), %rdi     # 0x2d4a0
leaq -0x1228(%rbp), %rsi
callq 0x145b0
jmp 0xf197
leaq -0x1228(%rbp), %rdi
callq 0x12ed0
leaq -0x1250(%rbp), %rdi
callq 0x12ed0
leaq 0x1477c(%rip), %rsi     # 0x23932
leaq -0x1278(%rbp), %rdi
movq %rdi, -0x1378(%rbp)
movl $0x2, %edx
callq 0x14ac0
movq -0x1378(%rbp), %rsi
leaq 0x1e2c4(%rip), %rdi     # 0x2d4a0
callq 0x145b0
jmp 0xf1e3
leaq -0x1278(%rbp), %rdi
callq 0x12ed0
leaq 0x1e2aa(%rip), %rdi     # 0x2d4a0
leaq 0x1e223(%rip), %rsi     # 0x2d420
callq 0x13e60
leaq 0x14733(%rip), %rsi     # 0x2393c
leaq -0x12c8(%rbp), %rdi
movq %rdi, -0x1380(%rbp)
movl $0x2, %edx
callq 0x14ac0
movq -0x1380(%rbp), %rdx
leaq 0x1e219(%rip), %rsi     # 0x2d448
leaq -0x12a0(%rbp), %rdi
callq 0x146c0
jmp 0xf23d
leaq 0x1e274(%rip), %rdi     # 0x2d4b8
leaq -0x12a0(%rbp), %rsi
callq 0x145b0
jmp 0xf252
leaq -0x12a0(%rbp), %rdi
callq 0x12ed0
leaq -0x12c8(%rbp), %rdi
callq 0x12ed0
leaq 0x1e247(%rip), %rdi     # 0x2d4b8
leaq 0x1e1d0(%rip), %rsi     # 0x2d448
movq %rsi, -0x1390(%rbp)
callq 0x13e60
leaq 0x146bb(%rip), %rsi     # 0x23946
leaq -0x1318(%rbp), %rdi
movq %rdi, -0x1388(%rbp)
movl $0x2, %edx
callq 0x14900
movq -0x1390(%rbp), %rsi
movq -0x1388(%rbp), %rdx
leaq -0x12f0(%rbp), %rdi
callq 0x146c0
jmp 0xf2bf
leaq 0x1e20a(%rip), %rdi     # 0x2d4d0
leaq -0x12f0(%rbp), %rsi
callq 0x145b0
jmp 0xf2d4
leaq -0x12f0(%rbp), %rdi
callq 0x12ed0
leaq -0x1318(%rbp), %rdi
callq 0x12ed0
leaq 0x1e12d(%rip), %rsi     # 0x2d420
movq %rsi, %rdi
addq $0x68, %rdi
addq $0x48, %rdi
addq $0x28, %rsi
callq 0x13e60
xorl %eax, %eax
addq $0x1390, %rsp           # imm = 0x1390
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf344
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1030(%rbp), %rdi
callq 0x12ed0
leaq -0x1058(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x10b8(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x10e8(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf3cd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1110(%rbp), %rdi
callq 0x12ed0
leaq -0x1138(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf410
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1160(%rbp), %rdi
callq 0x12ed0
leaq -0x1188(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x11b0(%rbp), %rdi
callq 0x12ed0
leaq -0x11d8(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1200(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf4b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1228(%rbp), %rdi
callq 0x12ed0
leaq -0x1250(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x1278(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf51c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x12a0(%rbp), %rdi
callq 0x12ed0
leaq -0x12c8(%rbp), %rdi
callq 0x12ed0
jmp 0xf568
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
jmp 0xf55c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1060(%rbp)
movl %eax, -0x1064(%rbp)
leaq -0x12f0(%rbp), %rdi
callq 0x12ed0
leaq -0x1318(%rbp), %rdi
callq 0x12ed0
movq -0x1060(%rbp), %rdi
callq 0x7710
nopw %cs:(%rax,%rax)