// File: mdpkh[P]retro6k/src/main.cpp
// Function: ReadArgs(int, char**)
int ReadArgs(int argc, char** argv)
{
	// TODO: read command-line arguments.
	char* cartspec = nullptr;
	char* savespec = nullptr;
	for (char** thisargptr = argv + 1; thisargptr - argv < argc; ++thisargptr)
	{
		if ((*thisargptr)[0] == '-')
		{
			if ((*thisargptr)[1] == '-')
			{
				// long option
#define WARN_NOMATCH default: \
                       SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, \
                         "Command line option %s " \
				         "ambiguous or not understood.", \
                         *thisargptr);
				switch ((*thisargptr)[2])
				{
				case 'c':
					switch ((*thisargptr)[3])
					{
					case 'a': //--ca(rtpath): specify cart path
						goto readcartpathparameter;
					case 'o': //--co(nfigfile): specify config file location
						goto readconfigfileparameter;
					WARN_NOMATCH
					}
					break;
				case 'r':
					switch ((*thisargptr)[3])
					{
					case 'o': //--ro(mpath): specify rom path
						goto readrompathparameter;
						WARN_NOMATCH
					}
					break;
				case 's':
					switch ((*thisargptr)[3])
					{
					case 'a': //--sa(vepath): specify save path
						goto readsavepathparameter;
					case 'c': //--sc(reencappath): specify save path
						goto readscreencappathparameter;
						WARN_NOMATCH
					}
					break;
					WARN_NOMATCH
				}
#undef WARN_NOMATCH
			}
			else
			{
				// short option(s) / switch(es)
				char lastshort = 0;
				for (char* thisshortptr = (*thisargptr) + 1; 
					*thisshortptr; ++thisshortptr)
				{
					lastshort = *thisshortptr;
					switch (lastshort)
					{
						// handlers for each short option / switch
					case 'c': // specify config file location
					case 'p': // specify cart path
					case 'r': // specify rom path
					case 's': // specify save path
						break;
					default:
						SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
							"Command line switch -%c not understood.",
							lastshort);
					}
				}
				switch (lastshort)
				{
				case 'c':
					goto readconfigfileparameter;
				case 'p':
					goto readcartpathparameter;
				case 'r':
					goto readrompathparameter;
				case 's':
					goto readsavepathparameter;
					// jumps for next-argument reading
				default:
					;
				}
			}
			continue;
			// labels for reading next argument as parameter to an option
		readconfigfileparameter:
			// config file spec, if given, should become
			//only item of config.system.configloc
			++thisargptr;
			config.system.configloc.clear();
			config.system.configloc.push_back(
				std::filesystem::path(*thisargptr));
			continue;
		readcartpathparameter:
			++thisargptr;
			config.sessionpath.cartpath.push_back(
				std::filesystem::path(*thisargptr));
			continue;
		readrompathparameter:
			++thisargptr;
			config.sessionpath.rompath.push_back(
				std::filesystem::path(*thisargptr));
			continue;
		readsavepathparameter:
			++thisargptr;
			config.sessionpath.savepath.push_back(
				std::filesystem::path(*thisargptr));
			continue;
		readscreencappathparameter:
			++thisargptr;
			config.sessionpath.screencappath.push_back(
				std::filesystem::path(*thisargptr));
			continue;
		}
		else
		{
			// positional argument
			if (cartspec == nullptr)
			{
				cartspec = *thisargptr;
				continue;
			}
			if (savespec == nullptr)
			{
				savespec = *thisargptr;
				continue;
			}
			SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
				"Command line argument %s not understood.",
				*thisargptr);
		}
	}
    // TODO: actually do something with cartspec and savespec if they are non-null
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
sarq $0x3, %rax
movslq -0x4(%rbp), %rcx
cmpq %rcx, %rax
jge 0x1231f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x122cd
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2d, %eax
jne 0x11ff3
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x2(%rax), %eax
movl %eax, -0x114(%rbp)
subl $0x63, %eax
je 0x11f16
jmp 0x11ef9
movl -0x114(%rbp), %eax
subl $0x72, %eax
je 0x11f61
jmp 0x11f06
movl -0x114(%rbp), %eax
subl $0x73, %eax
je 0x11f8f
jmp 0x11fd7
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x3(%rax), %eax
movl %eax, -0x118(%rbp)
subl $0x61, %eax
je 0x11f3b
jmp 0x11f2e
movl -0x118(%rbp), %eax
subl $0x6f, %eax
je 0x11f40
jmp 0x11f45
jmp 0x12127
jmp 0x120b2
movq -0x28(%rbp), %rax
movq (%rax), %rdx
xorl %edi, %edi
leaq 0x11a3e(%rip), %rsi     # 0x23993
movb $0x0, %al
callq 0x7650
jmp 0x11fee
movq -0x28(%rbp), %rax
movq (%rax), %rax
movb 0x3(%rax), %al
subb $0x6f, %al
jne 0x11f76
jmp 0x11f71
jmp 0x12192
movq -0x28(%rbp), %rax
movq (%rax), %rdx
xorl %edi, %edi
leaq 0x11a0d(%rip), %rsi     # 0x23993
movb $0x0, %al
callq 0x7650
jmp 0x11fee
movq -0x28(%rbp), %rax
movq (%rax), %rax
movsbl 0x3(%rax), %eax
movl %eax, -0x11c(%rbp)
subl $0x61, %eax
je 0x11fb4
jmp 0x11fa7
movl -0x11c(%rbp), %eax
subl $0x63, %eax
je 0x11fb9
jmp 0x11fbe
jmp 0x121fd
jmp 0x12268
movq -0x28(%rbp), %rax
movq (%rax), %rdx
xorl %edi, %edi
leaq 0x119c5(%rip), %rsi     # 0x23993
movb $0x0, %al
callq 0x7650
jmp 0x11fee
movq -0x28(%rbp), %rax
movq (%rax), %rdx
xorl %edi, %edi
leaq 0x119ac(%rip), %rsi     # 0x23993
movb $0x0, %al
callq 0x7650
jmp 0x120ad
movb $0x0, -0x29(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1206c
movq -0x38(%rbp), %rax
movb (%rax), %al
movb %al, -0x29(%rbp)
movsbl -0x29(%rbp), %eax
movl %eax, -0x120(%rbp)
subl $0x63, %eax
je 0x12046
jmp 0x12029
movl -0x120(%rbp), %eax
subl $0x70, %eax
je 0x12046
jmp 0x12036
movl -0x120(%rbp), %eax
addl $-0x72, %eax
subl $0x1, %eax
ja 0x12048
jmp 0x12046
jmp 0x1205c
movsbl -0x29(%rbp), %edx
xorl %edi, %edi
leaq 0x11972(%rip), %rsi     # 0x239c7
movb $0x0, %al
callq 0x7650
jmp 0x1205e
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x12006
movsbl -0x29(%rbp), %eax
addl $-0x63, %eax
movl %eax, %ecx
movq %rcx, -0x128(%rbp)
subl $0x10, %eax
ja 0x120a9
movq -0x128(%rbp), %rax
leaq 0xe1d1(%rip), %rcx      # 0x20260
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x120b2
jmp 0x12127
jmp 0x12192
jmp 0x121fd
jmp 0x120ab
jmp 0x120ad
jmp 0x1230e
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
leaq 0x1b3ab(%rip), %rdi     # 0x2d470
movq %rdi, -0x138(%rbp)
callq 0x13a70
movq -0x28(%rbp), %rsi
leaq -0x60(%rbp), %rdi
movq %rdi, -0x130(%rbp)
movl $0x2, %edx
callq 0x144d0
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
callq 0x145b0
jmp 0x120ff
leaq -0x60(%rbp), %rdi
callq 0x12ed0
jmp 0x1230e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x12ed0
jmp 0x1232a
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0x140(%rbp)
movl $0x2, %edx
callq 0x144d0
movq -0x140(%rbp), %rsi
leaq 0x1b3a3(%rip), %rdi     # 0x2d500
callq 0x145b0
jmp 0x12164
leaq -0x98(%rbp), %rdi
callq 0x12ed0
jmp 0x1230e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x12ed0
jmp 0x1232a
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x148(%rbp)
movl $0x2, %edx
callq 0x144d0
movq -0x148(%rbp), %rsi
leaq 0x1b320(%rip), %rdi     # 0x2d4e8
callq 0x145b0
jmp 0x121cf
leaq -0xc0(%rbp), %rdi
callq 0x12ed0
jmp 0x1230e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x12ed0
jmp 0x1232a
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x150(%rbp)
movl $0x2, %edx
callq 0x144d0
movq -0x150(%rbp), %rsi
leaq 0x1b2e5(%rip), %rdi     # 0x2d518
callq 0x145b0
jmp 0x1223a
leaq -0xe8(%rbp), %rdi
callq 0x12ed0
jmp 0x1230e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x12ed0
jmp 0x1232a
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
leaq -0x110(%rbp), %rdi
movq %rdi, -0x158(%rbp)
movl $0x2, %edx
callq 0x144d0
movq -0x158(%rbp), %rsi
leaq 0x1b292(%rip), %rdi     # 0x2d530
callq 0x145b0
jmp 0x122a5
leaq -0x110(%rbp), %rdi
callq 0x12ed0
jmp 0x1230e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x12ed0
jmp 0x1232a
cmpq $0x0, -0x18(%rbp)
jne 0x122e1
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x1230e
cmpq $0x0, -0x20(%rbp)
jne 0x122f5
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x1230e
movq -0x28(%rbp), %rax
movq (%rax), %rdx
xorl %edi, %edi
leaq 0x116ea(%rip), %rsi     # 0x239ef
movb $0x0, %al
callq 0x7650
jmp 0x1230e
movq -0x28(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x28(%rbp)
jmp 0x11e9e
xorl %eax, %eax
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x7710
nopw %cs:(%rax,%rax)