// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, %r12
movq %rdi, %r14
cmpb $0x1, 0xcc(%rdi)
jne 0x52e2
movl 0x428(%r14), %ebp
movq 0xad4d(%rip), %r15      # 0xffc8
leaq 0x4ef6(%rip), %rsi      # 0xa178
movq %r15, %rdi
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x3450
leaq 0x52f1(%rip), %rsi      # 0xa58c
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movq %r12, %rsi
callq 0x3220
movq %rax, %rdi
callq 0x3120
movq %r15, %rdi
callq 0x31c0
leaq 0x4ec2(%rip), %rsi      # 0xa187
movq %r15, %rdi
callq 0x3250
movq 0x8(%r12), %rsi
movq %rax, %rdi
callq 0x31b0
movq %rax, %rdi
callq 0x3120
leaq 0xd0(%r14), %rbx
movq %rbx, %rdi
callq 0x6e46
movl %eax, %r15d
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x6e4a
movl %eax, %ebp
leaq 0x430(%r14), %rdi
movq %r12, %rsi
callq 0x3140
movslq %ebp, %rax
movq %rax, 0x20(%rsp)
movl %r15d, 0xc(%rsp)
movslq %r15d, %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movb $0x1, %al
movl %eax, 0x4(%rsp)
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
movq 0x458(%r14), %rax
subq 0x450(%r14), %rax
sarq $0x2, %rax
cmpq %rbx, %rax
jbe 0x557f
movq 0x8(%r12), %rax
movslq %r13d, %rdx
movq %rax, %rsi
subq %rdx, %rsi
movq 0x20(%rsp), %rdi
movq %rdi, %rcx
cmpq %rdi, %rsi
movq 0x10(%rsp), %rbp
jae 0x5385
subl %r13d, %eax
cmpl 0xc(%rsp), %eax
jle 0x5602
movslq %eax, %rcx
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x3280
cmpb $0x1, 0xcc(%r14)
jne 0x53d1
movq 0xac25(%rip), %rdi      # 0xffc8
leaq 0x4dfe(%rip), %rsi      # 0xa1a8
callq 0x3250
movq %rax, %rdi
movl %r13d, %esi
callq 0x3450
movq %rax, %rdi
leaq 0x518b(%rip), %rsi      # 0xa54f
callq 0x3250
movq %rax, %rdi
callq 0x3120
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3380
movq %r12, %r15
movq 0x450(%r14), %rax
movl (%rax,%rbx,4), %edx
movq %rbp, %rdi
leaq 0x48(%rsp), %r12
movq %r12, %rsi
callq 0x6e4e
movq %r12, %rdi
callq 0x3110
movq %rbp, %rdi
callq 0x6f90
movl %eax, %r12d
movq %rbp, %rdi
callq 0x6f98
movl %eax, %ebp
cmpb $0x1, 0xcc(%r14)
jne 0x5463
movq 0xab9d(%rip), %rdi      # 0xffc8
leaq 0x4d80(%rip), %rsi      # 0xa1b2
callq 0x3250
movq %rax, %rdi
movl %r12d, %esi
callq 0x3450
movq %rax, %rdi
leaq 0x4d79(%rip), %rsi      # 0xa1c5
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x3450
movq %rax, %rdi
callq 0x3120
movq 0x450(%r14), %rax
cmpl (%rax,%rbx,4), %ebp
jle 0x5495
movq 0x10(%rsp), %rdi
callq 0x6fac
movb 0xcc(%r14), %cl
testb %al, %al
je 0x5528
leaq 0x4d5e(%rip), %rsi      # 0xa1ed
testb %cl, %cl
jne 0x54a6
jmp 0x54ba
cmpb $0x0, 0xcc(%r14)
leaq 0x4d2e(%rip), %rsi      # 0xa1d2
je 0x54ba
movq 0xab1b(%rip), %rdi      # 0xffc8
callq 0x3250
movq %rax, %rdi
callq 0x3120
addl %r12d, %r13d
movslq %r13d, %rax
movq %r15, %r12
movq 0x8(%r15), %rcx
cmpq %rax, %rcx
jne 0x54f7
cmpb $0x1, 0xcc(%r14)
jne 0x5567
movq 0xaae7(%rip), %rdi      # 0xffc8
leaq 0x4d45(%rip), %rsi      # 0xa22d
callq 0x3250
movq %rax, %rdi
callq 0x3120
jmp 0x554f
subq 0x18(%rsp), %rcx
cmpq %rax, %rcx
jae 0x5556
cmpb $0x1, 0xcc(%r14)
jne 0x555f
movq 0xaab6(%rip), %rdi      # 0xffc8
leaq 0x4d36(%rip), %rsi      # 0xa24f
callq 0x3250
movq %rax, %rdi
callq 0x3120
jmp 0x5547
testb %cl, %cl
je 0x5547
movq 0xaa95(%rip), %rdi      # 0xffc8
leaq 0x4cce(%rip), %rsi      # 0xa208
callq 0x3250
movq %rax, %rdi
callq 0x3120
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
movq %r15, %r12
jmp 0x5569
addl %ebp, 0x8(%rsp)
movb $0x1, %bpl
jmp 0x5569
movl $0x0, 0x4(%rsp)
xorl %ebp, %ebp
leaq 0x28(%rsp), %rdi
callq 0x3110
incq %rbx
testb %bpl, %bpl
jne 0x533a
testb $0x1, 0x4(%rsp)
je 0x562c
cmpb $0x1, 0xcc(%r14)
jne 0x55fc
movq 0xaa2d(%rip), %rdi      # 0xffc8
leaq 0x4cc7(%rip), %rsi      # 0xa269
callq 0x3250
movq %rax, %rdi
movl %r13d, %esi
callq 0x3450
leaq 0x4ccb(%rip), %rsi      # 0xa284
movq %rax, %rdi
callq 0x3250
movq 0x8(%r12), %rsi
movq %rax, %rdi
callq 0x31b0
leaq 0x4cb6(%rip), %rsi      # 0xa28b
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
movl 0x8(%rsp), %ebx
movl %ebx, %esi
callq 0x3450
leaq 0x4a5f(%rip), %rsi      # 0xa051
movq %rax, %rdi
callq 0x3250
jmp 0x5622
movl 0x8(%rsp), %ebx
jmp 0x562f
cmpb $0x1, 0xcc(%r14)
jne 0x562c
movq 0xa9b5(%rip), %rdi      # 0xffc8
leaq 0x4b7f(%rip), %rsi      # 0xa199
callq 0x3250
pushq $-0x1
popq %rbx
movq %rax, %rdi
callq 0x3120
jmp 0x562f
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5653
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3110
jmp 0x5656
jmp 0x5653
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3470