// File: tplinderoth[P]introDist/Dxyz.cpp
// Function: parseArgs(int, char**, std::basic_ifstream<char, std::char_traits<char>>&, std::basic_ifstream<char, std::char_traits<char>>&, std::basic_ofstream<char, std::char_traits<char>>&, int&, int&, double&, int&)
int parseArgs (int argc, char** argv, std::ifstream &poptab, std::ifstream &vcf, std::ofstream &outf, int &winsize, int &step, double &maf, int &infmt) {
	int rv = 0;

		if (argc < 5) {
			dxyzInfo(winsize, step, maf);
			return 1;
		}

		int argpos = 1;
		while (argpos < argc) {
			if (strcmp(argv[argpos], "-vcf") == 0) {
				// input VCF
				const char* invcf_name = argv[argpos+1];
				if (strcmp(invcf_name, "-") == 0) {
					infmt = 2; // reading from standard input
				} else {
					vcf.open(invcf_name, std::ios_base::in | std::ios_base::binary);
					if (! vcf) {
						std::cerr << "Unable to open input VCF " << invcf_name << "\n";
						return -1;
					}
					// check for gzipped file by reading magic numbers
					unsigned char magic [2] = {0};
					vcf.read(reinterpret_cast<char*>(magic), sizeof(magic));
					infmt = (magic[0] == 0x1f && magic[1] == 0x8b) ? 1 : 0;
					vcf.seekg(0, std::ios_base::beg);
				}
			}

			else if (strcmp(argv[argpos], "-poptable") == 0) {
				const char* poptable_name = argv[argpos+1];
				poptab.open(poptable_name, std::ios_base::in);
				if (!poptab) {
					std::cerr << "Unable to open population table " << poptable_name << "\n";
					return -1;
				}
			}

			else if (strcmp(argv[argpos], "-out") == 0) {
				// output file
				const char* outfile = argv[argpos+1];
				outf.open(outfile, std::ios::out);
				if (! outf) {
					std::cerr << "Unable to open output VCF " << *outfile << "\n";
					return -1;
				}
			}

			else if (strcmp(argv[argpos],"-winsize") == 0) {
				// window size
				winsize = atoi(argv[argpos+1]);
				if (winsize < 1) {
					std::cerr << "Window size must be at least one SNP\n";
					return -1;
				}
			}

			else if (strcmp(argv[argpos], "-step") == 0) {
				// step size
				step = atoi(argv[argpos+1]);
				if (step < 1) {
					std::cerr << "Step size must be at least one SNP\n";
					return -1;
				}
			}

			else if (strcmp(argv[argpos], "-maf") == 0) {
				// maf
				maf = atof(argv[argpos+1]);
				if (maf < 0 || maf > 0.5) {
					std::cerr << "-maf must be in range [0,0.5)\n";
					return -1;
				}
			}

			else {
				std::cerr << "Unknown argument " << argv[argpos] << "\n";
				return -1;
			}

			argpos += 2;
		}

		return rv;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
cmpl $0x5, -0x8(%rbp)
jge 0x8ced
movq -0x30(%rbp), %rax
movl (%rax), %edi
movq 0x10(%rbp), %rax
movl (%rax), %esi
movq 0x18(%rbp), %rax
movsd (%rax), %xmm0
callq 0xa080
movl $0x1, -0x4(%rbp)
jmp 0x911f
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x9119
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x15372(%rip), %rsi     # 0x1e085
callq 0x8630
cmpl $0x0, %eax
jne 0x8e41
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x155d3(%rip), %rsi     # 0x1e314
callq 0x8630
cmpl $0x0, %eax
jne 0x8d5a
movq 0x20(%rbp), %rax
movl $0x2, (%rax)
jmp 0x8e3c
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x8, %edi
movl $0x4, %esi
callq 0xb3f0
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl %eax, %edx
callq 0x82b0
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x8220
testb $0x1, %al
jne 0x8d9e
jmp 0x8dd8
movq 0x24243(%rip), %rdi     # 0x2cfe8
leaq 0x1556a(%rip), %rsi     # 0x1e316
callq 0x8400
movq %rax, %rdi
movq -0x40(%rbp), %rsi
callq 0x8400
movq %rax, %rdi
leaq 0x152bc(%rip), %rsi     # 0x1e083
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
leaq -0x42(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x8260
movq -0x20(%rbp), %rdi
leaq -0x42(%rbp), %rsi
movl $0x2, %edx
callq 0x8580
movzbl -0x42(%rbp), %ecx
xorl %eax, %eax
cmpl $0x1f, %ecx
movb %al, -0x69(%rbp)
jne 0x8e17
movzbl -0x41(%rbp), %eax
cmpl $0x8b, %eax
sete %al
movb %al, -0x69(%rbp)
movb -0x69(%rbp), %dl
xorl %ecx, %ecx
movl $0x1, %eax
testb $0x1, %dl
cmovnel %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x8600
jmp 0x910b
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x15276(%rip), %rsi     # 0x1e0ca
callq 0x8630
cmpl $0x0, %eax
jne 0x8eda
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl $0x8, %edx
callq 0x82b0
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x8220
testb $0x1, %al
jne 0x8e9b
jmp 0x8ed5
movq 0x24146(%rip), %rdi     # 0x2cfe8
leaq 0x15487(%rip), %rsi     # 0x1e330
callq 0x8400
movq %rax, %rdi
movq -0x50(%rbp), %rsi
callq 0x8400
movq %rax, %rdi
leaq 0x151bf(%rip), %rsi     # 0x1e083
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9109
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x151f9(%rip), %rsi     # 0x1e0e6
callq 0x8630
cmpl $0x0, %eax
jne 0x8f76
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl $0x10, %edx
callq 0x8060
movq -0x28(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x8220
testb $0x1, %al
jne 0x8f34
jmp 0x8f71
movq 0x240ad(%rip), %rdi     # 0x2cfe8
leaq 0x1540f(%rip), %rsi     # 0x1e351
callq 0x8400
movq %rax, %rdi
movq -0x58(%rbp), %rax
movsbl (%rax), %esi
callq 0x8510
movq %rax, %rdi
leaq 0x15123(%rip), %rsi     # 0x1e083
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9107
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1517d(%rip), %rsi     # 0x1e106
callq 0x8630
cmpl $0x0, %eax
jne 0x8fde
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8710
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
cmpl $0x1, (%rax)
jge 0x8fd9
movq 0x24027(%rip), %rdi     # 0x2cfe8
leaq 0x153a4(%rip), %rsi     # 0x1e36c
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9105
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1513f(%rip), %rsi     # 0x1e130
callq 0x8630
cmpl $0x0, %eax
jne 0x9046
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8710
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq 0x10(%rbp), %rax
cmpl $0x1, (%rax)
jge 0x9041
movq 0x23fbf(%rip), %rdi     # 0x2cfe8
leaq 0x15362(%rip), %rsi     # 0x1e392
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9103
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x1510b(%rip), %rsi     # 0x1e164
callq 0x8630
cmpl $0x0, %eax
jne 0x90c2
movq -0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x8560
movq 0x18(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x18(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
ja 0x90a4
movq 0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd 0x14f6a(%rip), %xmm1    # 0x1e008
ucomisd %xmm1, %xmm0
jbe 0x90c0
movq 0x23f3d(%rip), %rdi     # 0x2cfe8
leaq 0x15304(%rip), %rsi     # 0x1e3b6
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9101
movq 0x23f1f(%rip), %rdi     # 0x2cfe8
leaq 0x15305(%rip), %rsi     # 0x1e3d5
callq 0x8400
movq %rax, %rdi
movq -0x10(%rbp), %rax
movslq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x8400
movq %rax, %rdi
leaq 0x14f90(%rip), %rsi     # 0x1e083
callq 0x8400
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x911f
jmp 0x9103
jmp 0x9105
jmp 0x9107
jmp 0x9109
jmp 0x910b
movl -0x38(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
jmp 0x8cf4
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)