// File: /workspace/github_cpp_makefile_star2_O3/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, -0xd8(%rbp)
movl %ecx, %r12d
movl %edx, %r14d
movq %rsi, -0x40(%rbp)
movq %rdi, -0xe0(%rbp)
leaq -0x138(%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 -0x370(%rbp), %rdi
callq 0x5410
movl %r14d, -0x30(%rbp)
testl %r14d, %r14d
jle 0x24176
movq 0x1cea8(%rip), %rdi     # 0x40fc0
leaq 0x105fc(%rip), %rsi     # 0x3471b
movl $0x2d, %edx
callq 0x5420
movq 0x1ce90(%rip), %rdi     # 0x40fc0
movl -0x30(%rbp), %esi
callq 0x5690
movq %rax, %r14
leaq 0x10607(%rip), %rsi     # 0x34749
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 0x241e0
movq 0x1ce3e(%rip), %rdi     # 0x40fc0
leaq 0x105c9(%rip), %rsi     # 0x34752
movl $0x2c, %edx
callq 0x5420
movq 0x1ce26(%rip), %rdi     # 0x40fc0
movl %r12d, %esi
callq 0x5690
movq %rax, %rbx
leaq 0x1059d(%rip), %rsi     # 0x34749
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 -0x40(%rbp), %rax
movq (%rax), %rdi
cmpq 0x8(%rax), %rdi
je 0x24591
leaq -0x370(%rbp), %r13
leaq -0x90(%rbp), %r15
movl %r12d, -0x38(%rbp)
movq %rdi, -0xf0(%rbp)
movq %r13, %rsi
movl $0x1, %edx
callq 0x7f3f
movl $0x1, %r14d
testl %eax, %eax
jne 0x245db
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
movq $0x0, -0xa0(%rbp)
movq %r13, %rdi
leaq -0x34(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x9684
movq -0xd8(%rbp), %rdx
movl (%rdx), %ecx
movl -0x34(%rbp), %eax
testl %ecx, %ecx
je 0x24266
cmpl %ecx, %eax
je 0x24268
jmp 0x24605
movl %eax, (%rdx)
movq %rsp, -0xe8(%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 0x242b8
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x242d0
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xf3a8(%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, -0x100(%rbp)
movq %rdx, -0x48(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0xf8(%rbp)
movq %r14, -0x108(%rbp)
movq %r13, %rdi
movq -0x48(%rbp), %rsi
movq -0x100(%rbp), %rdx
callq 0x5480
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24559
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0x85fc
xorl %eax, %eax
movq %rax, -0x70(%rbp)
movl %eax, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movl %eax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
movq %rbx, %rdi
leaq -0x70(%rbp), %rsi
callq 0x8993
movq -0x70(%rbp), %rax
movq -0x60(%rbp), %rcx
movl -0x58(%rbp), %edx
cmpq %rcx, %rax
setne %sil
testl %edx, %edx
setne %dil
orb %sil, %dil
xorl %ebx, %ebx
cmpb $0x1, %dil
jne 0x243d7
xorl %ebx, %ebx
xorl %esi, %esi
xorl %r9d, %r9d
movq (%rax), %rdi
btq %rsi, %rdi
adcq $0x0, %rbx
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 0x243a6
movq %r13, %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x24507
movq %r13, %rdi
leaq -0x29(%rbp), %rsi
callq 0x5500
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24507
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x5480
movl -0x34(%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 -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
addq %r14, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x85fc
leaq -0x168(%rbp), %rdi
movq %r15, %rsi
callq 0x25840
testq %rax, %rax
je 0x24464
addl %ebx, 0x28(%rax)
jmp 0x244e2
movq %r13, %r14
movq %r12, %r13
movq %r15, %r12
leaq -0x120(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
addq %rsi, %rdx
leaq -0x130(%rbp), %r15
movq %r15, %rdi
callq 0x8566
movl %ebx, -0x110(%rbp)
leaq -0x168(%rbp), %rdi
movq %r15, %rsi
callq 0x2593c
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x244d2
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x53c0
movq %r12, %r15
movq %r13, %r12
movq %r14, %r13
movq -0x108(%rbp), %r14
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x243d7
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x243d7
movl $0x100, %esi            # imm = 0x100
movq %r13, %rdi
movl $0xa, %edx
callq 0x5220
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2452e
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x2430f
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x2430f
movq %r13, %rdi
callq 0x50b0
movq -0xe8(%rbp), %rsp
leaq -0xb0(%rbp), %rdi
callq 0xab8e
movq -0xf0(%rbp), %rdi
addq $0x20, %rdi
movq -0x40(%rbp), %rax
cmpq 0x8(%rax), %rdi
movl -0x38(%rbp), %r12d
jne 0x24203
leaq -0x168(%rbp), %rbx
movq 0x10(%rbx), %r14
testq %r14, %r14
je 0x245d8
movl 0x28(%r14), %eax
cmpl -0x30(%rbp), %eax
setge %cl
cmpl %r12d, %eax
setle %al
testb %al, %cl
jne 0x245c5
leaq 0x8(%r14), %rsi
movq -0xe0(%rbp), %rdi
callq 0xb4a0
movq (%r14), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x25c46
movq %r15, %r14
jmp 0x2459c
xorl %r14d, %r14d
leaq -0x370(%rbp), %rdi
callq 0x50d0
leaq -0x168(%rbp), %rdi
callq 0x257a2
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c9e4(%rip), %rbx     # 0x40ff0
leaq 0x1016c(%rip), %rsi     # 0x3477f
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x5420
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq 0x1c9b2(%rip), %rdi     # 0x40ff0
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rax, %rbx
movq (%rax), %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
leaq -0xb0(%rbp), %rdi
callq 0xab8e
jmp 0x245db
movq %rax, %r14
jmp 0x2475b
jmp 0x246a6
jmp 0x24698
jmp 0x246a6
jmp 0x246a6
jmp 0x24698
movq %rax, %r14
jmp 0x2474f
jmp 0x246fa
jmp 0x246a6
jmp 0x246fa
movq %rax, %r14
jmp 0x24743
movq %rax, %r14
movq -0x130(%rbp), %rdi
leaq -0x120(%rbp), %rax
cmpq %rax, %rdi
je 0x246da
movq -0x120(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x246da
jmp 0x246d7
movq %rax, %r14
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0x246fd
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x53c0
jmp 0x246fd
jmp 0x246fa
movq %rax, %r14
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x24721
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x53c0
movq $0x0, -0x70(%rbp)
movl $0x0, -0x68(%rbp)
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x24743
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x53c0
leaq -0xb0(%rbp), %rdi
callq 0xab8e
leaq -0x370(%rbp), %rdi
callq 0x50d0
leaq -0x168(%rbp), %rdi
callq 0x257a2
movq %r14, %rdi
callq 0x56a0