// File: 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
subq $0xf0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movl $0x2, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
leaq -0x68(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x8e10
movq -0xa0(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x90e0
movq %rax, -0x98(%rbp)
jmp 0x3cfd
movq -0x98(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9110
movq %rax, -0x88(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x90b0
testb $0x1, %al
jne 0x3d31
jmp 0x3db2
movq 0x112b8(%rip), %rdi     # 0x14ff0
leaq 0xbead(%rip), %rsi      # 0xfbec
callq 0x32c0
movq %rax, -0xa8(%rbp)
jmp 0x3d4d
movq -0xa8(%rbp), %rdi
movq 0x30(%rbp), %rsi
callq 0x3280
movq %rax, -0xb0(%rbp)
jmp 0x3d66
movq -0xb0(%rbp), %rdi
leaq 0xb318(%rip), %rsi      # 0xf08c
callq 0x32c0
jmp 0x3d7b
callq 0x3720
jmp 0x3d82
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9220
jmp 0x4319
movq 0x30(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x9140
movq %rax, -0xb8(%rbp)
jmp 0x3dc8
movq -0xb8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xbc(%rbp)
subl $0x2, %eax
je 0x3dfb
jmp 0x3dde
movl -0xbc(%rbp), %eax
subl $0x3, %eax
je 0x3e32
jmp 0x3deb
movl -0xbc(%rbp), %eax
subl $0x4, %eax
je 0x3e63
jmp 0x3e8e
cmpl $0x6, -0x8(%rbp)
jge 0x3e30
movq -0x28(%rbp), %rax
movl (%rax), %edi
movq -0x30(%rbp), %rax
movl (%rax), %esi
movq 0x18(%rbp), %rax
movl (%rax), %edx
callq 0x5160
jmp 0x3e1a
movl $0x1, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x3e8e
cmpl $0x6, -0x8(%rbp)
jge 0x3e61
movq -0x28(%rbp), %rax
movl (%rax), %edi
movq -0x30(%rbp), %rax
movl (%rax), %esi
callq 0x55c0
jmp 0x3e4b
movl $0x1, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x3e8e
cmpl $0x4, -0x8(%rbp)
jge 0x3e8c
movq 0x20(%rbp), %rax
movl (%rax), %edi
callq 0x58c0
jmp 0x3e76
movl $0x1, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x3e8e
jmp 0x3e90
movl -0x34(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x42f3
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb1f6(%rip), %rsi      # 0xf0a5
callq 0x33e0
cmpl $0x0, %eax
jne 0x3f6c
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
movl $0x8, %edx
callq 0x32a0
jmp 0x3eda
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3170
movb %al, -0xbd(%rbp)
jmp 0x3ef5
movb -0xbd(%rbp), %al
testb $0x1, %al
jne 0x3f01
jmp 0x3f6a
movq 0x110e8(%rip), %rdi     # 0x14ff0
leaq 0xb19c(%rip), %rsi      # 0xf0ab
callq 0x32c0
movq %rax, -0xc8(%rbp)
jmp 0x3f1d
movq -0xc8(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
callq 0x32c0
movq %rax, -0xd0(%rbp)
jmp 0x3f3f
movq -0xd0(%rbp), %rdi
leaq 0xbc9f(%rip), %rsi      # 0xfbec
callq 0x32c0
jmp 0x3f54
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x3f6c
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb152(%rip), %rsi      # 0xf0d1
callq 0x33e0
cmpl $0x0, %eax
jne 0x4054
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
movq 0x10(%rbp), %rdi
callq 0x33c0
jmp 0x3fa5
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
movl $0x8, %edx
callq 0x32a0
jmp 0x3fc2
movq -0x20(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3170
movb %al, -0xd1(%rbp)
jmp 0x3fdd
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0x3fe9
jmp 0x4052
movq 0x11000(%rip), %rdi     # 0x14ff0
leaq 0xb0e0(%rip), %rsi      # 0xf0d7
callq 0x32c0
movq %rax, -0xe0(%rbp)
jmp 0x4005
movq -0xe0(%rbp), %rdi
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rsi
callq 0x32c0
movq %rax, -0xe8(%rbp)
jmp 0x4027
movq -0xe8(%rbp), %rdi
leaq 0xbbb7(%rip), %rsi      # 0xfbec
callq 0x32c0
jmp 0x403c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x4054
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb08e(%rip), %rsi      # 0xf0f5
callq 0x33e0
cmpl $0x0, %eax
jne 0x40c5
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3430
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, (%rax)
jae 0x40c3
movq 0x10f51(%rip), %rdi     # 0x14ff0
leaq 0xb057(%rip), %rsi      # 0xf0fd
callq 0x32c0
jmp 0x40ad
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x40c5
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb043(%rip), %rsi      # 0xf11b
callq 0x33e0
cmpl $0x0, %eax
jne 0x4136
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3430
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jae 0x4134
movq 0x10ee0(%rip), %rdi     # 0x14ff0
leaq 0xb00a(%rip), %rsi      # 0xf121
callq 0x32c0
jmp 0x411e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x4136
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xaff4(%rip), %rsi      # 0xf13d
callq 0x33e0
cmpl $0x0, %eax
jne 0x41bd
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x3430
movl %eax, %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
movq 0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xec(%rbp)
testl %eax, %eax
je 0x418c
jmp 0x417f
movl -0xec(%rbp), %eax
subl $0x1, %eax
je 0x418e
jmp 0x4190
jmp 0x41bb
jmp 0x41bb
movq 0x10e59(%rip), %rdi     # 0x14ff0
leaq 0xafa8(%rip), %rsi      # 0xf146
callq 0x32c0
jmp 0x41a5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x41bd
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xafd2(%rip), %rsi      # 0xf1a2
callq 0x33e0
cmpl $0x0, %eax
jne 0x4253
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq 0x8(%rax,%rcx,8), %rdi
callq 0x3430
movl %eax, %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movq 0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xf0(%rbp)
testl %eax, %eax
je 0x4220
jmp 0x4206
movl -0xf0(%rbp), %eax
subl $0x1, %eax
je 0x4222
jmp 0x4213
movl -0xf0(%rbp), %eax
subl $0x2, %eax
je 0x4224
jmp 0x4226
jmp 0x4251
jmp 0x4251
jmp 0x4251
movq 0x10dc3(%rip), %rdi     # 0x14ff0
leaq 0xaf7a(%rip), %rsi      # 0xf1ae
callq 0x32c0
jmp 0x423b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x4253
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xafb7(%rip), %rsi      # 0xf21d
callq 0x33e0
cmpl $0x0, %eax
jne 0x42db
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x3380
movq 0x28(%rbp), %rax
movsd %xmm0, (%rax)
movq 0x28(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd (%rax), %xmm0
ja 0x42b1
movq 0x28(%rbp), %rax
movsd (%rax), %xmm0
movsd 0xad5d(%rip), %xmm1     # 0xf008
ucomisd %xmm1, %xmm0
jbe 0x42d9
movq 0x10d38(%rip), %rdi     # 0x14ff0
leaq 0xaf68(%rip), %rsi      # 0xf227
callq 0x32c0
jmp 0x42c6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x8c(%rbp)
jmp 0x4304
jmp 0x42db
movl -0x38(%rbp), %eax
addl $0x2, %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
jmp 0x3e90
movl $0x0, -0x4(%rbp)
movl $0x1, -0x8c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x9220
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x34c0
nopw %cs:(%rax,%rax)