// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %r8, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rsi, %rbx
movl %edi, -0x2c(%rbp)
movq 0x30(%rbp), %r15
leaq -0x80(%rbp), %r12
movq %r12, %rdi
callq 0x7fd0
movq %r12, %rdi
movq %r15, %rsi
callq 0x83f4
leaq -0x78(%rbp), %rcx
cmpq %rcx, %rax
je 0x3a87
leaq -0x80(%rbp), %rdi
movq %r15, %rsi
callq 0x8146
movl (%rax), %eax
cmpl $0x4, %eax
je 0x3af4
cmpl $0x3, %eax
je 0x3ad5
cmpl $0x2, %eax
jne 0x3b0f
cmpl $0x5, -0x2c(%rbp)
jg 0x3b0f
movq -0x38(%rbp), %rax
movl (%rax), %edi
movl (%r13), %esi
movq 0x18(%rbp), %rax
movl (%rax), %edx
movl $0x1, %ebx
callq 0x4a20
jmp 0x3d94
movq 0xa562(%rip), %rdi      # 0xdff0
leaq 0x711d(%rip), %rsi      # 0xabb2
movl $0x1, %edx
callq 0x3250
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq 0xa543(%rip), %rdi      # 0xdff0
callq 0x3250
leaq 0x65d3(%rip), %rsi      # 0xa08c
movl $0x18, %edx
movq %rax, %rdi
callq 0x3250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0x35d2
jmp 0x3d94
cmpl $0x5, -0x2c(%rbp)
jg 0x3b0f
movq -0x38(%rbp), %rax
movl (%rax), %edi
movl (%r13), %esi
movl $0x1, %ebx
callq 0x4e99
jmp 0x3d94
cmpl $0x3, -0x2c(%rbp)
jg 0x3b0f
movq 0x20(%rbp), %rax
movl (%rax), %edi
movl $0x1, %ebx
callq 0x51d5
jmp 0x3d94
cmpl $0x3, -0x2c(%rbp)
jl 0x3cd5
movq %r13, -0x50(%rbp)
movl $0x2, %r13d
leaq 0x657b(%rip), %r14      # 0xa0a5
leaq 0x65a0(%rip), %r12      # 0xa0d1
movq (%rbx,%r13,8), %rdi
movq %r14, %rsi
callq 0x3290
testl %eax, %eax
jne 0x3b6a
movq 0x8(%rbx,%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 0x3cdc
movq (%rbx,%r13,8), %rdi
movq %r12, %rsi
callq 0x3290
testl %eax, %eax
jne 0x3bdf
movq 0x8(%rbx,%r13,8), %r15
movq %rbx, %r12
movq 0x10(%rbp), %rbx
movq 0x8(%rbx), %r14
movq %r15, %rdi
callq 0x3110
movq %rbx, %rdi
movq %r12, %rbx
leaq 0x6532(%rip), %r12      # 0xa0d1
xorl %esi, %esi
movq %r14, %rdx
leaq 0x64fa(%rip), %r14      # 0xa0a5
movq %r15, %rcx
movq %rax, %r8
callq 0x32e0
movq 0x8(%rbx,%r13,8), %rsi
movq -0x40(%rbp), %rdi
movl $0x8, %edx
callq 0x31f0
movq -0x40(%rbp), %rcx
movq (%rcx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rcx,%rax)
jne 0x3cf6
movq (%rbx,%r13,8), %r15
movq %r15, %rdi
leaq 0x6508(%rip), %rsi      # 0xa0f5
callq 0x3290
testl %eax, %eax
jne 0x3c06
movq 0x8(%rbx,%r13,8), %rdi
callq 0x32f0
movq -0x38(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x64ed(%rip), %rsi      # 0xa0fd
callq 0x3290
testl %eax, %eax
jne 0x3c29
movq 0x8(%rbx,%r13,8), %rdi
callq 0x32f0
movq -0x50(%rbp), %rcx
movl %eax, (%rcx)
movq %r15, %rdi
leaq 0x64d0(%rip), %rsi      # 0xa103
callq 0x3290
testl %eax, %eax
jne 0x3c55
movq 0x8(%rbx,%r13,8), %rdi
callq 0x32f0
movq 0x20(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x1, %eax
ja 0x3d34
movq %r15, %rdi
leaq 0x6509(%rip), %rsi      # 0xa168
callq 0x3290
testl %eax, %eax
jne 0x3c81
movq 0x8(%rbx,%r13,8), %rdi
callq 0x32f0
movq 0x18(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x2, %eax
ja 0x3d42
movq %r15, %rdi
leaq 0x6558(%rip), %rsi      # 0xa1e3
callq 0x3290
testl %eax, %eax
jne 0x3cc7
movq 0x8(%rbx,%r13,8), %rdi
callq 0x3270
movq 0x28(%rbp), %rax
movsd %xmm0, (%rax)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
movl $0x22, %edx
ja 0x3d50
ucomisd 0x6347(%rip), %xmm0     # 0xa008
ja 0x3d50
addq $0x2, %r13
cmpl -0x2c(%rbp), %r13d
jl 0x3b31
xorl %ebx, %ebx
jmp 0x3d94
movq 0xa30d(%rip), %rdi      # 0xdff0
leaq 0x63c1(%rip), %rsi      # 0xa0ab
movl $0x25, %edx
callq 0x3250
jmp 0x3d0e
movq 0xa2f3(%rip), %rdi      # 0xdff0
leaq 0x63d3(%rip), %rsi      # 0xa0d7
movl $0x1d, %edx
callq 0x3250
movq 0x8(%rbx,%r13,8), %rbx
testq %rbx, %rbx
je 0x3d59
movq %rbx, %rdi
callq 0x3110
movq 0xa2c9(%rip), %rdi      # 0xdff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3250
jmp 0x3d77
movl $0x5b, %edx
leaq 0x63cc(%rip), %rsi      # 0xa10c
jmp 0x3d83
movl $0x6e, %edx
leaq 0x6426(%rip), %rsi      # 0xa174
jmp 0x3d83
leaq 0x6496(%rip), %rsi      # 0xa1ed
jmp 0x3d83
movq 0xa290(%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 0x6e2f(%rip), %rsi      # 0xabb2
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq 0xa261(%rip), %rdi      # 0xdff0
callq 0x3250
leaq -0x80(%rbp), %rdi
callq 0x82ca
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3db2
jmp 0x3db2
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x82ca
movq %rbx, %rdi
callq 0x3390