// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]optimal-seed-solver/optimalSolverLN.cc
// Function: OptimalSolverLN::fillMatrix(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void OptimalSolverLN::fillMatrix(string& DNA) {
	//Load all substrings
	loadL0(DNA);

#ifdef DEBUG
	cout << "l-" << 0 << "  ";
#endif

	//Fill level 0
	int lvPosCount = readLength - minLength * seedNum;
	for (int pos = 0; pos <= lvPosCount; pos++) {
		level[0][pos].start = level0[pos][0].start;
		level[0][pos].end = level0[pos][0].end;
		level[0][pos].frequency = level0[pos][0].frequency;
		//Fill up again for tracing back
		level[0][pos].rstart = level0[pos][0].start;
		level[0][pos].rend = level0[pos][0].end;
		level[0][pos].rfreq = level0[pos][0].frequency;
#ifdef DEBUG
		cout << 0 << "-" << pos + minLength - 1 << ":" << level[0][pos].start << "-"
			<< level[0][pos].end << ":" << level[0][pos].frequency << " ";
#endif
	}
#ifdef DEBUG
	cout << endl;
#endif

	int opt_div;

	//Now calculate the rest levels
	for (int l = 1; l < seedNum - 1; l++) {

		lvPosCount = readLength - minLength * seedNum;
		opt_div = solveFirstOptimal(lvPosCount, lvPosCount, l);
#ifdef DEBUG
		cout << "lvPosCount: " << lvPosCount << " opt_div: " << opt_div << endl;
#endif
		level[l][lvPosCount].lstart = level[l-1][opt_div].start;
		level[l][lvPosCount].lend = level[l-1][opt_div].end;
		level[l][lvPosCount].lfreq = level[l-1][opt_div].frequency;
		level[l][lvPosCount].rstart = level0[lvPosCount - opt_div][opt_div + l * minLength].start;
		level[l][lvPosCount].rend = level0[lvPosCount - opt_div][opt_div + l * minLength].end;
		level[l][lvPosCount].rfreq = level0[lvPosCount - opt_div][opt_div + l * minLength].frequency;
		level[l][lvPosCount].start = level[l][lvPosCount].lstart;
		level[l][lvPosCount].end = level[l][lvPosCount].rend;
		level[l][lvPosCount].frequency = level[l][lvPosCount].lfreq + level[l][lvPosCount].rfreq;

		for (int pos = lvPosCount - 1; pos >= 0; pos--) {

			int prev_opt_div = level[l][pos+1].rstart - l * minLength;
#ifdef DEBUG
			cout << "prev_opt_div: " << prev_opt_div << endl;
#endif
			//Optimal Solution Forwarding
			if (true && prev_opt_div <= pos && level0[pos - prev_opt_div][prev_opt_div + l * minLength].frequency == level[l][pos+1].rfreq) {
#ifdef TEST
				cout << "same as previous; pos: " << pos << endl;
				cout.flush();
#endif
				level[l][pos].lstart = level[l][pos+1].lstart;
				level[l][pos].lend = level[l][pos+1].lend;
				level[l][pos].lfreq = level[l][pos+1].lfreq;
				level[l][pos].rstart = level0[pos - prev_opt_div][opt_div + l * minLength].start;
				level[l][pos].rend = level0[pos - prev_opt_div][prev_opt_div + l * minLength].end;
				level[l][pos].rfreq = level0[pos - prev_opt_div][prev_opt_div + l * minLength].frequency;
				level[l][pos].start = level[l][pos].lstart;
				level[l][pos].end = level[l][pos].rend;
				level[l][pos].frequency = level[l][pos].lfreq + level[l][pos].rfreq;
				divTravel[0]++;
			}
			else {
				if (opt_div > pos)
					opt_div = pos;

				opt_div = solveFirstOptimal(opt_div, pos, l);

				level[l][pos].lstart = level[l-1][opt_div].start;
				level[l][pos].lend = level[l-1][opt_div].end;
				level[l][pos].lfreq = level[l-1][opt_div].frequency;
				level[l][pos].rstart = level0[pos - opt_div][opt_div + l * minLength].start;
				level[l][pos].rend = level0[pos - opt_div][opt_div + l * minLength].end;
				level[l][pos].rfreq = level0[pos - opt_div][opt_div + l * minLength].frequency;
				level[l][pos].start = level[l][pos].lstart;
				level[l][pos].end = level[l][pos].rend;
				level[l][pos].frequency = level[l][pos].lfreq + level[l][pos].rfreq;
			}
		}

#ifdef TEST
		cout << "freq: ";
		for (int pos = 0; pos <= lvPosCount; pos++) {
			cout << level[l][pos].frequency << " ";
		}
		cout << endl;
		cout << "rstart: ";
		for (int pos = 0; pos <= lvPosCount; pos++) {
			cout << level[l][pos].rstart << " ";
		}
		cout << endl;
		cout << "rfreq: ";
		for (int pos = 0; pos <= lvPosCount; pos++) {
			cout << level[l][pos].rfreq << " ";
		}
		cout << endl;
#endif
#ifdef DEBUG
		cout << "l-" << l << "  ";
		for (int pos = 0; pos <= lvPosCount; pos++) {
			cout << 0 << "-" << pos + (l + 1) * minLength - 1 << ":" << level[l][pos].lstart << "-"
				<< level[l][pos].lend << "(" << level[l][pos].lfreq << ")"
				<< level[l][pos].rstart << "-" << level[l][pos].rend << "("
				<< level[l][pos].rfreq << "):" << level[l][pos].frequency << " ";

		}
		cout << endl;
#endif
	}

	L0Loaded = false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r15
callq 0x4d16
movl 0x360(%r15), %eax
movl 0x364(%r15), %ecx
imull 0x368(%r15), %ecx
subl %ecx, %eax
jl 0x524e
movq 0x380(%r15), %rdx
movq 0x398(%r15), %rcx
movq (%rdx), %rdx
incl %eax
addq $0x24, %rdx
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
movq (%rdi), %r8
movq %r8, -0x24(%rdx)
movl 0xc(%rdi), %r8d
movl %r8d, -0x18(%rdx)
movq (%rdi), %r8
movq %r8, -0x8(%rdx)
movl 0xc(%rdi), %edi
movl %edi, (%rdx)
incq %rsi
addq $0x28, %rdx
cmpq %rsi, %rax
jne 0x5223
movl 0x364(%r15), %eax
cmpl $0x3, %eax
jl 0x5735
movl $0x1, %r12d
movl 0x360(%r15), %ebx
imull 0x368(%r15), %eax
subl %eax, %ebx
movq %r15, %rdi
movl %ebx, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0x5748
movl %eax, %r11d
movq 0x380(%r15), %rax
movslq %r11d, %rcx
leaq (%rcx,%rcx,4), %rdx
movq -0x8(%rax,%r12,8), %rsi
movq (%rax,%r12,8), %rax
movq %rbx, 0x8(%rsp)
movslq %ebx, %rcx
leaq (%rcx,%rcx,4), %rdi
movq (%rsi,%rdx,8), %r8
movq %r8, 0x10(%rax,%rdi,8)
movl 0xc(%rsi,%rdx,8), %edx
movl %edx, 0x18(%rax,%rdi,8)
movl %ecx, %esi
subl %r11d, %esi
movslq %esi, %rsi
movq 0x398(%r15), %r9
movq (%r9,%rsi,8), %rsi
movl 0x368(%r15), %r9d
movq %r12, 0x20(%rsp)
imull %r12d, %r9d
addl %r11d, %r9d
movslq %r9d, %r9
leaq (%r9,%r9,4), %r9
movl (%rsi,%r9,8), %r10d
movl %r10d, 0x1c(%rax,%rdi,8)
movl 0x4(%rsi,%r9,8), %r10d
movl %r10d, 0x20(%rax,%rdi,8)
movl 0xc(%rsi,%r9,8), %esi
movl %esi, 0x24(%rax,%rdi,8)
movl %r8d, (%rax,%rdi,8)
movl %r10d, 0x4(%rax,%rdi,8)
addl %edx, %esi
movl %esi, 0xc(%rax,%rdi,8)
testl %ecx, %ecx
jle 0x558a
movl 0x8(%rsp), %r10d
leaq (,%r10,8), %rax
leaq (%rax,%rax,4), %r13
movl $0x1, %ecx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq %r10, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
leaq (%r10,%r14), %rsi
leaq (%r10,%r14), %rax
decq %rax
movq %rax, 0x28(%rsp)
leaq (%r10,%r12), %r8
movq 0x380(%r15), %rax
movq 0x20(%rsp), %rbp
movq (%rax,%rbp,8), %rax
movslq 0x1c(%rax,%r13), %rcx
movl 0x368(%r15), %edx
movl %edx, %edi
imull %ebp, %edi
movq %r15, %rbx
movl %ecx, %r15d
subl %edi, %r15d
cmpl %esi, %r15d
jge 0x54c2
imull %ebp, %edx
subl %ecx, %edx
addl %r10d, %edx
addl %r12d, %edx
movslq %edx, %rsi
movq 0x398(%rbx), %rdx
movq (%rdx,%rsi,8), %rdx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rdx,%rcx,8), %ecx
cmpl 0x24(%rax,%r13), %ecx
jne 0x54c2
movl $0x17, %edx
movq 0xbc05(%rip), %rdi      # 0x10fc8
movq %rsi, 0x38(%rsp)
leaq 0x6ece(%rip), %rsi      # 0xc29d
movq %r11, 0x40(%rsp)
movq %r8, 0x10(%rsp)
callq 0x32e0
movq 0xbbe3(%rip), %rdi      # 0x10fc8
movq 0x10(%rsp), %rsi
callq 0x3450
movq %rax, 0x10(%rsp)
movq (%rax), %rcx
movq -0x18(%rcx), %rdi
addq %rax, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq 0x10(%rsp), %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movq 0xbba4(%rip), %rdi      # 0x10fc8
callq 0x31c0
movq 0x18(%rsp), %r10
movq 0x40(%rsp), %r11
movq 0x380(%rbx), %rax
movq (%rax,%rbp,8), %rax
movl 0x28(%rsp), %ecx
leaq (%rcx,%rcx,4), %rcx
movq 0x10(%rax,%r13), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movl 0x18(%rax,%r13), %esi
movl %esi, 0x18(%rax,%rcx,8)
movq 0x398(%rbx), %rdi
movq 0x38(%rsp), %r8
movq (%rdi,%r8,8), %rdi
movl 0x368(%rbx), %r8d
imull %ebp, %r8d
leal (%r8,%r11), %r9d
movslq %r9d, %r9
leaq (%r9,%r9,4), %r9
movl (%rdi,%r9,8), %r9d
movl %r9d, 0x1c(%rax,%rcx,8)
addl %r15d, %r8d
movslq %r8d, %r8
leaq (%r8,%r8,4), %r8
movl 0x4(%rdi,%r8,8), %r9d
movl %r9d, 0x20(%rax,%rcx,8)
movl 0xc(%rdi,%r8,8), %edi
movl %edi, 0x24(%rax,%rcx,8)
movl %edx, (%rax,%rcx,8)
movl %r9d, 0x4(%rax,%rcx,8)
addl %esi, %edi
movl %edi, 0xc(%rax,%rcx,8)
movq 0x3e8(%rbx), %rax
incq (%rax)
jmp 0x5568
leal (%r10,%r14), %eax
cmpl %eax, %r11d
cmovgel %r8d, %r11d
movq %rbx, %rdi
movl %r11d, %esi
movl %r8d, %edx
movl %ebp, %ecx
callq 0x5748
movq 0x18(%rsp), %r10
movl %eax, %r11d
movq 0x380(%rbx), %rax
movslq %r11d, %rdx
leaq (%rdx,%rdx,4), %rsi
movq -0x8(%rax,%rbp,8), %rdi
movq (%rax,%rbp,8), %rax
movl 0x28(%rsp), %ecx
leaq (%rcx,%rcx,4), %rcx
movq (%rdi,%rsi,8), %r8
movq %r8, 0x10(%rax,%rcx,8)
movl 0xc(%rdi,%rsi,8), %esi
movl %esi, 0x18(%rax,%rcx,8)
leal (%r12,%r10), %edi
subl %edx, %edi
movslq %edi, %rdi
movq 0x398(%rbx), %r9
movq (%r9,%rdi,8), %rdi
movl 0x368(%rbx), %r9d
imull %ebp, %r9d
addl %edx, %r9d
movslq %r9d, %rdx
leaq (%rdx,%rdx,4), %rdx
movl (%rdi,%rdx,8), %r9d
movl %r9d, 0x1c(%rax,%rcx,8)
movl 0x4(%rdi,%rdx,8), %r9d
movl %r9d, 0x20(%rax,%rcx,8)
movl 0xc(%rdi,%rdx,8), %edx
movl %edx, 0x24(%rax,%rcx,8)
movl %r8d, (%rax,%rcx,8)
movl %r9d, 0x4(%rax,%rcx,8)
addl %esi, %edx
movl %edx, 0xc(%rax,%rcx,8)
movq %rbx, %r15
movq 0x30(%rsp), %rcx
decq %rcx
leal (%rcx,%r10), %eax
addq $-0x28, %r13
decq %r14
decq %r12
cmpl $0x1, %eax
jg 0x533f
movl $0x6, %edx
movq 0xba32(%rip), %r13      # 0x10fc8
movq %r13, %rdi
leaq 0x6d20(%rip), %rsi      # 0xc2c0
callq 0x32e0
cmpl $0x0, 0x8(%rsp)
movq 0x20(%rsp), %r12
leaq 0x7068(%rip), %rbp      # 0xc61e
js 0x55f6
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ebx
movl $0xc, %r14d
movq 0x380(%r15), %rax
movq (%rax,%r12,8), %rax
movl (%rax,%r14), %esi
movq %r13, %rdi
callq 0x31b0
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x32e0
addq $0x28, %r14
decq %rbx
jne 0x55c6
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movl $0x8, %edx
movq %r13, %rdi
leaq 0x6c88(%rip), %rsi      # 0xc2b5
callq 0x32e0
cmpl $0x0, 0x8(%rsp)
js 0x5677
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ebx
movl $0x1c, %r14d
movq 0x380(%r15), %rax
movq (%rax,%r12,8), %rax
movl (%rax,%r14), %esi
movq %r13, %rdi
callq 0x31b0
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x32e0
addq $0x28, %r14
decq %rbx
jne 0x5647
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
movl $0x7, %edx
movq %r13, %rdi
leaq 0x6c1a(%rip), %rsi      # 0xc2c8
callq 0x32e0
movq 0x8(%rsp), %r14
testl %r14d, %r14d
js 0x56f4
incl %r14d
movl $0x24, %ebx
movq 0x380(%r15), %rax
movq (%rax,%r12,8), %rax
movl (%rax,%rbx), %esi
movq %r13, %rdi
callq 0x31b0
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x32e0
addq $0x28, %rbx
decq %r14
jne 0x56c5
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %r13, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31c0
incq %r12
movslq 0x364(%r15), %rax
movq %rax, %rcx
decq %rcx
cmpq %rcx, %r12
jl 0x5264
movb $0x0, (%r15)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq