// File: /workspace/github_cpp_makefile_star2_O3/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 0x79a0
movl $0x1, %eax
jmp 0x6d17
movq %rdx, %r13
movq %rsi, %rbx
movl %edi, %ebp
movq %r9, 0x10(%rsp)
movl $0x1, %r15d
movq %rsi, 0x18(%rsp)
movq (%rbx,%r15,8), %r14
movq %r14, %rdi
leaq 0x86f9(%rip), %rsi      # 0xf08d
callq 0x6430
testl %eax, %eax
je 0x6a48
movq %r14, %rdi
leaq 0x8727(%rip), %rsi      # 0xf0d2
callq 0x6430
testl %eax, %eax
je 0x6ac7
movq %r14, %rdi
leaq 0x872c(%rip), %rsi      # 0xf0ee
callq 0x6430
testl %eax, %eax
je 0x6af5
movq %r14, %rdi
leaq 0x8735(%rip), %rsi      # 0xf10e
callq 0x6430
testl %eax, %eax
je 0x6b3b
movq %r14, %rdi
leaq 0x8748(%rip), %rsi      # 0xf138
callq 0x6430
testl %eax, %eax
je 0x6b62
movq %r14, %rdi
leaq 0x8765(%rip), %rsi      # 0xf16c
callq 0x6430
testl %eax, %eax
jne 0x6c52
movq 0x8(%rbx,%r15,8), %rdi
callq 0x63d0
movq 0x68(%rsp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x6c92
ucomisd 0x85cb(%rip), %xmm0     # 0xf008
jbe 0x6b7b
jmp 0x6c92
movq 0x8(%rbx,%r15,8), %r14
cmpb $0x2d, (%r14)
jne 0x6a5e
cmpb $0x0, 0x1(%r14)
je 0x6b55
movq %r12, %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x61d0
movq (%r12), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r12,%rax)
jne 0x6b8f
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 0x6b7b
movq 0x8(%rbx,%r15,8), %r14
movq %r13, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x61d0
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r13,%rax)
je 0x6b7b
jmp 0x6bd4
movq 0x8(%rbx,%r15,8), %r14
movq %r12, %rbx
movl %ebp, %r12d
movq %r13, %rbp
movq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r14, %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 0x6b7b
jmp 0x6c0c
movq 0x8(%rbx,%r15,8), %rdi
callq 0x64e0
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jg 0x6b7b
jmp 0x6ca7
movq 0x70(%rsp), %rax
movl $0x2, (%rax)
jmp 0x6b7b
movq 0x8(%rbx,%r15,8), %rdi
callq 0x64e0
movq 0x60(%rsp), %rcx
movl %eax, (%rcx)
testl %eax, %eax
jle 0x6ccd
addq $0x2, %r15
cmpl %ebp, %r15d
jl 0x6986
xorl %eax, %eax
jmp 0x6d17
movq 0x10452(%rip), %rbx     # 0x16fe8
leaq 0x877f(%rip), %rsi      # 0xf31c
movl $0x19, %edx
movq %rbx, %rdi
callq 0x6390
movq %r14, %rdi
callq 0x6160
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x6390
leaq 0x84c4(%rip), %rsi      # 0xf08b
movl $0x1, %edx
movq %rbx, %rdi
jmp 0x6d0d
movq 0x1040d(%rip), %rbx     # 0x16fe8
leaq 0x8754(%rip), %rsi      # 0xf336
movl $0x20, %edx
movq %rbx, %rdi
callq 0x6390
testq %r14, %r14
je 0x6cbc
movq %r14, %rdi
callq 0x6160
movq 0x103e1(%rip), %rdi     # 0x16fe8
movq %r14, %rsi
jmp 0x6c88
movq 0x103d5(%rip), %rbx     # 0x16fe8
leaq 0x873d(%rip), %rsi      # 0xf357
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x6390
movb (%r14), %al
leaq 0xf(%rsp), %rsi
movb %al, (%rsi)
movl $0x1, %edx
movq %rbx, %rdi
callq 0x6390
leaq 0x8446(%rip), %rsi      # 0xf08b
movl $0x1, %edx
movq %rax, %rdi
jmp 0x6d0d
movq 0x1038f(%rip), %r14     # 0x16fe8
leaq 0x877b(%rip), %rsi      # 0xf3db
movl $0x11, %edx
movq %r14, %rdi
callq 0x6390
movq (%rbx,%r15,8), %rbx
testq %rbx, %rbx
je 0x6ce2
movq %rbx, %rdi
callq 0x6160
movq 0x10363(%rip), %rdi     # 0x16fe8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x6390
jmp 0x6cfa
movq 0x1034f(%rip), %rdi     # 0x16fe8
leaq 0x871c(%rip), %rsi      # 0xf3bc
movl $0x1e, %edx
jmp 0x6d0d
movq 0x1033a(%rip), %rdi     # 0x16fe8
leaq 0x86bd(%rip), %rsi      # 0xf372
movl $0x25, %edx
jmp 0x6d0d
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
jmp 0x6cf2
movq 0x10314(%rip), %rdi     # 0x16fe8
leaq 0x86bd(%rip), %rsi      # 0xf398
movl $0x23, %edx
jmp 0x6d0d
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x6550
movq 0x102e7(%rip), %rdi     # 0x16fe8
leaq 0x8383(%rip), %rsi      # 0xf08b
movl $0x1, %edx
callq 0x6390
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq