// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]optimal-seed-solver/optimalSolverLN.cc
// Function: OptimalSolverLN::loadL0(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void OptimalSolverLN::loadL0(string& DNA) {
	bool edge_left;
	bool edge_right;
	int lvPosCount = readLength + 1 - minLength;

	if (!L0Loaded) {
		assert(DNA.length() == static_cast<unsigned int>(readLength) );

		reset();

		//Calculate the first level

#ifdef DEBUG
		cout << "*L0*: " << endl << "*subl 0*: ";
#endif
		for (int i = 0; i < lvPosCount; i++) {
			string seed = DNA.substr(i, minLength);
			tree.query(seed);
			level0[0][i].start = i;
			level0[0][i].end = i + minLength - 1;
			level0[0][i].frequency = tree.frequency();
			level0[0][i].isleaf = tree.isLeaf();

#ifdef DEBUG
			cout << level0[0][i].start << "-" << level0[0][i].end << ":" << level0[0][i].frequency << " ";
#endif
		}
#ifdef DEBUG
		cout << endl;
#endif

		for (int i = 1; i < lvPosCount; i++) {
#ifdef DEBUG
			cout << "*subl" << i << "*: ";
#endif
			for (int j = 0; j < lvPosCount - i; j++) {
				edge_left = level0[i-1][j].start == static_cast<unsigned int>(j);
				edge_right = level0[i-1][j+1].end == static_cast<unsigned int>(j + i + minLength - 1);

				// If left edge but not right, copy left seed
				if (edge_left && !edge_right) {
					level0[i][j].isleaf = level0[i-1][j].isleaf;
					level0[i][j].frequency = level0[i-1][j].frequency;
					level0[i][j].start = level0[i-1][j].start;
					level0[i][j].end = level0[i-1][j].end;
				}
				// If right edge but not left, copy right seed
				else if (!edge_left && edge_right) {
					level0[i][j].isleaf = level0[i-1][j+1].isleaf;
					level0[i][j].frequency = level0[i-1][j+1].frequency;
					level0[i][j].start = level0[i-1][j+1].start;
					level0[i][j].end = level0[i-1][j+1].end;
				}
				// If both are edging, then we have to test potentially three cases
				else if (edge_left && edge_right) {
					// If left is not leaf, always merge
					if (!level0[i-1][j].isleaf) {
						string seed = DNA.substr(j, minLength + i);

						//TODO: Optimize query
						tree.query(seed);
						level0[i][j].isleaf = tree.isLeaf();
						level0[i][j].frequency = tree.frequency();
						level0[i][j].start = j;
						level0[i][j].end = j + minLength + i - 1;
					}
					// Check left seed vs right seed if not merging.
					else {
						// Prioritize left position over right position
						if (level0[i-1][j].frequency <= level0[i-1][j+1].frequency) {
							level0[i][j].isleaf = level0[i-1][j].isleaf;
							level0[i][j].frequency = level0[i-1][j].frequency;
							level0[i][j].start = level0[i-1][j].start;
							level0[i][j].end = level0[i-1][j].end;
						}
						else {
							level0[i][j].isleaf = level0[i-1][j+1].isleaf;
							level0[i][j].frequency = level0[i-1][j+1].frequency;
							level0[i][j].start = level0[i-1][j+1].start;
							level0[i][j].end = level0[i-1][j+1].end;
						}
					}

				}
				// None is edging. The left and right seed should be equal.
				else {
					assert(level0[i-1][j].start == level0[i-1][j+1].start);
					assert(level0[i-1][j].end == level0[i-1][j+1].end);
					assert(level0[i-1][j].frequency == level0[i-1][j+1].frequency);
					assert(level0[i-1][j].isleaf == level0[i-1][j+1].isleaf);
					level0[i][j].start = level0[i-1][j].start;
					level0[i][j].end = level0[i-1][j].end;
					level0[i][j].frequency = level0[i-1][j].frequency;
					level0[i][j].isleaf = level0[i-1][j].isleaf;
				}

#ifdef DEBUG
				cout << level0[i][j].start << "-" << level0[i][j].end << ":" << level0[i][j].frequency << "|" << level0[i][j].isleaf << " ";
#endif
			}
#ifdef DEBUG
			cout << endl;
#endif
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, 0x18(%rsp)
cmpb $0x0, (%rdi)
je 0x48a9
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
movl 0x360(%rdi), %ebx
movq 0x18(%rsp), %rax
cmpq %rbx, 0x8(%rax)
jne 0x4c42
subl 0x368(%r14), %ebx
incl %ebx
movq %r14, %rdi
callq 0x4746
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
testl %ebx, %ebx
movl $0x0, %eax
movq %rbx, 0x10(%rsp)
cmovgl %ebx, %eax
movq %rax, (%rsp)
leaq 0x28(%rsp), %r12
leaq 0x68(%rsp), %r13
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
cmpq %rbp, (%rsp)
je 0x49a6
movslq 0x368(%r14), %rcx
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x3270
movq %r13, %rdi
movq %r12, %rsi
callq 0x3360
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x7cea
movq %r13, %rdi
callq 0x3110
movq 0x398(%r14), %rax
movq (%rax), %rax
movl %ebp, (%rax,%r15)
movl 0x368(%r14), %ecx
leal -0x1(%rbp,%rcx), %ecx
movl %ecx, 0x4(%rax,%r15)
movq %rbx, %rdi
callq 0x7e34
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movl %eax, 0xc(%rcx,%r15)
movq 0x8(%rsp), %rdi
callq 0x7e48
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movb %al, 0x8(%rcx,%r15)
movq %r12, %rdi
callq 0x3110
incq %rbp
addq $0x28, %r15
jmp 0x4902
movslq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rbx
movq $0x0, (%rsp)
movq 0x20(%rsp), %r10
subq %rbx, %r10
jle 0x4897
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r10, 0x10(%rsp)
cmpq %r10, %rbp
jge 0x4bba
movq 0x398(%r14), %rdi
movq -0x8(%rdi,%rbx,8), %rax
movl (%rax,%r15), %esi
movl 0x2c(%rax,%r15), %edx
movslq 0x368(%r14), %rcx
movq (%rsp), %r8
addl %ebp, %r8d
leal (%rcx,%r8), %r9d
cmpq %rsi, %rbp
setne %r8b
jne 0x4a3c
cmpl %edx, %r9d
je 0x4a3c
movb 0x8(%rax,%r15), %cl
movq (%rdi,%rbx,8), %rdx
movb %cl, 0x8(%rdx,%r15)
movl 0xc(%rax,%r15), %ecx
movl %ecx, 0xc(%rdx,%r15)
movl %esi, (%rdx,%r15)
movl 0x4(%rax,%r15), %eax
movl %eax, 0x4(%rdx,%r15)
jmp 0x4ba0
cmpq %rsi, %rbp
je 0x4a71
cmpl %edx, %r9d
jne 0x4a71
movb 0x30(%rax,%r15), %cl
movq (%rdi,%rbx,8), %rsi
movb %cl, 0x8(%rsi,%r15)
movl 0x34(%rax,%r15), %ecx
movl %ecx, 0xc(%rsi,%r15)
movl 0x28(%rax,%r15), %eax
movl %eax, (%rsi,%r15)
movl %edx, 0x4(%rsi,%r15)
jmp 0x4ba0
cmpl %edx, %r9d
setne %r9b
orb %r9b, %r8b
je 0x4acf
cmpl 0x28(%rax,%r15), %esi
jne 0x4c04
cmpl %edx, 0x4(%rax,%r15)
jne 0x4c23
movl 0xc(%rax,%r15), %ecx
cmpl 0x34(%rax,%r15), %ecx
jne 0x4bc6
movb 0x8(%rax,%r15), %r8b
cmpb 0x30(%rax,%r15), %r8b
jne 0x4be5
movq (%rdi,%rbx,8), %rax
movl %esi, (%rax,%r15)
movl %edx, 0x4(%rax,%r15)
movl %ecx, 0xc(%rax,%r15)
movb %r8b, 0x8(%rax,%r15)
jmp 0x4ba0
movb 0x8(%rax,%r15), %r8b
testb %r8b, %r8b
je 0x4b04
movl 0xc(%rax,%r15), %ecx
movl 0x34(%rax,%r15), %r9d
cmpl %r9d, %ecx
jbe 0x4bac
movb 0x30(%rax,%r15), %cl
movq (%rdi,%rbx,8), %rsi
movb %cl, 0x8(%rsi,%r15)
movl %r9d, 0xc(%rsi,%r15)
jmp 0x4a5e
addq %rbx, %rcx
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x3270
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x3360
movq 0x8(%rsp), %r12
movq %r12, %rdi
leaq 0x48(%rsp), %r13
movq %r13, %rsi
pushq $0x1
popq %rdx
callq 0x7cea
movq %r13, %rdi
callq 0x3110
movq %r12, %rdi
callq 0x7e48
movq 0x398(%r14), %rcx
movq (%rcx,%rbx,8), %rcx
movb %al, 0x8(%rcx,%r15)
movq 0x8(%rsp), %rdi
callq 0x7e34
movq 0x398(%r14), %rcx
movq (%rcx,%rbx,8), %rcx
movl %eax, 0xc(%rcx,%r15)
movl %ebp, (%rcx,%r15)
movq (%rsp), %rax
addl %ebp, %eax
addl 0x368(%r14), %eax
movl %eax, 0x4(%rcx,%r15)
leaq 0x28(%rsp), %rdi
callq 0x3110
movq 0x10(%rsp), %r10
addq $0x28, %r15
incq %rbp
jmp 0x49d3
movq (%rdi,%rbx,8), %rdx
movb %r8b, 0x8(%rdx,%r15)
jmp 0x4a24
incq %rbx
incq (%rsp)
jmp 0x49bb
leaq 0x6648(%rip), %rdi      # 0xb215
leaq 0x6535(%rip), %rsi      # 0xb109
leaq 0x65b9(%rip), %rcx      # 0xb194
movl $0x11c, %edx            # imm = 0x11C
callq 0x3170
leaq 0x6660(%rip), %rdi      # 0xb24c
leaq 0x6516(%rip), %rsi      # 0xb109
leaq 0x659a(%rip), %rcx      # 0xb194
movl $0x11d, %edx            # imm = 0x11D
callq 0x3170
leaq 0x65b0(%rip), %rdi      # 0xb1bb
leaq 0x64f7(%rip), %rsi      # 0xb109
leaq 0x657b(%rip), %rcx      # 0xb194
movl $0x11a, %edx            # imm = 0x11A
callq 0x3170
leaq 0x65c0(%rip), %rdi      # 0xb1ea
leaq 0x64d8(%rip), %rsi      # 0xb109
leaq 0x655c(%rip), %rcx      # 0xb194
movl $0x11b, %edx            # imm = 0x11B
callq 0x3170
leaq 0x6515(%rip), %rdi      # 0xb15e
leaq 0x64b9(%rip), %rsi      # 0xb109
leaq 0x653d(%rip), %rcx      # 0xb194
movl $0xca, %edx
callq 0x3170
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x4c73
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3110
jmp 0x4c7f
jmp 0x4c7c
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x3470
nop