// File: /workspace/github_cpp_makefile_star2_O3/tplinderoth[P]introDist/introDist.cpp
// Function: parseArgs(int, char**, std::basic_fstream<char, std::char_traits<char>>&, std::basic_fstream<char, std::char_traits<char>>&, unsigned int&, unsigned int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, int&, int&, double&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
int parseArgs (int c, char **v, std::fstream &freqfile, std::fstream &genofile, unsigned int &window, unsigned int &step,
		std::string* genofname, int &useAvg, int &avgtype, double &distance, std::string* dist_type) {
	int argpos = 2;
	int increment = 0;

	// check distance function
	DistCodes d;

	if (d.codes.find(*dist_type) == d.codes.end()) {
		std::cerr << "\n" << *dist_type << " is an invalid function\n";
		mainInfo();
		return -1;
	}

	switch (d.codes[*dist_type]) {
		case 2 :
			if (c < 6) {
				comp2DistInfo(window, step, useAvg);
				return 1;
			}
			break;
		case 3 :
			if (c < 6) {
				comp3DistInfo(window, step);
				return 1;
			}
			break;
		case 4 :
			if (c < 4) {
				avgDistInfo(avgtype);
				return 1;
			}
			break;
	}


	// parse arguments
	while (argpos < c) {

		if (strcmp(v[argpos], "-freq") == 0) {
				freqfile.open(v[argpos+1], std::ios::in);
				if (! freqfile) {
					std::cerr << "Unable to open allele frequency file " << v[argpos+1] << "\n";
					return -1;
				}
		}

		if (strcmp(v[argpos], "-geno") == 0) {
			*genofname = v[argpos+1];
			genofile.open(v[argpos+1], std::ios::in);
			if (! genofile) {
				std::cerr << "Unable to open genotype file " << v[argpos+1] << "\n";
				return -1;
			}
		}

		if (strcmp(v[argpos], "-window") == 0) {
			window = atoi(v[argpos+1]);
			if (window < 0) {
				std::cerr << "Window size must be positive\n";
				return -1;
			}
		}

		if (strcmp(v[argpos], "-step") == 0) {
			step = atoi(v[argpos+1]);
			if (step < 0) {
				std::cerr << "Step size must be positive\n";
				return -1;
			}
		}

		if (strcmp(v[argpos], "-avgtype") == 0) {
			avgtype = atoi(v[argpos+1]);
			switch (avgtype) {
				case 0 :
					break;
				case 1 :
					break;
				default :
					std::cerr << "-avgtype can only be 0 (distance between pops) or 1 (distance between individual and pop2)\n";
					return -1;
			}
		}

		if (strcmp(v[argpos], "-useAverage") == 0) {
			useAvg = atoi(v[argpos+1]);
			switch (useAvg) {
				case 0 :
					break;
				case 1:
					break;
				case 2:
					break;
				default :
					std::cerr << "-useAverage can only be 0 (region pop distance), 1 (genome pop distance), 2 (genome individual-pop2 distance)\n";
					return -1;
			}
		}

		if (strcmp(v[argpos], "-distance") == 0) {
			distance = atof(v[argpos+1]);
			if (distance < 0 || distance > 1) {
				std::cerr << "Valid -distance is in range [0,1]\n";
				return -1;
			}
		}

		argpos += 2 + increment;
		increment = 0;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, -0x40(%rbp)
movq %r8, -0x38(%rbp)
movq %rcx, -0x48(%rbp)
movq %rdx, -0x50(%rbp)
movq %rsi, %r12
movl %edi, -0x2c(%rbp)
movq 0x30(%rbp), %r15
leaq -0x80(%rbp), %rbx
movq %rbx, %rdi
callq 0x7fc8
movq %rbx, %rdi
movq %r15, %rsi
callq 0x83f6
leaq -0x78(%rbp), %rcx
cmpq %rcx, %rax
je 0x3a8a
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x813e
movl (%rax), %eax
cmpl $0x4, %eax
je 0x3af9
cmpl $0x3, %eax
je 0x3ad8
cmpl $0x2, %eax
jne 0x3b14
cmpl $0x5, -0x2c(%rbp)
jg 0x3b1e
movq -0x38(%rbp), %rax
movl (%rax), %edi
movq -0x40(%rbp), %rax
movl (%rax), %esi
movq 0x18(%rbp), %rax
movl (%rax), %edx
movl $0x1, %ebx
callq 0x4a19
jmp 0x3d95
movq 0xa55f(%rip), %rdi      # 0xdff0
leaq 0x711a(%rip), %rsi      # 0xabb2
movl $0x1, %edx
callq 0x3250
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xa540(%rip), %rdi      # 0xdff0
callq 0x3250
leaq 0x65d0(%rip), %rsi      # 0xa08c
movl $0x18, %edx
movq %rax, %rdi
callq 0x3250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x35d2
jmp 0x3d95
cmpl $0x5, -0x2c(%rbp)
jg 0x3b1e
movq -0x38(%rbp), %rax
movl (%rax), %edi
movq -0x40(%rbp), %rax
movl (%rax), %esi
movl $0x1, %ebx
callq 0x4e92
jmp 0x3d95
cmpl $0x3, -0x2c(%rbp)
jg 0x3b1e
movq 0x20(%rbp), %rax
movl (%rax), %edi
movl $0x1, %ebx
callq 0x51ce
jmp 0x3d95
cmpl $0x3, -0x2c(%rbp)
jl 0x3cd6
movl $0x2, %r13d
leaq 0x657a(%rip), %r14      # 0xa0a5
leaq 0x659f(%rip), %rbx      # 0xa0d1
movq (%r12,%r13,8), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3290
testl %eax, %eax
jne 0x3b72
movq 0x8(%r12,%r13,8), %rsi
movq -0x50(%rbp), %rdi
movl $0x8, %edx
callq 0x31f0
movq -0x50(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rcx,%rax)
jne 0x3cdd
movq (%r12,%r13,8), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3290
testl %eax, %eax
jne 0x3be4
movq 0x8(%r12,%r13,8), %r15
movq %r14, %rbx
movq 0x10(%rbp), %r14
movq 0x8(%r14), %r14
movq %r15, %rdi
callq 0x3110
movq 0x10(%rbp), %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %rbx, %r14
leaq 0x6525(%rip), %rbx      # 0xa0d1
movq %r15, %rcx
movq %rax, %r8
callq 0x32e0
movq 0x8(%r12,%r13,8), %rsi
movq -0x48(%rbp), %rdi
movl $0x8, %edx
callq 0x31f0
movq -0x48(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rcx,%rax)
jne 0x3cf7
movq (%r12,%r13,8), %r15
movq %r15, %rdi
leaq 0x6507(%rip), %rsi      # 0xa0f5
callq 0x3290
testl %eax, %eax
jne 0x3c07
movq 0x8(%r12,%r13,8), %rdi
callq 0x32f0
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x64ec(%rip), %rsi      # 0xa0fd
callq 0x3290
testl %eax, %eax
jne 0x3c2a
movq 0x8(%r12,%r13,8), %rdi
callq 0x32f0
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x64cf(%rip), %rsi      # 0xa103
callq 0x3290
testl %eax, %eax
jne 0x3c56
movq 0x8(%r12,%r13,8), %rdi
callq 0x32f0
movq 0x20(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x1, %eax
ja 0x3d35
movq %r15, %rdi
leaq 0x6508(%rip), %rsi      # 0xa168
callq 0x3290
testl %eax, %eax
jne 0x3c82
movq 0x8(%r12,%r13,8), %rdi
callq 0x32f0
movq 0x18(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x2, %eax
ja 0x3d43
movq %r15, %rdi
leaq 0x6557(%rip), %rsi      # 0xa1e3
callq 0x3290
testl %eax, %eax
jne 0x3cc8
movq 0x8(%r12,%r13,8), %rdi
callq 0x3270
movq 0x28(%rbp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movl $0x22, %edx
ja 0x3d51
ucomisd 0x6346(%rip), %xmm0     # 0xa008
ja 0x3d51
addq $0x2, %r13
cmpl -0x2c(%rbp), %r13d
jl 0x3b32
xorl %ebx, %ebx
jmp 0x3d95
movq 0xa30c(%rip), %rdi      # 0xdff0
leaq 0x63c0(%rip), %rsi      # 0xa0ab
movl $0x25, %edx
callq 0x3250
jmp 0x3d0f
movq 0xa2f2(%rip), %rdi      # 0xdff0
leaq 0x63d2(%rip), %rsi      # 0xa0d7
movl $0x1d, %edx
callq 0x3250
movq 0x8(%r12,%r13,8), %rbx
testq %rbx, %rbx
je 0x3d5a
movq %rbx, %rdi
callq 0x3110
movq 0xa2c8(%rip), %rdi      # 0xdff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3250
jmp 0x3d78
movl $0x5b, %edx
leaq 0x63cb(%rip), %rsi      # 0xa10c
jmp 0x3d84
movl $0x6e, %edx
leaq 0x6425(%rip), %rsi      # 0xa174
jmp 0x3d84
leaq 0x6495(%rip), %rsi      # 0xa1ed
jmp 0x3d84
movq 0xa28f(%rip), %rax      # 0xdff0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rdi
movl 0x20(%rax,%rcx), %esi
orl $0x1, %esi
callq 0x3350
movl $0x1, %edx
leaq 0x6e2e(%rip), %rsi      # 0xabb2
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0xa260(%rip), %rdi      # 0xdff0
callq 0x3250
leaq -0x80(%rbp), %rdi
callq 0x82c8
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3db3
jmp 0x3db3
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x82c8
movq %rbx, %rdi
callq 0x3390