// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/ska.cpp
// Function: distanceSubcommand(int, char**)
int distanceSubcommand(int argc, char *argv[]){

	bool distancefile=true;
	bool clusterfile=true;
	bool includesingletons=false;
	std::string prefix="distances";
	float minid=0.9;
	int maxsnps=20;
	std::vector < std::string > args;

	for (int i=2; i<argc; ++i){

		std::string arg=(argv[i]);

		if (arg=="-h" || arg=="--help"){
			distanceHelp();
			return 0;
		}
		else if (arg=="-c"){
			clusterfile=false;
		}
		else if (arg == "-d"){
			distancefile=false;
		}
		else if (arg == "-f"){
			i++;
			if (i<argc){
				fileToVector(argv[i], args);
			}
			else {
				std::cerr << std::endl << "Expecting file name after -f flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-i"){
			i++;
			if (i<argc){
				try {
					minid = getfloat(argv[i]);
				}
				catch (const std::invalid_argument& e) {
					std::cerr << std::endl << "Expecting float between 0 and 1 after -i flag" << std::endl << std::endl;
					return 1;
				}
			}
			else {
				std::cerr << std::endl << "Expecting float between 0 and 1 after -i flag" << std::endl << std::endl;
				return 1;
			}
			if (minid < 0 || minid > 1){
				std::cerr << std::endl << "Minimum identity must be between 0 and 1" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-o"){
			i++;
			if (i<argc){
				prefix = argv[i];
			}
			else {
				std::cerr << std::endl << "Expecting string after -o flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-s"){
			i++;
			if (i<argc){
				try {
					maxsnps = getint(argv[i]);
				}
				catch (const std::invalid_argument& e) {
					std::cerr << std::endl << "Expecting positive integer after -s flag" << std::endl << std::endl;
					return 1;
				}
			}
			else {
				std::cerr << std::endl << "Expecting positive integer after -s flag" << std::endl << std::endl;
				return 1;
			}
			if (maxsnps < 0){
				std::cerr << std::endl << "Maximum number of SNPs must be 0 or greater" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-S"){
			includesingletons=true;
		}
		else if (arg[0]=='-'){
			std::cerr << std::endl << "Unrecognised flag " << arg << std::endl << std::endl;
				return 1;
		}
		else {
			args.push_back(arg);
		}
	}
	
	if (args.size()<1){
		std::cerr << std::endl << "Too few arguments" << std::endl;
		distanceHelp();
		return 1;
	}

	if (distancefile==false && clusterfile==false){
		std::cerr << std::endl << "At least one output file is required. i.e. you cannot use the -c and -d flags together." << std::endl << std::endl;
		return 1;
	}
	if (prefix=="false"){
		std::cerr << std::endl << "An output prefix (-p) is required." << std::endl << std::endl;
		return 1;
	}

	skaVersion();

	std::cout << "Calculating pairwise distances for ";
	if (args.size()>maxNamesToPrint){
		std::cout << args.size() << " files";
	}
	else {
		for (std::vector < std::string >::iterator it = args.begin(); it != args.end(); ++it){
			std::cout << *it << " ";
		}
	}
	std::cout << std::endl;


	std::cout << "Clusters will be created for isolates that are within " << maxsnps << " SNPs of one another and share at least " << minid*100 << "% of the split kmers of the isolate with fewer kmers" << std::endl;

	if (distancefile){
		std::cout << "Distances will be written to " << prefix << ".distances.tsv" << std::endl;
	}
	if (clusterfile){
		std::cout << "Clusters will be written to files with the prefix " << prefix << std::endl;
	}
	std::cout << std::endl;

	if (kmerDistance(prefix, distancefile, clusterfile, args, maxsnps, minid, includesingletons)){return 1;}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %rbp
movl %edi, 0x4(%rsp)
leaq 0x61a0(%rip), %rsi      # 0x25995
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x77c0
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
pushq $0x2
popq %rbx
pushq $0x14
popq %rax
movl %eax, 0x18(%rsp)
movss 0x70d6(%rip), %xmm0     # 0x268fc
movss %xmm0, 0x8(%rsp)
movb $0x1, %al
movl %eax, 0x1c(%rsp)
movl $0x0, 0x14(%rsp)
leaq 0x68(%rsp), %r12
leaq 0x88(%rsp), %r14
movb $0x1, %al
movl %eax, 0xc(%rsp)
cmpl 0x4(%rsp), %ebx
jge 0x1fc3a
movslq %ebx, %rax
movq (%rbp,%rax,8), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x77c0
movq %r12, %rdi
leaq 0x777f(%rip), %rsi      # 0x26ff3
callq 0x132f3
testb %al, %al
jne 0x1f890
movq %r12, %rdi
leaq 0x776f(%rip), %rsi      # 0x26ff6
callq 0x132f3
testb %al, %al
je 0x1f8af
xorl %r13d, %r13d
callq 0x1f6b5
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x5778
incl %ebx
testb %r13b, %r13b
jne 0x1f84d
jmp 0x1fcd9
movq %r12, %rdi
leaq 0x80ca(%rip), %rsi      # 0x27983
callq 0x132f3
movb $0x1, %r13b
testb %al, %al
je 0x1f8cf
movl $0x0, 0x1c(%rsp)
jmp 0x1f89b
movq %r12, %rdi
leaq 0x80ad(%rip), %rsi      # 0x27986
callq 0x132f3
testb %al, %al
je 0x1f8ec
movl $0x0, 0xc(%rsp)
jmp 0x1f89b
movq %r12, %rdi
leaq 0x777d(%rip), %rsi      # 0x27073
callq 0x132f3
testb %al, %al
je 0x1f947
incl %ebx
cmpl 0x4(%rsp), %ebx
jge 0x1f99a
movslq %ebx, %rax
movq %rbp, 0x40(%rsp)
movq (%rbp,%rax,8), %rsi
movq %r14, %rbp
movq %r14, %rdi
leaq 0x13(%rsp), %rdx
callq 0x77c0
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7529
movq %rbp, %rdi
callq 0x5778
movq 0x40(%rsp), %rbp
jmp 0x1f89b
movq %r12, %rdi
leaq 0x7bf4(%rip), %rsi      # 0x27545
callq 0x132f3
testb %al, %al
je 0x1f9ca
incl %ebx
cmpl 0x4(%rsp), %ebx
jge 0x1fa00
movslq %ebx, %rax
movq (%rbp,%rax,8), %rdi
callq 0x1dd0d
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
ja 0x1faed
ucomiss 0x567b(%rip), %xmm1     # 0x25004
ja 0x1faed
movss %xmm1, 0x8(%rsp)
jmp 0x1f89b
movq 0x1664f(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x76c6(%rip), %rsi      # 0x27076
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq %r12, %rdi
leaq 0x7929(%rip), %rsi      # 0x272fd
callq 0x132f3
testb %al, %al
je 0x1fa30
incl %ebx
cmpl 0x4(%rsp), %ebx
jge 0x1fa9c
movslq %ebx, %rax
movq (%rbp,%rax,8), %rsi
leaq 0x48(%rsp), %rdi
callq 0x5730
jmp 0x1f89b
movq 0x165e9(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7f73(%rip), %rsi      # 0x27989
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq %r12, %rdi
leaq 0x7941(%rip), %rsi      # 0x2737b
callq 0x132f3
testb %al, %al
je 0x1facc
incl %ebx
cmpl 0x4(%rsp), %ebx
jge 0x1fb20
movslq %ebx, %rax
movq (%rbp,%rax,8), %rdi
callq 0x1dca8
movl %eax, 0x18(%rsp)
testl %eax, %eax
jns 0x1f89b
movq 0x1657d(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7f87(%rip), %rsi      # 0x27a09
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq 0x1654d(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x784e(%rip), %rsi      # 0x27300
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq %r12, %rdi
leaq 0x7f5f(%rip), %rsi      # 0x27a35
callq 0x132f3
movb $0x1, %r13b
testb %al, %al
je 0x1fb4d
movb $0x1, %al
movl %eax, 0x14(%rsp)
jmp 0x1f89b
movss %xmm1, 0x8(%rsp)
movq 0x164f6(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7eae(%rip), %rsi      # 0x279b7
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq 0x164c9(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7eaa(%rip), %rsi      # 0x279e0
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x1fb8d
movq 0x68(%rsp), %rax
cmpb $0x2d, (%rax)
jne 0x1fb99
movq 0x16492(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x752b(%rip), %rsi      # 0x27098
callq 0x53f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
xorl %r13d, %r13d
pushq $0x1
popq %r15
jmp 0x1f89b
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x77fc
jmp 0x1f89b
movq %rax, %r15
cmpl $0x1, %edx
jne 0x1fef1
movq %r15, %rdi
callq 0x5170
movq 0x1642a(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7e0b(%rip), %rsi      # 0x279e0
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
callq 0x5640
jmp 0x1fb8d
movq %rax, %r15
cmpl $0x1, %edx
jne 0x1fef1
movq %r15, %rdi
callq 0x5170
movq 0x163e4(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x7d6e(%rip), %rsi      # 0x27989
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
callq 0x5640
jmp 0x1fb8d
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x1fc6b
movl 0xc(%rsp), %eax
movl 0x1c(%rsp), %r12d
orb %r12b, %al
testb $0x1, %al
jne 0x1fc95
movq 0x16393(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x7dcf(%rip), %rsi      # 0x27a38
jmp 0x1fcbd
movq 0x1637e(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x742d(%rip), %rsi      # 0x270ab
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
callq 0x1f6b5
jmp 0x1fcd5
leaq 0x7df4(%rip), %rsi      # 0x27a90
leaq 0x48(%rsp), %rdi
callq 0x132f3
testb %al, %al
je 0x1fd02
movq 0x1633f(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x7dd9(%rip), %rsi      # 0x27a96
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
pushq $0x1
popq %r15
leaq 0x20(%rsp), %rdi
callq 0x911e
leaq 0x48(%rsp), %rdi
callq 0x5778
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1df8a
movl 0x18(%rsp), %ebp
movq 0x162a6(%rip), %rdi     # 0x35fb8
leaq 0x7da0(%rip), %rsi      # 0x27ab9
callq 0x53f0
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %rax
movq %rax, %rsi
subq %rbx, %rsi
sarq $0x5, %rsi
movslq 0x167c7(%rip), %rcx     # 0x36500
cmpq %rcx, %rsi
jbe 0x1fe79
movq 0x1626f(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x6d70(%rip), %rsi      # 0x26ac5
movq %rax, %rdi
callq 0x53f0
movq 0x16254(%rip), %rdi     # 0x35fb8
callq 0x51c0
movq 0x16248(%rip), %rdi     # 0x35fb8
leaq 0x7d66(%rip), %rsi      # 0x27add
callq 0x53f0
movq %rax, %rdi
movl %ebp, %esi
callq 0x56a0
leaq 0x7d87(%rip), %rsi      # 0x27b14
movq %rax, %rdi
callq 0x53f0
movss 0x58cf(%rip), %xmm0     # 0x2566c
mulss 0x8(%rsp), %xmm0
movq %rax, %rdi
callq 0x5300
leaq 0x7d8b(%rip), %rsi      # 0x27b3d
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
testb $0x1, 0xc(%rsp)
je 0x1fe00
movq 0x161e8(%rip), %rdi     # 0x35fb8
leaq 0x7d9b(%rip), %rsi      # 0x27b72
callq 0x53f0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x53a0
leaq 0x5c25(%rip), %rsi      # 0x25a15
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
testb $0x1, %r12b
je 0x1fe2e
movq 0x161ab(%rip), %rdi     # 0x35fb8
leaq 0x7d7c(%rip), %rsi      # 0x27b90
callq 0x53f0
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq 0x16183(%rip), %rdi     # 0x35fb8
callq 0x51c0
movzbl 0xc(%rsp), %esi
andl $0x1, %esi
movzbl %r12b, %edx
andl $0x1, %edx
movzbl 0x14(%rsp), %r9d
andl $0x1, %r9d
leaq 0x48(%rsp), %rdi
leaq 0x20(%rsp), %rcx
movl %ebp, %r8d
movss 0x8(%rsp), %xmm0
callq 0xdf04
xorl %r15d, %r15d
testl %eax, %eax
setne %r15b
jmp 0x1fcd9
movq 0x16138(%rip), %r14     # 0x35fb8
leaq 0x64d6(%rip), %r15      # 0x2635d
cmpq %rax, %rbx
je 0x1fd5d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x53f0
addq $0x20, %rbx
movq 0x28(%rsp), %rax
jmp 0x1fe87
jmp 0x1feee
movq %rax, %r15
callq 0x5640
jmp 0x1fef1
jmp 0x1feee
movq %rax, %r15
callq 0x5640
jmp 0x1fef1
movq %rax, %rdi
callq 0x7102
movq %rax, %r15
leaq 0x88(%rsp), %rdi
callq 0x5778
jmp 0x1fef1
jmp 0x1feee
movq %rax, %r15
jmp 0x1ff14
jmp 0x1fefd
jmp 0x1fefd
movq %rax, %r15
leaq 0x68(%rsp), %rdi
callq 0x5778
jmp 0x1ff00
movq %rax, %r15
leaq 0x20(%rsp), %rdi
callq 0x911e
leaq 0x48(%rsp), %rdi
callq 0x5778
movq %r15, %rdi
callq 0x56c0