// File: /workspace/github_cpp_makefile_star2_O1/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 $0x358, %rsp            # imm = 0x358
movl %r9d, %ebx
movq %r8, -0xe0(%rbp)
movl %ecx, %r12d
movl %edx, %r14d
movq %rsi, -0x70(%rbp)
movq %rdi, -0xe8(%rbp)
leaq -0x140(%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 -0x378(%rbp), %rdi
callq 0x5410
movl %r14d, -0x38(%rbp)
testl %r14d, %r14d
jle 0x244fe
movq 0x1cb20(%rip), %rdi     # 0x40fc0
leaq 0x10284(%rip), %rsi     # 0x3472b
movl $0x2d, %edx
callq 0x5420
movq 0x1cb08(%rip), %rdi     # 0x40fc0
movl -0x38(%rbp), %esi
callq 0x5690
movq %rax, %r14
leaq 0x1028f(%rip), %rsi     # 0x34759
movl $0x8, %edx
movq %rax, %rdi
callq 0x5420
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
cmpl %ebx, %r12d
jge 0x24568
movq 0x1cab6(%rip), %rdi     # 0x40fc0
leaq 0x10251(%rip), %rsi     # 0x34762
movl $0x2c, %edx
callq 0x5420
movq 0x1ca9e(%rip), %rdi     # 0x40fc0
movl %r12d, %esi
callq 0x5690
movq %rax, %rbx
leaq 0x10225(%rip), %rsi     # 0x34759
movl $0x8, %edx
movq %rax, %rdi
callq 0x5420
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq -0x70(%rbp), %rax
movq (%rax), %rdi
cmpq 0x8(%rax), %rdi
je 0x249cf
leaq -0x378(%rbp), %r15
leaq -0x98(%rbp), %rbx
movl %r12d, -0x64(%rbp)
movq %rdi, -0xf8(%rbp)
movq %r15, %rsi
movl $0x1, %edx
callq 0x7f73
movl $0x1, -0x34(%rbp)
testl %eax, %eax
jne 0x24a1d
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq %r15, %rdi
leaq -0x2c(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x96e2
movq -0xe0(%rbp), %rcx
cmpl $0x0, (%rcx)
jne 0x245e7
movl -0x2c(%rbp), %eax
movl %eax, (%rcx)
movl -0x2c(%rbp), %eax
movl (%rcx), %ecx
movl %ecx, -0x68(%rbp)
cmpl %ecx, %eax
movq %rax, -0xf0(%rbp)
jne 0x24651
movq %rsp, -0xd8(%rbp)
addl %eax, %eax
cltq
imulq $0x55555556, %rax, %r12 # imm = 0x55555556
movq %r12, %rax
shrq $0x3f, %rax
shrq $0x20, %r12
addl %eax, %r12d
leaq 0xf(%r12), %rax
andq $-0x10, %rax
movq %rsp, %r14
subq %rax, %r14
movq %r14, %rsp
movq -0xa8(%rbp), %rax
subq -0xb0(%rbp), %rax
sarq $0x5, %rax
js 0x246ca
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x246e2
movl $0x1f, %edx
movq 0x1c993(%rip), %rdi     # 0x40ff0
leaq 0x1012b(%rip), %rsi     # 0x3478f
callq 0x5420
movq 0x1c980(%rip), %rcx     # 0x40ff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq 0x1c962(%rip), %rdi     # 0x40ff0
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
jmp 0x2499f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xef96(%rip), %xmm0     # 0x33680
callq 0x51e0
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0x108(%rbp)
movq %rdx, -0x78(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0x100(%rbp)
movq %r14, -0x110(%rbp)
movq %r15, %rdi
movq -0x78(%rbp), %rsi
movq -0x108(%rbp), %rdx
callq 0x5480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2498c
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
movq -0x78(%rbp), %rsi
movq -0x100(%rbp), %rdx
callq 0x862a
xorl %eax, %eax
movq %rax, -0x60(%rbp)
movl %eax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movl %eax, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq %r13, %rdi
leaq -0x60(%rbp), %rsi
callq 0x89ef
movq -0x60(%rbp), %rax
movq -0x50(%rbp), %rcx
movl -0x48(%rbp), %edx
cmpq %rcx, %rax
setne %sil
testl %edx, %edx
setne %dil
orb %sil, %dil
xorl %r13d, %r13d
cmpb $0x1, %dil
jne 0x247eb
xorl %r13d, %r13d
xorl %esi, %esi
xorl %r9d, %r9d
movq (%rax), %rdi
btq %rsi, %rdi
adcq $0x0, %r13
xorl %edi, %edi
cmpl $0x3f, %esi
leal 0x1(%rsi), %esi
sete %dil
leaq (%rax,%rdi,8), %rax
cmovel %r9d, %esi
cmpq %rcx, %rax
setne %dil
cmpl %edx, %esi
setne %r8b
orb %dil, %r8b
jne 0x247ba
movq %r15, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x24926
movq %r15, %rdi
leaq -0x2d(%rbp), %rsi
callq 0x5500
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24926
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x5480
movl -0x2c(%rbp), %eax
addl %eax, %eax
cltq
imulq $0x55555556, %rax, %rax # imm = 0x55555556
movq %rax, %rcx
shrq $0x3f, %rcx
shrq $0x20, %rax
addl %ecx, %eax
movslq %eax, %rdx
leaq -0x88(%rbp), %rax
movq %rax, -0x98(%rbp)
addq %r14, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x862a
leaq -0x170(%rbp), %rdi
movq %rbx, %rsi
callq 0x25c20
testq %rax, %rax
je 0x2487c
addl %r13d, 0x28(%rax)
jmp 0x248fb
movq %r15, %r14
movq %r12, %r15
movq %rbx, %r12
leaq -0x128(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x98(%rbp), %rsi
movq -0x90(%rbp), %rdx
addq %rsi, %rdx
leaq -0x138(%rbp), %rbx
movq %rbx, %rdi
callq 0x859a
movl %r13d, -0x118(%rbp)
leaq -0x170(%rbp), %rdi
movq %rbx, %rsi
callq 0x25d1c
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x248eb
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x53c0
movq %r12, %rbx
movq %r15, %r12
movq %r14, %r15
movq -0x110(%rbp), %r14
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x247eb
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x247eb
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
movl $0xa, %edx
callq 0x5220
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x24961
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %eax, %eax
movq %rax, -0x60(%rbp)
movl %eax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movl %eax, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x24721
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x24721
movq %r15, %rdi
callq 0x50b0
movq -0xd8(%rbp), %rsp
movl -0x64(%rbp), %r12d
leaq -0xb0(%rbp), %rdi
callq 0xabee
movl -0x68(%rbp), %eax
cmpl %eax, -0xf0(%rbp)
jne 0x24a1d
movq -0xf8(%rbp), %rdi
addq $0x20, %rdi
movq -0x70(%rbp), %rax
cmpq 0x8(%rax), %rdi
jne 0x2458b
leaq -0x170(%rbp), %rbx
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x24a16
movl 0x28(%r14), %eax
cmpl -0x38(%rbp), %eax
setge %cl
cmpl %r12d, %eax
setle %al
testb %al, %cl
jne 0x24a03
leaq 0x8(%r14), %rsi
movq -0xe8(%rbp), %rdi
callq 0xb4ec
movq (%r14), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2602a
movq %r15, %r14
jmp 0x249da
movl $0x0, -0x34(%rbp)
leaq -0x378(%rbp), %rdi
callq 0x50d0
leaq -0x170(%rbp), %rdi
callq 0x25b82
movl -0x34(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x24b25
jmp 0x24a65
jmp 0x24a55
jmp 0x24a55
movq %rax, %rbx
jmp 0x24b19
jmp 0x24a65
jmp 0x24abf
jmp 0x24a65
jmp 0x24abf
movq %rax, %rbx
jmp 0x24b0d
movq %rax, %rbx
movq -0x138(%rbp), %rdi
leaq -0x128(%rbp), %rax
cmpq %rax, %rdi
je 0x24a99
movq -0x128(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x24a99
jmp 0x24a96
movq %rax, %rbx
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0x24ac2
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x24ac2
jmp 0x24abf
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x24aeb
movq -0x40(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
xorl %eax, %eax
movq %rax, -0x60(%rbp)
movl %eax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movl %eax, -0x48(%rbp)
movq %rax, -0x40(%rbp)
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x24b0d
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x53c0
leaq -0xb0(%rbp), %rdi
callq 0xabee
leaq -0x378(%rbp), %rdi
callq 0x50d0
leaq -0x170(%rbp), %rdi
callq 0x25b82
movq %rbx, %rdi
callq 0x56a0