// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]optimal-seed-solver/optimalSolverLN.cc
// Function: OptimalSolverLN::init(int, int)
void OptimalSolverLN::init(int readLength, int seedNum) {
	//Initialize the matrix
	if  (matrix != NULL) {
		delete [] matrix;
		matrix = NULL;
	}
	if  (base != NULL) {
		delete [] base;
		base = NULL;
	}
	if  (defaultMatrix != NULL) {
		delete [] defaultMatrix;
		defaultMatrix = NULL;
	}
	if  (defaultBase != NULL) {
		delete [] defaultBase;
		defaultBase = NULL;
	}

	this->readLength = readLength;

	if (readLength >= seedNum * minLength)
		this->seedNum = seedNum;
	else
		this->seedNum = readLength / minLength;

	//Position count for the level
	int lvPosCount;

	//Matrix and base overall size
	matrixSize = 0;
	baseSize = 0;

	//For the base case, we need to consider all possible seeds
	lvPosCount = readLength + 1 - minLength;
	baseSize = (lvPosCount + 1) * lvPosCount / 2;

	//For the matrix, we need to consider all cases, while leaving
	//the last minLength out (because we don't need to)
	if (seedNum > 1)
		matrixSize = (readLength + 1 - minLength * seedNum) * (seedNum - 1);
	else
		matrixSize = readLength + 1 - minLength;
	/*
	for (int i = 0; i < this->seedNum - 1; i++) {
		//Notice that for the rest levels, we do not need to consider the last
		//hashLength positions
		lvPosCount = readLength + 1 - minLength * (i + 2);
		matrixSize += lvPosCount;
	}
	*/

#ifdef DEBUG
	cout << "matrixSize: " << matrixSize << endl;
#endif
	//Allocate space for the base
	base = new Cell[baseSize];
	defaultBase = new Cell[baseSize];

	//Allocate space for the matrix
	matrix = new Cell[matrixSize];
	defaultMatrix = new Cell[matrixSize];

	//Fill the level0 pointers
	lvPosCount = readLength + 1 - minLength;
	level0.resize(lvPosCount);
	int levelIdx = 0;
	int baseProgress = 0;
	//Initialize the first one
	for (int i = lvPosCount; i > 0; i--) {
		level0[levelIdx] = base + baseProgress;
		baseProgress += i;
		levelIdx++;
	}

	//Just to check the baseSize
	assert (baseProgress == baseSize);

	if (seedNum > 1) {
		//Fill the level pointers. No need to have the last level
		level.resize(seedNum - 1);
		levelIdx = 0;
		int matrixProgress = 0;
		for (int i = 0; i < seedNum - 1; i++) {
			lvPosCount = readLength + 1 - minLength * seedNum;
			level[i] = matrix + matrixProgress;
			matrixProgress += lvPosCount;
		}

		//Just to check the matrixSize
		assert (matrixProgress == matrixSize);
	}
	else {
		level.resize(1);
		level[0] = matrix;
	}


	//Initialize all the seeds
	seeds.resize(seedNum);

	//Initialize statistics
	processedReads = 0;
	divTravel.resize(readLength, 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r12d
movl %esi, %r14d
movq %rdi, %rbx
movq 0x370(%rdi), %rdi
testq %rdi, %rdi
je 0x440e
callq 0x3300
andq $0x0, 0x370(%rbx)
movq 0x378(%rbx), %rdi
testq %rdi, %rdi
je 0x4427
callq 0x3300
andq $0x0, 0x378(%rbx)
movq 0x3b0(%rbx), %rdi
testq %rdi, %rdi
je 0x4440
callq 0x3300
andq $0x0, 0x3b0(%rbx)
movq 0x3b8(%rbx), %rdi
testq %rdi, %rdi
je 0x4459
callq 0x3300
andq $0x0, 0x3b8(%rbx)
movl %r14d, 0x360(%rbx)
movl 0x368(%rbx), %esi
movl %esi, %ecx
imull %r12d, %ecx
movl %r12d, %eax
cmpl %r14d, %ecx
jle 0x447a
movl %r14d, %eax
cltd
idivl %esi
movl %eax, 0x364(%rbx)
movq %r14, 0x10(%rsp)
incl %r14d
movl %r14d, %r15d
subl %esi, %r15d
leal 0x1(%r15), %ebp
imull %r15d, %ebp
pushq $0x2
popq %rsi
movl %ebp, %eax
cltd
idivl %esi
movl %eax, 0x3c4(%rbx)
movl %r14d, 0xc(%rsp)
subl %ecx, %r14d
leal -0x1(%r12), %ecx
movl %ecx, 0x8(%rsp)
imull %ecx, %r14d
movq %r12, 0x18(%rsp)
cmpl $0x2, %r12d
cmovll %r15d, %r14d
movl %r14d, 0x3c0(%rbx)
movslq %eax, %r12
pushq $0x28
popq %rcx
movq %r12, %rax
mulq %rcx
movq %rax, %r13
pushq $-0x1
popq %rax
cmovoq %rax, %r13
movq %r13, %rdi
callq 0x3030
incl %ebp
cmpl $0x3, %ebp
jb 0x451e
imulq $0x28, %r12, %rdx
movq %rax, %rcx
addq %rdx, %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
orq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x4503
movq %rax, 0x378(%rbx)
movq %r13, %rdi
callq 0x3030
movq %rax, %rcx
cmpl $0x3, %ebp
jb 0x4561
imulq $0x28, %r12, %rdx
movq %rcx, %rax
addq %rdx, %rax
pcmpeqd %xmm0, %xmm0
movq %rcx, %rdx
orq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rax, %rdx
jne 0x4546
movslq %r14d, %r12
movq %r12, %rax
pushq $0x28
popq %rdx
mulq %rdx
movq %rax, %r13
movq %rcx, 0x3b8(%rbx)
pushq $-0x1
popq %rax
cmovoq %rax, %r13
movq %r13, %rdi
callq 0x3030
testl %r14d, %r14d
je 0x45b7
imulq $0x28, %r12, %rdx
movq %rax, %rcx
addq %rdx, %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
orq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x459c
movq %rax, 0x370(%rbx)
movq %r13, %rdi
callq 0x3030
testl %r14d, %r14d
je 0x45f7
imulq $0x28, %r12, %rdx
movq %rax, %rcx
addq %rdx, %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
orq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x45dc
movq %rax, 0x3b0(%rbx)
leaq 0x398(%rbx), %rdi
movslq %r15d, %rsi
callq 0x58c4
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %r13
testl %r15d, %r15d
jle 0x463f
movl %ecx, %edx
imulq $0x28, %rdx, %rdx
addq 0x378(%rbx), %rdx
movq 0x398(%rbx), %rsi
movq %rdx, (%rsi,%rax)
addl %r15d, %ecx
decl %r15d
addq $0x8, %rax
jmp 0x4616
cmpl 0x3c4(%rbx), %ecx
jne 0x4708
leaq 0x380(%rbx), %rdi
cmpl $0x2, %r13d
movq 0x10(%rsp), %r14
jl 0x46a5
movl 0x8(%rsp), %r12d
movq %r12, %rsi
callq 0x58c4
movl 0x368(%rbx), %eax
imull %r13d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%rsp), %r8d
cmpq %rcx, %r12
je 0x46c0
movslq %edx, %rdx
imulq $0x28, %rdx, %rsi
addq 0x370(%rbx), %rsi
movq 0x380(%rbx), %rdi
movq %rsi, (%rdi,%rcx,8)
addl %r8d, %edx
subl %eax, %edx
incq %rcx
jmp 0x467d
pushq $0x1
popq %rsi
callq 0x58c4
movq 0x370(%rbx), %rax
movq 0x380(%rbx), %rcx
movq %rax, (%rcx)
jmp 0x46c8
cmpl %edx, 0x3c0(%rbx)
jne 0x4727
leaq 0x3c8(%rbx), %rdi
movslq %r13d, %rsi
callq 0x58f6
andl $0x0, 0x400(%rbx)
addq $0x3e8, %rbx            # imm = 0x3E8
movslq %r14d, %rsi
leaq 0x20(%rsp), %rdx
andq $0x0, (%rdx)
movq %rbx, %rdi
callq 0x592e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x69e1(%rip), %rdi      # 0xb0f0
leaq 0x69f3(%rip), %rsi      # 0xb109
leaq 0x69ff(%rip), %rcx      # 0xb11c
movl $0x84, %edx
callq 0x3170
leaq 0x6a13(%rip), %rdi      # 0xb141
leaq 0x69d4(%rip), %rsi      # 0xb109
leaq 0x69e0(%rip), %rcx      # 0xb11c
movl $0x92, %edx
callq 0x3170