// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
jne 0x628a
movl 0x428(%r14), %ebp
movq 0xae06(%rip), %rbx      # 0x10fc8
leaq 0x6001(%rip), %rsi      # 0xc1ca
movl $0xe, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %ebp, %esi
movq 0x18(%rsp), %r12
callq 0x3450
movq %rax, %r15
leaq 0x63ed(%rip), %rsi      # 0xc5dc
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 0x5f93(%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 0x8430
movl %eax, %ebp
movq %r13, %rdi
callq 0x8434
movl %eax, %ebx
leaq 0x430(%r14), %rdi
movq %r12, %rsi
callq 0x3150
movq 0x458(%r14), %rax
movq %r14, 0x10(%rsp)
cmpq 0x450(%r14), %rax
je 0x66aa
movslq %ebx, %rax
movq %rax, 0x70(%rsp)
movl %ebp, 0x24(%rsp)
movslq %ebp, %rax
movq %rax, 0x68(%rsp)
movb $0x1, %al
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
movq 0xacd8(%rip), %r12      # 0x10fc8
movl $0x0, 0x8(%rsp)
movl $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rsi
movq 0x8(%rsi), %rax
movslq 0x8(%rsp), %rdx
movq %rax, %rdi
subq %rdx, %rdi
movq 0x70(%rsp), %r8
movq %r8, %rcx
cmpq %r8, %rdi
jae 0x6337
subl 0x8(%rsp), %eax
cmpl 0x24(%rsp), %eax
jle 0x6782
movslq %eax, %rcx
movq 0x18(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x32a0
movq 0x10(%rsp), %rax
cmpb $0x1, 0xcc(%rax)
jne 0x63ad
movl $0x9, %edx
movq %r12, %rdi
leaq 0x5e9c(%rip), %rsi      # 0xc1fa
callq 0x32e0
movq %r12, %rdi
movl 0x8(%rsp), %esi
callq 0x3450
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x621e(%rip), %rsi      # 0xc59f
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 0x44a2
movq 0x10(%rsp), %rax
movq 0x450(%rax), %rax
movl (%rax,%rbp,4), %edx
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0x8438
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x6406
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %r12, %r14
movq %r13, %rdi
callq 0x85b8
movl %eax, %ebx
movq %r13, %r15
movq %r13, %rdi
callq 0x85c0
movl %eax, %r13d
movq 0x10(%rsp), %rax
cmpb $0x1, 0xcc(%rax)
jne 0x6499
movl $0x12, %edx
movq %r14, %rdi
leaq 0x5dc6(%rip), %rsi      # 0xc204
callq 0x32e0
movq %r14, %rdi
movl %ebx, %esi
callq 0x3450
movq %rax, %r12
movl $0xc, %edx
movq %rax, %rdi
leaq 0x5db8(%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 0x10(%rsp), %rcx
movq 0x450(%rcx), %rax
cmpl (%rax,%rbp,4), %r13d
jle 0x64d3
movq %r15, %rdi
callq 0x85d4
movq 0x10(%rsp), %rcx
movb 0xcc(%rcx), %cl
testb %al, %al
je 0x65e6
leaq 0x5d72(%rip), %rsi      # 0xc23f
testb %cl, %cl
jne 0x64e3
jmp 0x6517
cmpb $0x0, 0xcc(%rcx)
leaq 0x5d43(%rip), %rsi      # 0xc224
je 0x6517
movl $0x1a, %edx
movq %r14, %rdi
callq 0x32e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movl 0x8(%rsp), %eax
addl %ebx, %eax
movl %eax, 0x8(%rsp)
cltq
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpq %rax, %rcx
jne 0x6586
movq 0x10(%rsp), %rax
cmpb $0x1, 0xcc(%rax)
movq %r14, %r12
jne 0x6648
movl $0x21, %edx
movq %r12, %rdi
leaq 0x5d2a(%rip), %rsi      # 0xc27f
callq 0x32e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
jmp 0x662d
subq 0x68(%rsp), %rcx
cmpq %rax, %rcx
movq %r14, %r12
jae 0x6637
movq 0x10(%rsp), %rax
cmpb $0x1, 0xcc(%rax)
jne 0x6640
movl $0x19, %edx
movq %r12, %rdi
leaq 0x5ce9(%rip), %rsi      # 0xc2a1
callq 0x32e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
jmp 0x6625
testb %cl, %cl
je 0x6625
movl $0x24, %edx
movq %r14, %rdi
leaq 0x5c61(%rip), %rsi      # 0xc25a
callq 0x32e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31d0
movl $0x0, 0xc(%rsp)
xorl %ebx, %ebx
movq %r15, %r13
movq %r14, %r12
jmp 0x664d
addl %r13d, 0x20(%rsp)
movb $0x1, %bl
jmp 0x664a
movl $0x0, 0xc(%rsp)
xorl %ebx, %ebx
movq %r15, %r13
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x6669
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
testb %bl, %bl
je 0x6690
incq %rbp
movq 0x10(%rsp), %rcx
movq 0x458(%rcx), %rax
subq 0x450(%rcx), %rax
sarq $0x2, %rax
cmpq %rbp, %rax
ja 0x6300
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb $0x1, 0xc(%rsp)
movq 0x18(%rsp), %r12
movl 0x20(%rsp), %ebp
jne 0x66b4
jmp 0x6773
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
cmpb $0x1, 0xcc(%rax)
jne 0x6771
movq 0xa8fb(%rip), %rbx      # 0x10fc8
leaq 0x5be7(%rip), %rsi      # 0xc2bb
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x32e0
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x3450
movq %rax, %rbx
leaq 0x5bdf(%rip), %rsi      # 0xc2d6
movl $0x6, %edx
movq %rax, %rdi
callq 0x32e0
movq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x31c0
movq %rax, %rbx
leaq 0x5bc2(%rip), %rsi      # 0xc2dd
movl $0x15, %edx
movq %rax, %rdi
callq 0x32e0
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3450
movq %rax, %r12
leaq 0x5951(%rip), %rsi      # 0xc08d
movl $0x2, %edx
movq %rax, %rdi
callq 0x32e0
movq (%r12), %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
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rcx
cmpb $0x1, 0xcc(%rcx)
jne 0x6773
leaq 0x5a4f(%rip), %rsi      # 0xc1eb
movl $0xe, %edx
movq %r12, %rdi
callq 0x32e0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x6749
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x67d8
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x67d8
jmp 0x67d5
jmp 0x67d5
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x67f4
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x3470