// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]optimal-seed-solver/thresholdSolver.cc
// Function: ThresholdSolver::solve(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int ThresholdSolver::solve(string read)
{
	int index = this->index;
	if(verbose)
	{
		cout << "Reading read #" << index << ": " << read << endl;
		cout.flush();
		cout << "Original length: " << read.length() << endl;
	}

	int minLength = tree.getHashLength();
	int maxLength = tree.getFullLength();

	int pos = 0;
	int count = 0;

	bool pass = true;

	int ll = 0;
	int totalfreq = 0;

	this->read = read;

	for (int i = 0; i < thresholdVec.size(); i++) {

		int currLength = maxLength;

		if(read.length() - pos < maxLength)
		{
			currLength = read.length() - pos;
			if(currLength <= minLength)
			{
				// Read too small
				if(verbose)
					cout << "Read too small" << endl;
				pass = false;
				break;
			}
		}

		string seed = read.substr(pos, currLength);
		if(verbose)
			cout << "Position " << pos << "." << endl;

		tree.query(seed, thresholdVec[i]);
		int len = tree.effectiveLength();
		int freq = tree.frequency();

		if(verbose)
			cout << "Effective length: " << len << " Frequency: " << freq << endl;

		if(freq <= thresholdVec[i])
		{
			//Case 1
			//this works
			if(verbose)
				cout << "Frequency is <= threshold." << endl;
			pos += len;
			//freqTable[index][count] = freq;
			//seedLenTable[index][count] = len;
		}
		else if(tree.isLeaf())
		{
			//Case 2
			//hits leaf before threshold - ok
			if(verbose)
				cout << "Hit leaf before threshold." << endl;
			pos += len;
			//freqTable[index][count] = freq;
			//seedLenTable[index][count] = len;
		}
		else
		{
			//Case 3
			//does not meet threhold, not leaf - fail
			if(verbose)
				cout << "Leaf does not meet threshold = fail." << endl;
			pass = false;
			break;
		}

		count++;
		if (pos == read.length())
		{
			if(verbose)
				cout << "Position at read length. Success." << endl;
			break;
		}
		if (pos > read.length() - minLength)
		{
			if(verbose)
				cout << "Next read size too small." << endl;
			pass = false;
			break;
		}
		totalfreq += freq;
	}

	if(!pass)
	{
		//Did not pass - calculate letters left
		/*
		ll = read.length() - pos;
		if(verbose)
			cout << ll << " letters left." << endl;
		lettersLeft[index] = ll;
		*/
		return -1;
	}
	if(verbose)
		cout << "Finished read at position " << pos << " from " << read.length() << " with totalFrequency " << totalfreq << ".\n" << endl;

	return totalfreq;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r12
movq %rdi, %r14
cmpb $0x1, 0xcc(%rdi)
movq %rsi, 0x10(%rsp)
jne 0x6294
movl 0x428(%r14), %ebp
movq 0xadfc(%rip), %rbx      # 0x10fc8
leaq 0x5ff7(%rip), %rsi      # 0xc1ca
movl $0xe, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %ebp, %esi
movq 0x10(%rsp), %r12
callq 0x3450
movq %rax, %r15
leaq 0x63f3(%rip), %rsi      # 0xc5ec
movl $0x2, %edx
movq %rax, %rdi
callq 0x32e0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r15, %rdi
callq 0x32e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movq %rbx, %rdi
callq 0x31d0
leaq 0x5f89(%rip), %rsi      # 0xc1d9
movl $0x11, %edx
movq %rbx, %rdi
callq 0x32e0
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x31c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
leaq 0xd0(%r14), %r13
movq %r13, %rdi
callq 0x84aa
movl %eax, %ebx
movq %r13, %rdi
callq 0x84ae
movl %eax, %ebp
leaq 0x430(%r14), %rdi
movq %r12, %rsi
callq 0x3150
movq 0x458(%r14), %rax
cmpq 0x450(%r14), %rax
je 0x6728
movslq %ebp, %rax
movq %rax, 0x70(%rsp)
movl %ebx, 0x24(%rsp)
movslq %ebx, %rax
movq %rax, 0x68(%rsp)
movb $0x1, %al
movl %eax, 0x8(%rsp)
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movl $0x0, 0x1c(%rsp)
movl %ebp, 0x20(%rsp)
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movslq 0xc(%rsp), %rdx
movq %rax, %rsi
subq %rdx, %rsi
movl %ebp, %ecx
cmpq 0x70(%rsp), %rsi
jae 0x6330
subl 0xc(%rsp), %eax
movl %eax, %ecx
cmpl 0x24(%rsp), %eax
jle 0x660c
movslq %ecx, %rcx
leaq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x32a0
cmpb $0x1, 0xcc(%r14)
jne 0x63b2
movl $0x9, %edx
movq 0xac70(%rip), %rdi      # 0x10fc8
leaq 0x5e9b(%rip), %rsi      # 0xc1fa
callq 0x32e0
movq 0xac5d(%rip), %rdi      # 0x10fc8
movl 0xc(%rsp), %esi
callq 0x3450
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6229(%rip), %rsi      # 0xc5af
callq 0x32e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %rdi
callq 0x4494
movq 0x450(%r14), %rax
movl (%rax,%r15,4), %edx
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x84b2
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6407
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %r13, %rdi
callq 0x8648
movl %eax, %ebx
movq %r13, %rbp
movq %r13, %rdi
callq 0x8650
movl %eax, %r13d
cmpb $0x1, 0xcc(%r14)
jne 0x649b
movl $0x12, %edx
movq 0xab93(%rip), %rdi      # 0x10fc8
leaq 0x5dc8(%rip), %rsi      # 0xc204
callq 0x32e0
movq 0xab80(%rip), %rdi      # 0x10fc8
movl %ebx, %esi
callq 0x3450
movq %rax, %r12
movl $0xc, %edx
movq %rax, %rdi
leaq 0x5db6(%rip), %rsi      # 0xc217
callq 0x32e0
movq %r12, %rdi
movl %r13d, %esi
callq 0x3450
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r12, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movq 0x450(%r14), %rax
cmpl (%rax,%r15,4), %r13d
jle 0x64cc
movq %rbp, %rdi
callq 0x8664
movb 0xcc(%r14), %cl
testb %al, %al
je 0x666b
leaq 0x5d79(%rip), %rsi      # 0xc23f
testb %cl, %cl
jne 0x64dd
jmp 0x6520
cmpb $0x0, 0xcc(%r14)
leaq 0x5d49(%rip), %rsi      # 0xc224
je 0x6520
movl $0x1a, %edx
movq 0xaadf(%rip), %rdi      # 0x10fc8
callq 0x32e0
movq 0xaad3(%rip), %rcx      # 0x10fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0xaab5(%rip), %rdi      # 0x10fc8
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movl 0xc(%rsp), %eax
addl %ebx, %eax
movl %eax, 0xc(%rsp)
cltq
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
jne 0x6597
cmpb $0x1, 0xcc(%r14)
jne 0x66cd
movl $0x21, %edx
movq 0xaa74(%rip), %rdi      # 0x10fc8
leaq 0x5d24(%rip), %rsi      # 0xc27f
callq 0x32e0
movq 0xaa61(%rip), %rcx      # 0x10fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0xaa43(%rip), %rdi      # 0x10fc8
callq 0x3040
movq %rax, %rdi
callq 0x31d0
jmp 0x66cd
subq 0x68(%rsp), %rcx
cmpq %rax, %rcx
jae 0x66d1
cmpb $0x1, 0xcc(%r14)
jne 0x66c5
movl $0x19, %edx
movq 0xaa09(%rip), %rdi      # 0x10fc8
leaq 0x5cdb(%rip), %rsi      # 0xc2a1
callq 0x32e0
movq 0xa9f6(%rip), %rcx      # 0x10fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0xa9d8(%rip), %rdi      # 0x10fc8
callq 0x3040
movl $0x0, 0x8(%rsp)
movq %rax, %rdi
callq 0x31d0
xorl %ebx, %ebx
jmp 0x66d8
cmpb $0x1, 0xcc(%r14)
jne 0x665c
movl $0xe, %edx
movq 0xa9a6(%rip), %rbx      # 0x10fc8
movq %rbx, %rdi
leaq 0x5bbf(%rip), %rsi      # 0xc1eb
callq 0x32e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movl 0x20(%rsp), %ebp
movl $0x0, 0x8(%rsp)
xorl %ebx, %ebx
jmp 0x66fb
testb %cl, %cl
je 0x66c5
movl $0x24, %edx
movq 0xa94d(%rip), %rdi      # 0x10fc8
leaq 0x5bd8(%rip), %rsi      # 0xc25a
callq 0x32e0
movq 0xa93a(%rip), %rcx      # 0x10fc8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0xa91c(%rip), %rdi      # 0x10fc8
callq 0x3040
movl $0x0, 0x8(%rsp)
movq %rax, %rdi
callq 0x31d0
xorl %ebx, %ebx
jmp 0x66d8
movl $0x0, 0x8(%rsp)
xorl %ebx, %ebx
jmp 0x66d8
addl %r13d, 0x1c(%rsp)
movb $0x1, %bl
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x66f4
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbp, %r13
movl 0x20(%rsp), %ebp
testb %bl, %bl
je 0x671d
incq %r15
movq 0x458(%r14), %rax
subq 0x450(%r14), %rax
sarq $0x2, %rax
cmpq %r15, %rax
ja 0x6303
movl 0x1c(%rsp), %r15d
movl 0x8(%rsp), %ecx
jmp 0x6735
xorl %r15d, %r15d
movb $0x1, %cl
movl $0x0, 0xc(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x1, %cl
je 0x6803
cmpb $0x1, 0xcc(%r14)
jne 0x6800
movq 0xa870(%rip), %rbx      # 0x10fc8
leaq 0x5b5c(%rip), %rsi      # 0xc2bb
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl 0xc(%rsp), %esi
callq 0x3450
movq %rax, %rbx
leaq 0x5b54(%rip), %rsi      # 0xc2d6
movl $0x6, %edx
movq %rax, %rdi
callq 0x32e0
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x31c0
movq %rax, %rbx
leaq 0x5b33(%rip), %rsi      # 0xc2dd
movl $0x15, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3450
movq %rax, %rbx
leaq 0x58c1(%rip), %rsi      # 0xc08d
movl $0x2, %edx
movq %rax, %rdi
callq 0x32e0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x683a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x683a
jmp 0x6837
jmp 0x6837
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x6856
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x3470