// File: /workspace/github_cpp_makefile_star2_O1/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 0x50d1
movq %rdi, %r14
movl 0x360(%rdi), %ebx
movq 0x28(%rsp), %rax
cmpq %rbx, 0x8(%rax)
jne 0x515c
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
movl %ebx, %eax
movq %rbx, (%rsp)
testl %ebx, %ebx
movq %rax, 0x30(%rsp)
jle 0x4e85
leaq 0x8(%r14), %r15
leaq 0x68(%rsp), %r13
movl $0x8, %ebx
xorl %r12d, %r12d
leaq 0x58(%rsp), %rbp
movslq 0x368(%r14), %rcx
leaq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
callq 0x32a0
movq %r13, 0x58(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
movq %rbp, %rdi
callq 0x625c
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0x8962
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x4e0a
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x398(%r14), %rax
movq (%rax), %rax
movl %r12d, -0x8(%rax,%rbx)
movl 0x368(%r14), %ecx
addl %r12d, %ecx
decl %ecx
movl %ecx, -0x4(%rax,%rbx)
movq %r15, %rdi
callq 0x8b00
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movl %eax, 0x4(%rcx,%rbx)
movq %r15, %rdi
callq 0x8b14
movq 0x398(%r14), %rcx
movq (%rcx), %rcx
movb %al, (%rcx,%rbx)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4e70
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
incq %r12
addq $0x28, %rbx
movq 0x30(%rsp), %rax
cmpq %r12, %rax
jne 0x4db0
cmpl $0x2, (%rsp)
jl 0x50d1
leaq 0x8(%r14), %rcx
movq %rcx, (%rsp)
movl $0x1, %r12d
xorl %ebx, %ebx
movq %rax, %r13
subq %r12, %r13
jle 0x50bd
xorl %r15d, %r15d
xorl %ebp, %ebp
movq 0x398(%r14), %rsi
movq -0x8(%rsi,%r12,8), %rax
movl (%rax,%r15), %edx
movl 0x2c(%rax,%r15), %edi
movslq 0x368(%r14), %rcx
leal (%rbx,%rbp), %r8d
leal (%rcx,%r8), %r9d
cmpq %rdx, %rbp
setne %r8b
jne 0x4f0d
cmpl %edi, %r9d
je 0x4f0d
movb 0x8(%rax,%r15), %cl
movq (%rsi,%r12,8), %rsi
movb %cl, 0x8(%rsi,%r15)
movl 0xc(%rax,%r15), %ecx
movl %ecx, 0xc(%rsi,%r15)
movl %edx, (%rsi,%r15)
movl 0x4(%rax,%r15), %eax
movl %eax, 0x4(%rsi,%r15)
jmp 0x509d
cmpq %rdx, %rbp
je 0x4f42
cmpl %edi, %r9d
jne 0x4f42
movb 0x30(%rax,%r15), %cl
movq (%rsi,%r12,8), %rdx
movb %cl, 0x8(%rdx,%r15)
movl 0x34(%rax,%r15), %ecx
movl %ecx, 0xc(%rdx,%r15)
movl 0x28(%rax,%r15), %eax
movl %eax, (%rdx,%r15)
movl %edi, 0x4(%rdx,%r15)
jmp 0x509d
cmpl %edi, %r9d
setne %r9b
orb %r9b, %r8b
je 0x4fa2
cmpl 0x28(%rax,%r15), %edx
jne 0x511e
movl 0x4(%rax,%r15), %ecx
cmpl %edi, %ecx
jne 0x50e0
movl 0xc(%rax,%r15), %edi
cmpl 0x34(%rax,%r15), %edi
jne 0x50ff
movb 0x8(%rax,%r15), %r8b
cmpb 0x30(%rax,%r15), %r8b
jne 0x513d
movq (%rsi,%r12,8), %rax
movl %edx, (%rax,%r15)
movl %ecx, 0x4(%rax,%r15)
movl %edi, 0xc(%rax,%r15)
movb %r8b, 0x8(%rax,%r15)
jmp 0x509d
movb 0x8(%rax,%r15), %r8b
testb %r8b, %r8b
je 0x4fd7
movl 0xc(%rax,%r15), %ecx
movl 0x34(%rax,%r15), %r9d
cmpl %r9d, %ecx
jbe 0x50af
movb 0x30(%rax,%r15), %cl
movq (%rsi,%r12,8), %rdx
movb %cl, 0x8(%rdx,%r15)
movl %r9d, 0xc(%rdx,%r15)
jmp 0x4f2f
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 0x625c
movq (%rsp), %rdi
leaq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x8962
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x503c
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x3290
movq (%rsp), %rdi
callq 0x8b14
movq 0x398(%r14), %rcx
movq (%rcx,%r12,8), %rcx
movb %al, 0x8(%rcx,%r15)
movq (%rsp), %rdi
callq 0x8b00
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 0x509d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
incq %rbp
addq $0x28, %r15
cmpq %r13, %rbp
jl 0x4eb0
jmp 0x50bd
movq (%rsi,%r12,8), %rsi
movb %r8b, 0x8(%rsi,%r15)
jmp 0x4ef5
incq %r12
incq %rbx
movq 0x30(%rsp), %rax
cmpq %rax, %r12
jne 0x4e9f
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7123(%rip), %rdi      # 0xc20a
leaq 0x703b(%rip), %rsi      # 0xc129
leaq 0x70bf(%rip), %rcx      # 0xc1b4
movl $0x11b, %edx            # imm = 0x11B
callq 0x3190
leaq 0x712f(%rip), %rdi      # 0xc235
leaq 0x701c(%rip), %rsi      # 0xc129
leaq 0x70a0(%rip), %rcx      # 0xc1b4
movl $0x11c, %edx            # imm = 0x11C
callq 0x3190
leaq 0x70b6(%rip), %rdi      # 0xc1db
leaq 0x6ffd(%rip), %rsi      # 0xc129
leaq 0x7081(%rip), %rcx      # 0xc1b4
movl $0x11a, %edx            # imm = 0x11A
callq 0x3190
leaq 0x7128(%rip), %rdi      # 0xc26c
leaq 0x6fde(%rip), %rsi      # 0xc129
leaq 0x7062(%rip), %rcx      # 0xc1b4
movl $0x11d, %edx            # imm = 0x11D
callq 0x3190
leaq 0x701b(%rip), %rdi      # 0xc17e
leaq 0x6fbf(%rip), %rsi      # 0xc129
leaq 0x7043(%rip), %rcx      # 0xc1b4
movl $0xca, %edx
callq 0x3190
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x51b5
movq 0x48(%rsp), %rsi
jmp 0x51a6
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r13, %rdi
je 0x51b5
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x51b5
jmp 0x51b2
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x51d1
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x3480
nop