// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %r9, -0x38(%rbp)
movq %r8, -0x30(%rbp)
movq %rcx, -0x40(%rbp)
movq %rdx, %r13
movq %rsi, %rbx
movl %edi, %r14d
movq 0x30(%rbp), %r15
leaq -0x70(%rbp), %r12
movq %r12, %rdi
callq 0x6c34
movq %r12, %rdi
movq %r15, %rsi
callq 0x702e
leaq -0x68(%rbp), %rcx
cmpq %rcx, %rax
je 0x39a4
leaq -0x70(%rbp), %rdi
movq %r15, %rsi
callq 0x6d58
movl (%rax), %eax
cmpl $0x4, %eax
je 0x39f4
cmpl $0x3, %eax
je 0x39db
cmpl $0x2, %eax
jne 0x3a0c
cmpl $0x5, %r14d
jg 0x3a0c
movq -0x30(%rbp), %rax
movl (%rax), %edi
movq -0x38(%rbp), %rax
movl (%rax), %esi
movq 0x18(%rbp), %rax
movl (%rax), %edx
callq 0x44f8
jmp 0x3a05
movq 0x9645(%rip), %rdi      # 0xcff0
leaq 0x6200(%rip), %rsi      # 0x9bb2
callq 0x3220
movq %rax, %rdi
movq %r15, %rsi
callq 0x31f0
leaq 0x56c3(%rip), %rsi      # 0x908c
movq %rax, %rdi
callq 0x3220
callq 0x3592
jmp 0x3bee
cmpl $0x5, %r14d
jg 0x3a0c
movq -0x30(%rbp), %rax
movl (%rax), %edi
movq -0x38(%rbp), %rax
movl (%rax), %esi
callq 0x488c
jmp 0x3a05
cmpl $0x3, %r14d
jg 0x3a0c
movq 0x20(%rbp), %rax
movl (%rax), %edi
callq 0x4b27
pushq $0x1
jmp 0x3bf0
movslq %r14d, %r12
addq $0x18, %rbx
pushq $0x2
popq %r14
cmpq %r12, %r14
jge 0x3b7f
movq -0x8(%rbx), %r15
movq %r15, %rdi
leaq 0x5677(%rip), %rsi      # 0x90a5
callq 0x32b0
testl %eax, %eax
jne 0x3a5d
movq (%rbx), %rsi
movq %r13, %rdi
pushq $0x8
popq %rdx
callq 0x3210
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%r13,%rax)
jne 0x3b83
movq -0x8(%rbx), %r15
movq %r15, %rdi
leaq 0x566a(%rip), %rsi      # 0x90d1
callq 0x32b0
testl %eax, %eax
jne 0x3aa5
movq (%rbx), %rsi
movq 0x10(%rbp), %rdi
callq 0x33f0
movq (%rbx), %rsi
movq -0x40(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x3210
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rcx,%rax)
jne 0x3b98
movq -0x8(%rbx), %r15
movq %r15, %rdi
leaq 0x5646(%rip), %rsi      # 0x90f5
callq 0x32b0
testl %eax, %eax
jne 0x3ac6
movq (%rbx), %rdi
callq 0x3300
movq -0x30(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x562d(%rip), %rsi      # 0x90fd
callq 0x32b0
testl %eax, %eax
jne 0x3ae7
movq (%rbx), %rdi
callq 0x3300
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x5612(%rip), %rsi      # 0x9103
callq 0x32b0
testl %eax, %eax
jne 0x3b11
movq (%rbx), %rdi
callq 0x3300
movq 0x20(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x1, %eax
ja 0x3bc2
movq %r15, %rdi
leaq 0x564d(%rip), %rsi      # 0x9168
callq 0x32b0
testl %eax, %eax
jne 0x3b3b
movq (%rbx), %rdi
callq 0x3300
movq 0x18(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x2, %eax
ja 0x3bd2
movq %r15, %rdi
leaq 0x569e(%rip), %rsi      # 0x91e3
callq 0x32b0
testl %eax, %eax
jne 0x3b72
movq (%rbx), %rdi
callq 0x3270
movq 0x28(%rbp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x3bdb
ucomisd 0x5498(%rip), %xmm0     # 0x9008
ja 0x3bdb
addq $0x2, %r14
addq $0x10, %rbx
jmp 0x3a17
xorl %ebx, %ebx
jmp 0x3bf1
movq 0x9466(%rip), %rdi      # 0xcff0
leaq 0x551a(%rip), %rsi      # 0x90ab
callq 0x3220
jmp 0x3bab
movq 0x9451(%rip), %rdi      # 0xcff0
leaq 0x5531(%rip), %rsi      # 0x90d7
callq 0x3220
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x3220
movq %rax, %rdi
leaq 0x5ff2(%rip), %rsi      # 0x9bb2
jmp 0x3be9
movq 0x9427(%rip), %rdi      # 0xcff0
leaq 0x553c(%rip), %rsi      # 0x910c
jmp 0x3be9
leaq 0x559b(%rip), %rsi      # 0x9174
jmp 0x3be2
leaq 0x560b(%rip), %rsi      # 0x91ed
movq 0x9407(%rip), %rdi      # 0xcff0
callq 0x3220
pushq $-0x1
popq %rbx
leaq -0x70(%rbp), %rdi
callq 0x6f14
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c0f
jmp 0x3c0f
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
callq 0x6f14
movq %rbx, %rdi
callq 0x33a0