// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_weed.cpp
// Function: addKmersByFilters(std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, int, int, int&, int)
int addKmersByFilters (std::set < std::string > & myKmerSet, const std::vector < std::string > & myFiles, const int minSamples, const int maxSamples, int & myKmerSize, const int numSamples) {
	std::unordered_map < std::string, int > kmerCountMap;
	std::ifstream myFileStream;

	if (minSamples>0){
		std::cout << "Weeding kmers that are present in fewer than " << minSamples << " samples" << std::endl;
	}
	if (maxSamples<numSamples){
		std::cout << "Weeding kmers that are present in more than " << maxSamples << " samples" << std::endl;
	}

	for (std::vector < std::string >::const_iterator it = myFiles.begin(); it != myFiles.end(); ++it){

		if (openFileStream(*it, myFileStream)){return 1;};

		int kmersize;
		std::vector < std::string > names;
		
		readKmerHeader(myFileStream, kmersize, names);
		
		if (myKmerSize==0){
			myKmerSize=kmersize;
		}
		if (kmersize!=myKmerSize){
			std::cerr << "Files have different kmer sizes" << std::endl << std::endl;
			return 1;
		}

		bool firstkmer=true;
		char base;
		char kmerbuffer[kmersize*2/3];
		char asciibuffer[int(ceil(float(names.size())/6))];
		while (myFileStream.read(asciibuffer, sizeof(asciibuffer))){
			std::string asciibits (asciibuffer, sizeof(asciibuffer));
			std::vector < bool > mybits;
			vectorbool_from_ascii(asciibits, mybits);//read the ascii representation of the taxa to a vector of bools
			int matchcount=std::count(mybits.begin(), mybits.end(), true);
			
			while (myFileStream.peek()!='\n' && myFileStream.get(base)){
				myFileStream.read(kmerbuffer, sizeof(kmerbuffer));
				std::string kmer (kmerbuffer, kmersize*2/3);
				
				std::unordered_map < std::string, int >::iterator kcmit = kmerCountMap.find(kmer);//check if the kmer is in the hash
				if ( kcmit != kmerCountMap.end() ){//if the kmer is not in the hash
					kcmit->second+=matchcount;
				}
				else {
					kmerCountMap.insert(std::make_pair(kmer, matchcount));
				}
			}
			myFileStream.ignore(256,'\n');//skip the end ofline character
    	}
		myFileStream.close();
	}

	std::unordered_map < std::string, int >::iterator kcmit = kmerCountMap.begin();
	std::unordered_map < std::string, int >::iterator kcmend = kmerCountMap.end();

	for (; kcmit!=kcmend; ){
		if (kcmit->second<minSamples || kcmit->second>maxSamples){
			//std::cout << kcmit->first << " " << kcmit->second << std::endl;
			myKmerSet.insert(kcmit->first);
		}
		kmerCountMap.erase(kcmit++);
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movl %r9d, %ebx
movq %r8, %r15
movl %ecx, -0x2c(%rbp)
movl %edx, %r14d
movq %rsi, -0x40(%rbp)
movq %rdi, -0x90(%rbp)
leaq -0x130(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
leaq -0x368(%rbp), %rdi
callq 0x5450
testl %r14d, %r14d
jle 0x1c8a2
movq 0x19744(%rip), %rdi     # 0x35fb8
leaq 0x9e75(%rip), %rsi      # 0x266f0
callq 0x53f0
movq %rax, %rdi
movl %r14d, %esi
callq 0x56a0
leaq 0x9e8c(%rip), %rsi      # 0x2671e
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
cmpl %ebx, -0x2c(%rbp)
jge 0x1c8dc
movq 0x1970a(%rip), %rdi     # 0x35fb8
leaq 0x9e72(%rip), %rsi      # 0x26727
callq 0x53f0
movq %rax, %rdi
movl -0x2c(%rbp), %esi
callq 0x56a0
leaq 0x9e52(%rip), %rsi      # 0x2671e
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movl %r14d, -0x38(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
leaq -0x368(%rbp), %r14
pushq $0x1
popq %rbx
leaq -0x128(%rbp), %r12
movq %r15, -0x98(%rbp)
movq -0x40(%rbp), %rax
cmpq 0x8(%rax), %rdi
je 0x1cbca
movq %rdi, -0xa8(%rbp)
movq %r14, %rsi
movl %ebx, %edx
callq 0x73fe
testl %eax, %eax
jne 0x1cc13
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
movq %r14, %rdi
leaq -0x34(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x832b
movl (%r15), %ecx
movl -0x34(%rbp), %eax
testl %ecx, %ecx
je 0x1c97a
cmpl %ecx, %eax
je 0x1c97d
movq 0x19699(%rip), %rdi     # 0x35ff0
leaq 0x9df6(%rip), %rsi      # 0x26754
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
xorl %ebx, %ebx
jmp 0x1cba9
movl %eax, (%r15)
movq %rsp, -0xa0(%rbp)
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movq %rax, -0xc0(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
sarq $0x5, %rax
js 0x1c9b9
cvtsi2ss %rax, %xmm0
jmp 0x1c9ce
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0x8c9a(%rip), %xmm0     # 0x25670
callq 0x5220
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0xb8(%rbp)
movq %rdx, -0x48(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0xb0(%rbp)
movq %r14, %rdi
movq -0x48(%rbp), %rsi
movq -0xb8(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1cb91
leaq -0xd0(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0xe0(%rbp), %r13
movq %r13, %rdi
movq -0x48(%rbp), %rsi
movq -0xb0(%rbp), %rdx
callq 0x7828
andq $0x0, -0x88(%rbp)
andl $0x0, -0x80(%rbp)
andq $0x0, -0x78(%rbp)
andl $0x0, -0x70(%rbp)
andq $0x0, -0x68(%rbp)
movq %r13, %rdi
leaq -0x88(%rbp), %rsi
callq 0x7bb6
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rdx
movl -0x70(%rbp), %ecx
movb $0x1, -0x108(%rbp)
xorl %esi, %esi
leaq -0x108(%rbp), %r8
callq 0xc10d
movq %rax, %r13
movq %r14, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x1cb64
movq %r14, %rdi
leaq -0x2d(%rbp), %rsi
callq 0x5550
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x1cb64
movq %r14, %rdi
movq %r15, %rsi
movq -0xc0(%rbp), %rdx
callq 0x54c0
movl -0x34(%rbp), %eax
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq -0x118(%rbp), %rax
movq %rax, -0x128(%rbp)
addq %r15, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x7828
leaq -0x160(%rbp), %rdi
movq %r12, %rsi
callq 0x1d7b8
testq %rax, %rax
je 0x1cb27
addl %r13d, 0x28(%rax)
jmp 0x1cb57
leaq -0x108(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5540
movl %r13d, -0xe8(%rbp)
leaq -0x160(%rbp), %rdi
movq %rbx, %rsi
callq 0x1d888
movq %rbx, %rdi
callq 0x5778
movq %r12, %rdi
callq 0x5778
jmp 0x1caa2
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
pushq $0xa
popq %rdx
callq 0x5260
leaq -0x88(%rbp), %rdi
callq 0x908a
leaq -0xe0(%rbp), %rdi
callq 0x5778
jmp 0x1ca06
movq %r14, %rdi
callq 0x50a0
movq -0xa0(%rbp), %rsp
movb $0x1, %bl
movq -0x98(%rbp), %r15
leaq -0x60(%rbp), %rdi
callq 0x911e
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
testb %bl, %bl
pushq $0x1
popq %rbx
jne 0x1c8ff
jmp 0x1cc13
leaq -0x160(%rbp), %rbx
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x1cc11
movl 0x28(%r14), %eax
cmpl -0x38(%rbp), %eax
setge %cl
cmpl -0x2c(%rbp), %eax
setle %al
testb %al, %cl
jne 0x1cbfe
leaq 0x8(%r14), %rsi
movq -0x90(%rbp), %rdi
callq 0x9a1e
movq (%r14), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1dba4
movq %r15, %r14
jmp 0x1cbd5
xorl %ebx, %ebx
leaq -0x368(%rbp), %rdi
callq 0x50e0
leaq -0x160(%rbp), %rdi
callq 0x1d6fa
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1ccb1
jmp 0x1cc47
jmp 0x1cc56
jmp 0x1cc47
movq %rax, %rbx
jmp 0x1cca5
jmp 0x1cc56
jmp 0x1cc81
jmp 0x1cc81
jmp 0x1cc81
jmp 0x1cc56
movq %rax, %rbx
jmp 0x1cc9c
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
callq 0x5778
jmp 0x1cc71
jmp 0x1cc6e
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
callq 0x5778
jmp 0x1cc84
jmp 0x1cc81
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x908a
leaq -0xe0(%rbp), %rdi
callq 0x5778
leaq -0x60(%rbp), %rdi
callq 0x911e
leaq -0x368(%rbp), %rdi
callq 0x50e0
leaq -0x160(%rbp), %rdi
callq 0x1d6fa
movq %rbx, %rdi
callq 0x56c0