// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rsi, 0x28(%rsp)
cmpb $0x0, (%rdi)
jne 0x50e1
movq %rdi, %r14
movl 0x360(%rdi), %ebx
movq 0x28(%rsp), %rax
cmpq %rbx, 0x8(%rax)
jne 0x516c
subl 0x368(%r14), %ebx
incl %ebx
movq 0x370(%r14), %rdi
movq 0x3b0(%r14), %rsi
movslq 0x3c0(%r14), %rdx
callq 0x3200
movq 0x378(%r14), %rdi
movq 0x3b8(%r14), %rsi
movslq 0x3c4(%r14), %rdx
callq 0x3200
testl %ebx, %ebx
jle 0x50e1
leaq 0x8(%r14), %rax
movq %rax, (%rsp)
leaq 0x68(%rsp), %r15
movl %ebx, %eax
movl $0x8, %r13d
xorl %r12d, %r12d
leaq 0x58(%rsp), %rbp
movq %rax, 0x30(%rsp)
movslq 0x368(%r14), %rcx
leaq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
callq 0x32a0
movq %r15, 0x58(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x631a
movq (%rsp), %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x89c4
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x4e20
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x398(%r14), %rax
movq (%rax), %rax
movl %r12d, -0x8(%rax,%r13)
movl 0x368(%r14), %ecx
addl %r12d, %ecx
decl %ecx
movl %ecx, -0x4(%rax,%r13)
movq (%rsp), %rdi
callq 0x8b4c
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movl %eax, 0x4(%rcx,%r13)
movq (%rsp), %rdi
callq 0x8b60
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%r13)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4e8b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
incq %r12
addq $0x28, %r13
movq 0x30(%rsp), %rax
cmpq %r12, %rax
jne 0x4dc5
cmpl $0x1, %ebx
je 0x50e1
movl $0x1, %r12d
xorl %ebx, %ebx
movq %rax, %r13
subq %r12, %r13
jle 0x50cd
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x398(%r14), %rdi
movq -0x8(%rdi,%r12,8), %rax
movl (%rax,%r15), %esi
movl 0x2c(%rax,%r15), %edx
movslq 0x368(%r14), %rcx
leal (%rbx,%rbp), %r8d
leal (%rcx,%r8), %r9d
cmpq %rsi, %rbp
setne %r8b
jne 0x4f1f
cmpl %edx, %r9d
je 0x4f1f
movb 0x8(%rax,%r15), %cl
movq (%rdi,%r12,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 0x50ad
cmpq %rsi, %rbp
je 0x4f54
cmpl %edx, %r9d
jne 0x4f54
movb 0x30(%rax,%r15), %cl
movq (%rdi,%r12,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 0x50ad
cmpl %edx, %r9d
setne %r9b
orb %r9b, %r8b
je 0x4fb2
cmpl 0x28(%rax,%r15), %esi
jne 0x512e
cmpl %edx, 0x4(%rax,%r15)
jne 0x50f0
movl 0xc(%rax,%r15), %ecx
cmpl 0x34(%rax,%r15), %ecx
jne 0x510f
movb 0x8(%rax,%r15), %r8b
cmpb 0x30(%rax,%r15), %r8b
jne 0x514d
movq (%rdi,%r12,8), %rax
movl %esi, (%rax,%r15)
movl %edx, 0x4(%rax,%r15)
movl %ecx, 0xc(%rax,%r15)
movb %r8b, 0x8(%rax,%r15)
jmp 0x50ad
movb 0x8(%rax,%r15), %r8b
testb %r8b, %r8b
je 0x4fe7
movl 0xc(%rax,%r15), %ecx
movl 0x34(%rax,%r15), %r9d
cmpl %r9d, %ecx
jbe 0x50bf
movb 0x30(%rax,%r15), %cl
movq (%rdi,%r12,8), %rsi
movb %cl, 0x8(%rsi,%r15)
movl %r9d, 0xc(%rsi,%r15)
jmp 0x4f41
addq %r12, %rcx
leaq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rbp, %rdx
callq 0x32a0
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x631a
movq (%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x89c4
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x504c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3290
movq (%rsp), %rdi
callq 0x8b60
movq 0x398(%r14), %rcx
movq (%rcx,%r12,8), %rcx
movb %al, 0x8(%rcx,%r15)
movq (%rsp), %rdi
callq 0x8b4c
movq 0x398(%r14), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, 0xc(%rcx,%r15)
movl %ebp, (%rcx,%r15)
leal (%rbx,%rbp), %eax
addl 0x368(%r14), %eax
movl %eax, 0x4(%rcx,%r15)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x50ad
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
incq %rbp
addq $0x28, %r15
cmpq %r13, %rbp
jl 0x4ec2
jmp 0x50cd
movq (%rdi,%r12,8), %rdx
movb %r8b, 0x8(%rdx,%r15)
jmp 0x4f07
incq %r12
incq %rbx
movq 0x30(%rsp), %rax
cmpq %rax, %r12
jne 0x4eb1
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7113(%rip), %rdi      # 0xc20a
leaq 0x702b(%rip), %rsi      # 0xc129
leaq 0x70af(%rip), %rcx      # 0xc1b4
movl $0x11b, %edx            # imm = 0x11B
callq 0x3190
leaq 0x711f(%rip), %rdi      # 0xc235
leaq 0x700c(%rip), %rsi      # 0xc129
leaq 0x7090(%rip), %rcx      # 0xc1b4
movl $0x11c, %edx            # imm = 0x11C
callq 0x3190
leaq 0x70a6(%rip), %rdi      # 0xc1db
leaq 0x6fed(%rip), %rsi      # 0xc129
leaq 0x7071(%rip), %rcx      # 0xc1b4
movl $0x11a, %edx            # imm = 0x11A
callq 0x3190
leaq 0x7118(%rip), %rdi      # 0xc26c
leaq 0x6fce(%rip), %rsi      # 0xc129
leaq 0x7052(%rip), %rcx      # 0xc1b4
movl $0x11d, %edx            # imm = 0x11D
callq 0x3190
leaq 0x700b(%rip), %rdi      # 0xc17e
leaq 0x6faf(%rip), %rsi      # 0xc129
leaq 0x7033(%rip), %rcx      # 0xc1b4
movl $0xca, %edx
callq 0x3190
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x51c5
movq 0x48(%rsp), %rsi
jmp 0x51b6
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x51c5
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x51c5
jmp 0x51c2
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x51e1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x3480
nop