// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, %r13
movq 0x68(%rsp), %rax
movq 0x60(%rsp), %rcx
cmpl $0x5, %edi
jge 0x6920
movl (%r9), %edi
movl (%rcx), %esi
movsd (%rax), %xmm0
callq 0x7579
pushq $0x1
jmp 0x6b7a
movq %rdx, %rbp
movq %rsi, %r14
movq %r9, 0x8(%rsp)
movq 0x70(%rsp), %r12
movl %edi, %eax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rbx
movq %rsi, 0x10(%rsp)
cmpq 0x20(%rsp), %rbx
jae 0x6b37
movq (%r14,%rbx,8), %r15
movq %r15, %rdi
leaq 0x7735(%rip), %rsi      # 0xe08d
callq 0x6460
testl %eax, %eax
je 0x6a0c
movq %r15, %rdi
leaq 0x7763(%rip), %rsi      # 0xe0d2
callq 0x6460
testl %eax, %eax
je 0x6a83
movq %r15, %rdi
leaq 0x7768(%rip), %rsi      # 0xe0ee
callq 0x6460
testl %eax, %eax
je 0x6aae
movq %r15, %rdi
leaq 0x7771(%rip), %rsi      # 0xe10e
callq 0x6460
testl %eax, %eax
je 0x6af1
movq %r15, %rdi
leaq 0x7784(%rip), %rsi      # 0xe138
callq 0x6460
testl %eax, %eax
je 0x6b15
movq %r15, %rdi
leaq 0x77a1(%rip), %rsi      # 0xe16c
callq 0x6460
testl %eax, %eax
jne 0x6bab
movq 0x8(%r14,%rbx,8), %rdi
callq 0x63e0
movq 0x68(%rsp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x6bc7
ucomisd 0x7607(%rip), %xmm0     # 0xe008
jbe 0x6b2e
jmp 0x6bc7
movq 0x8(%r14,%rbx,8), %r15
cmpb $0x2d, (%r15)
jne 0x6a22
cmpb $0x0, 0x1(%r15)
je 0x6b0b
movq %r13, %rdi
movq %r15, %rsi
pushq $0xc
popq %rdx
callq 0x6210
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r13,%rax)
jne 0x6b3b
andw $0x0, 0x6(%rsp)
movq %r13, %rdi
leaq 0x6(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x63f0
movb 0x6(%rsp), %al
movb 0x7(%rsp), %cl
xorb $0x1f, %al
xorb $-0x75, %cl
xorl %edx, %edx
orb %al, %cl
sete %dl
movl %edx, (%r12)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6440
jmp 0x6b2e
movq 0x8(%r14,%rbx,8), %r15
movq %rbp, %rdi
movq %r15, %rsi
pushq $0x8
popq %rdx
callq 0x6210
movq (%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rbp,%rax)
je 0x6b2e
jmp 0x6b4b
movq 0x8(%r14,%rbx,8), %r15
movq %r13, %r14
movq %r12, %r13
movq %rbp, %r12
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
pushq $0x10
popq %rdx
callq 0x6050
movq (%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rbp,%rax)
movq %r12, %rbp
movq %r13, %r12
movq %r14, %r13
movq 0x10(%rsp), %r14
je 0x6b2e
jmp 0x6b8a
movq 0x8(%r14,%rbx,8), %rdi
callq 0x6510
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jg 0x6b2e
jmp 0x6bd7
movl $0x2, (%r12)
jmp 0x6b2e
movq 0x8(%r14,%rbx,8), %rdi
callq 0x6510
movq 0x60(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jle 0x6be7
addq $0x2, %rbx
jmp 0x693f
xorl %eax, %eax
jmp 0x6b7b
movq 0x104a6(%rip), %rdi     # 0x16fe8
leaq 0x77d3(%rip), %rsi      # 0xe31c
jmp 0x6b59
movq 0x10496(%rip), %rdi     # 0x16fe8
leaq 0x77dd(%rip), %rsi      # 0xe336
callq 0x6320
movq %rax, %rdi
movq %r15, %rsi
callq 0x6320
leaq 0x751b(%rip), %rsi      # 0xe08b
movq %rax, %rdi
callq 0x6320
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10457(%rip), %rdi     # 0x16fe8
leaq 0x77bf(%rip), %rsi      # 0xe357
callq 0x6320
movsbl (%r15), %esi
movq %rax, %rdi
callq 0x63a0
jmp 0x6b69
movq 0x10436(%rip), %rdi     # 0x16fe8
leaq 0x7822(%rip), %rsi      # 0xe3db
callq 0x6320
movq (%r14,%rbx,8), %rsi
movq %rax, %rdi
jmp 0x6b64
movq 0x1041a(%rip), %rdi     # 0x16fe8
leaq 0x77e7(%rip), %rsi      # 0xe3bc
jmp 0x6b73
movq 0x1040a(%rip), %rdi     # 0x16fe8
leaq 0x778d(%rip), %rsi      # 0xe372
jmp 0x6b73
movq 0x103fa(%rip), %rdi     # 0x16fe8
leaq 0x77a3(%rip), %rsi      # 0xe398
jmp 0x6b73