// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movq %rcx, %r12
movq 0x68(%rsp), %rax
movq 0x60(%rsp), %rcx
cmpl $0x5, %edi
jge 0x696e
movl (%r9), %edi
movl (%rcx), %esi
movsd (%rax), %xmm0
callq 0x7978
movl $0x1, %eax
jmp 0x6cda
movq %rdx, %r13
movq %rsi, %rbx
movl %edi, %ebp
movq %r9, 0x10(%rsp)
movl $0x1, %r14d
movq %rsi, 0x18(%rsp)
movq (%rbx,%r14,8), %r15
movq %r15, %rdi
leaq 0x86f9(%rip), %rsi      # 0xf08d
callq 0x6430
testl %eax, %eax
je 0x6a48
movq %r15, %rdi
leaq 0x8727(%rip), %rsi      # 0xf0d2
callq 0x6430
testl %eax, %eax
je 0x6acd
movq %r15, %rdi
leaq 0x872c(%rip), %rsi      # 0xf0ee
callq 0x6430
testl %eax, %eax
je 0x6afb
movq %r15, %rdi
leaq 0x8735(%rip), %rsi      # 0xf10e
callq 0x6430
testl %eax, %eax
je 0x6b4e
movq %r15, %rdi
leaq 0x8748(%rip), %rsi      # 0xf138
callq 0x6430
testl %eax, %eax
je 0x6b68
movq %r15, %rdi
leaq 0x8765(%rip), %rsi      # 0xf16c
callq 0x6430
testl %eax, %eax
jne 0x6c58
movq 0x8(%rbx,%r14,8), %rdi
callq 0x63d0
movq 0x68(%rsp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x6c93
ucomisd 0x85cb(%rip), %xmm0     # 0xf008
jbe 0x6b81
jmp 0x6c93
movq 0x8(%rbx,%r14,8), %r15
movq %r15, %rdi
leaq 0x88c5(%rip), %rsi      # 0xf31c
callq 0x6430
testl %eax, %eax
je 0x6b41
movq %r12, %rdi
movq %r15, %rsi
movl $0xc, %edx
callq 0x61d0
movq (%r12), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r12,%rax)
jne 0x6baa
movw $0x0, 0xc(%rsp)
movl $0x2, %edx
movq %r12, %rdi
leaq 0xc(%rsp), %rsi
callq 0x63e0
movb 0xc(%rsp), %al
movb 0xd(%rsp), %cl
xorb $0x1f, %al
xorb $-0x75, %cl
xorl %edx, %edx
orb %al, %cl
sete %dl
movq 0x70(%rsp), %rax
movl %edx, (%rax)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6420
jmp 0x6b81
movq 0x8(%rbx,%r14,8), %r15
movq %r13, %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x61d0
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r13,%rax)
je 0x6b81
jmp 0x6b95
movq 0x8(%rbx,%r14,8), %r15
movq %r12, %rbx
movl %ebp, %r12d
movq %r13, %rbp
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl $0x10, %edx
callq 0x6060
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r13,%rax)
movq %rbp, %r13
movl %r12d, %ebp
movq %rbx, %r12
movq 0x18(%rsp), %rbx
je 0x6b81
jmp 0x6c15
movq 0x70(%rsp), %rax
movl $0x2, (%rax)
jmp 0x6b81
movq 0x8(%rbx,%r14,8), %rdi
callq 0x64e0
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jg 0x6b81
jmp 0x6ca8
movq 0x8(%rbx,%r14,8), %rdi
callq 0x64e0
movq 0x60(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jle 0x6cbd
addq $0x2, %r14
cmpl %ebp, %r14d
jl 0x6986
xorl %eax, %eax
jmp 0x6cda
movq 0x1044c(%rip), %rbx     # 0x16fe8
leaq 0x8795(%rip), %rsi      # 0xf338
movl $0x20, %edx
jmp 0x6bbd
movq 0x10437(%rip), %rbx     # 0x16fe8
leaq 0x8766(%rip), %rsi      # 0xf31e
movl $0x19, %edx
movq %rbx, %rdi
callq 0x6390
testq %r15, %r15
je 0x6be6
movq %r15, %rdi
callq 0x6160
movq 0x1040f(%rip), %rdi     # 0x16fe8
movq %r15, %rsi
movq %rax, %rdx
callq 0x6390
jmp 0x6bfd
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x6550
movq 0x103e4(%rip), %rdi     # 0x16fe8
leaq 0x8480(%rip), %rsi      # 0xf08b
movl $0x1, %edx
jmp 0x6cd0
movq 0x103cc(%rip), %rbx     # 0x16fe8
leaq 0x8736(%rip), %rsi      # 0xf359
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x6390
movb (%r15), %al
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x6390
leaq 0x843d(%rip), %rsi      # 0xf08b
movl $0x1, %edx
movq %rax, %rdi
jmp 0x6cd0
movq 0x10389(%rip), %r15     # 0x16fe8
leaq 0x8777(%rip), %rsi      # 0xf3dd
movl $0x11, %edx
movq %r15, %rdi
callq 0x6390
movq (%rbx,%r14,8), %rbx
testq %rbx, %rbx
je 0x6ce9
movq %rbx, %rdi
callq 0x6160
movq 0x1035d(%rip), %rdi     # 0x16fe8
movq %rbx, %rsi
jmp 0x6bdc
movq 0x1034e(%rip), %rdi     # 0x16fe8
leaq 0x871d(%rip), %rsi      # 0xf3be
movl $0x1e, %edx
jmp 0x6cd0
movq 0x10339(%rip), %rdi     # 0x16fe8
leaq 0x86be(%rip), %rsi      # 0xf374
movl $0x25, %edx
jmp 0x6cd0
movq 0x10324(%rip), %rdi     # 0x16fe8
leaq 0x86cf(%rip), %rsi      # 0xf39a
movl $0x23, %edx
callq 0x6390
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %rdi
movl 0x20(%r15,%rax), %esi
jmp 0x6bf5