// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %r15
callq 0x487c
movslq 0x360(%r15), %rcx
movslq 0x368(%r15), %rdx
movslq 0x364(%r15), %rax
imulq %rax, %rdx
subq %rdx, %rcx
movq 0x380(%r15), %rdx
movq 0x398(%r15), %rsi
pushq $0x24
popq %rdi
xorl %r8d, %r8d
cmpq %rcx, %r8
jg 0x4d15
movq (%rsi,%r8,8), %r9
movl (%r9), %r10d
movq (%rdx), %r11
movl %r10d, -0x24(%r11,%rdi)
movl 0x4(%r9), %ebx
movl %ebx, -0x20(%r11,%rdi)
movl 0xc(%r9), %r9d
movl %r9d, -0x18(%r11,%rdi)
movl %r10d, -0x8(%r11,%rdi)
movl %ebx, -0x4(%r11,%rdi)
movl %r9d, (%r11,%rdi)
incq %r8
addq $0x28, %rdi
jmp 0x4cd8
pushq $0x1
popq %rbp
movq %r15, (%rsp)
leal -0x1(%rax), %ecx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jge 0x5106
movslq 0x360(%r15), %r14
movslq 0x368(%r15), %rcx
cltq
imulq %rcx, %rax
subq %rax, %r14
movq %r15, %rdi
movl %r14d, %esi
movl %r14d, %edx
movl %ebp, %ecx
callq 0x511a
movq 0x380(%r15), %r12
movq %rax, 0x8(%rsp)
movslq %eax, %rsi
imulq $0x28, %rsi, %rcx
movq -0x8(%r12,%rbp,8), %rdx
movq (%r12,%rbp,8), %rdi
imulq $0x28, %r14, %r8
movq (%rdx,%rcx), %r9
movq %r9, 0x10(%rdi,%r8)
movl 0xc(%rdx,%rcx), %r10d
movl %r10d, 0x18(%rdi,%r8)
movl %r14d, %ecx
subl %esi, %ecx
movslq %ecx, %rdx
movq 0x398(%r15), %rcx
movq (%rcx,%rdx,8), %r11
movl 0x368(%r15), %edx
movl %edx, %ebx
imull %ebp, %ebx
addl %esi, %ebx
movslq %ebx, %rsi
imulq $0x28, %rsi, %rsi
movl (%r11,%rsi), %ebx
movl %ebx, 0x1c(%rdi,%r8)
movl 0x4(%r11,%rsi), %ebx
movl %ebx, 0x20(%rdi,%r8)
movl 0xc(%r11,%rsi), %esi
movl %esi, 0x24(%rdi,%r8)
movl %r9d, (%rdi,%r8)
movl %ebx, 0x4(%rdi,%r8)
addl %r10d, %esi
movl %esi, 0xc(%rdi,%r8)
movq %r14, 0x10(%rsp)
movl %r14d, %r11d
imulq $0x28, %r11, %r13
leal -0x1(%r11), %eax
movq %rax, 0x28(%rsp)
xorl %r10d, %r10d
movq %r11, 0x20(%rsp)
leaq (%r11,%r10), %r9
testl %r9d, %r9d
jle 0x4ffb
leaq (%r11,%r10), %rax
decq %rax
movq %rax, 0x18(%rsp)
movq (%r12,%rbp,8), %rax
movslq 0x1c(%rax,%r13), %rdi
movl %edx, %r8d
imull %ebp, %r8d
movl %edi, %ebx
subl %r8d, %ebx
cmpl %r9d, %ebx
movq 0x28(%rsp), %rsi
jge 0x4f3f
imull %ebp, %edx
subl %edi, %edx
addl %esi, %edx
addl %r10d, %edx
movslq %edx, %r14
movq (%rcx,%r14,8), %rcx
imulq $0x28, %rdi, %rdx
movl 0xc(%rcx,%rdx), %ecx
cmpl 0x24(%rax,%r13), %ecx
jne 0x4f3f
movq %rbp, %r15
leaq (%rsi,%r10), %rbp
movq 0xc162(%rip), %r12      # 0x10fc8
movq %r12, %rdi
leaq 0x640d(%rip), %rsi      # 0xb27d
movq %r10, 0x30(%rsp)
callq 0x3240
movq %rax, %rdi
movl %ebp, %esi
callq 0x3440
movq %rax, %rdi
callq 0x3120
movq %r12, %rdi
callq 0x31a0
movq (%rsp), %rax
movq 0x380(%rax), %r12
movq (%r12,%r15,8), %rsi
movl 0x18(%rsp), %ecx
imulq $0x28, %rcx, %rdi
movq 0x10(%rsi,%r13), %r8
movq %r8, 0x10(%rsi,%rdi)
movl 0x18(%rsi,%r13), %r9d
movl %r9d, 0x18(%rsi,%rdi)
movq 0x398(%rax), %rcx
movq (%rcx,%r14,8), %r10
movl 0x368(%rax), %edx
movl %edx, %r11d
imull %r15d, %r11d
movq 0x8(%rsp), %r14
leal (%r11,%r14), %ebp
movslq %ebp, %r14
imulq $0x28, %r14, %r14
movl (%r10,%r14), %ebp
movl %ebp, 0x1c(%rsi,%rdi)
movq %r15, %rbp
addl %ebx, %r11d
movslq %r11d, %r11
imulq $0x28, %r11, %r11
movl 0x4(%r10,%r11), %ebx
movl %ebx, 0x20(%rsi,%rdi)
movl 0xc(%r10,%r11), %r10d
movq 0x20(%rsp), %r11
movl %r10d, 0x24(%rsi,%rdi)
movl %r8d, (%rsi,%rdi)
movl %ebx, 0x4(%rsi,%rdi)
addl %r9d, %r10d
movl %r10d, 0xc(%rsi,%rdi)
movq 0x30(%rsp), %r10
movq 0x3e8(%rax), %rsi
incq (%rsi)
addq $-0x28, %r13
decq %r10
jmp 0x4df8
leal -0x1(%r11), %ebx
movq 0x8(%rsp), %rsi
cmpl %r9d, %esi
leal -0x1(%r11,%r10), %edx
cmovgel %edx, %esi
movq (%rsp), %r14
movq %r14, %rdi
movl %ebp, %ecx
movq %r10, %r15
callq 0x511a
movq 0x380(%r14), %r12
movq %rax, 0x8(%rsp)
movslq %eax, %r8
imulq $0x28, %r8, %rcx
movq -0x8(%r12,%rbp,8), %rdx
movq (%r12,%rbp,8), %rsi
movq 0x18(%rsp), %rax
movl %eax, %edi
imulq $0x28, %rdi, %rdi
movq (%rdx,%rcx), %r9
movq %r9, 0x10(%rsi,%rdi)
movl 0xc(%rdx,%rcx), %r10d
movl %r10d, 0x18(%rsi,%rdi)
subl %r8d, %ebx
addl %r15d, %ebx
movslq %ebx, %rdx
movq 0x398(%r14), %rcx
movq (%rcx,%rdx,8), %r11
movl 0x368(%r14), %edx
movl %edx, %ebx
imull %ebp, %ebx
addl %r8d, %ebx
movslq %ebx, %r8
imulq $0x28, %r8, %r8
movl (%r11,%r8), %ebx
movl %ebx, 0x1c(%rsi,%rdi)
movl 0x4(%r11,%r8), %ebx
movl %ebx, 0x20(%rsi,%rdi)
movl 0xc(%r11,%r8), %r8d
movl %r8d, 0x24(%rsi,%rdi)
movl %r9d, (%rsi,%rdi)
movl %ebx, 0x4(%rsi,%rdi)
addl %r10d, %r8d
movl %r8d, 0xc(%rsi,%rdi)
movq %rax, %r11
jmp 0x4de3
movq 0xbfc6(%rip), %r12      # 0x10fc8
movq %r12, %rdi
leaq 0x6294(%rip), %rsi      # 0xb2a0
callq 0x3240
pushq $0xc
popq %rbx
xorl %r14d, %r14d
movq (%rsp), %r15
leaq 0x65c4(%rip), %r13      # 0xb5e6
cmpq 0x10(%rsp), %r14
jg 0x5053
movq 0x380(%r15), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx), %esi
movq %r12, %rdi
callq 0x3190
movq %rax, %rdi
movq %r13, %rsi
callq 0x3240
incq %r14
addq $0x28, %rbx
jmp 0x5022
movq %r12, %rdi
callq 0x3120
movq %r12, %rdi
leaq 0x6230(%rip), %rsi      # 0xb295
callq 0x3240
pushq $0x1c
popq %rbx
xorl %r14d, %r14d
cmpq 0x10(%rsp), %r14
jg 0x50a1
movq 0x380(%r15), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx), %esi
movq %r12, %rdi
callq 0x3190
movq %rax, %rdi
movq %r13, %rsi
callq 0x3240
incq %r14
addq $0x28, %rbx
jmp 0x5070
movq %r12, %rdi
callq 0x3120
movq %r12, %rdi
leaq 0x61f5(%rip), %rsi      # 0xb2a8
callq 0x3240
pushq $0x24
popq %rbx
xorl %r14d, %r14d
cmpq 0x10(%rsp), %r14
jg 0x50ef
movq 0x380(%r15), %rax
movq (%rax,%rbp,8), %rax
movl (%rax,%rbx), %esi
movq %r12, %rdi
callq 0x3190
movq %rax, %rdi
movq %r13, %rsi
callq 0x3240
incq %r14
addq $0x28, %rbx
jmp 0x50be
movq %r12, %rdi
callq 0x3120
incq %rbp
movl 0x364(%r15), %eax
jmp 0x4d1c
movb $0x0, (%r15)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop