// File: CMU-SAFARI[P]optimal-seed-solver/optimalSolver.cc
// Function: OptimalSolver::init(int, int)
void OptimalSolver::init(int readLength, int seedNum) {
	//Initialize the matrix
	if (matrix != NULL) {
		delete [] matrix;
		matrix = NULL;
	}

	this->readLength = readLength;

	if (readLength >= seedNum * tree.getHashLength() )
		this->seedNum = seedNum;
	else
		this->seedNum = readLength / tree.getHashLength();

	//Position count for the level
	int lvPosCount;

	//Matrix overall size
	int matrixSize = 0;

	//For the first 1 seed case, we need to consider all cases
	lvPosCount = readLength + 1 - tree.getHashLength();
	matrixSize += (lvPosCount + 1) * lvPosCount / 2;

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

	//For the last seed, it's simpler all cases for dividers
	lvPosCount = readLength + 1 - this->seedNum * tree.getHashLength();
	matrixSize += lvPosCount;

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

	//Progress counter
	int matrixProgress = 0;
	//sublevel counter
	int levelIdx = 0;
	//Fill the level pointers
	level.resize(this->seedNum);
	//Find the first row
	lvPosCount = readLength + 1 - tree.getHashLength();
	level[0].resize(lvPosCount);
	for (int i = lvPosCount; i > 0; i--) {
		level[0][levelIdx] = matrix + matrixProgress;
		matrixProgress += i;
		levelIdx++;
	}

	//Find the reminding levels
	for (int idx = 1; idx < this->seedNum - 1; idx++) {
		lvPosCount = readLength + 1 - tree.getHashLength() * (idx + 2);
		level[idx].resize(lvPosCount);
		//reset the sublevel counter
		levelIdx = 0;
		for (int i = lvPosCount; i > 0; i--) {
			level[idx][levelIdx] = matrix + matrixProgress;
			matrixProgress += i;
			levelIdx++;
		}
	}

	//Set the last level
	level[this->seedNum - 1].resize(1);
	level[this->seedNum - 1][0] = matrix + matrixProgress;

	//Just to check the matrixSize
	assert (matrixProgress + readLength + 1 - this->seedNum * tree.getHashLength() == matrixSize);
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movl %esi, 0xfc(%rsp)
movl %edx, 0xf8(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0x368(%rax)
je 0x6284
movq 0xc0(%rsp), %rax
movq 0x368(%rax), %rax
movq %rax, 0xb8(%rsp)
cmpq $0x0, %rax
je 0x6271
movq 0xb8(%rsp), %rdi
callq 0x3410
movq 0xc0(%rsp), %rax
movq $0x0, 0x368(%rax)
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
movl %eax, 0x35c(%rdi)
movl 0xfc(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xf8(%rsp), %eax
movl %eax, 0xb0(%rsp)
callq 0xfad0
movl 0xb0(%rsp), %ecx
movl %eax, %edx
movl 0xb4(%rsp), %eax
imull %edx, %ecx
cmpl %ecx, %eax
jb 0x62e8
movq 0xc0(%rsp), %rax
movl 0xf8(%rsp), %ecx
movl %ecx, 0x360(%rax)
jmp 0x6320
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
movl %eax, 0xac(%rsp)
callq 0xfad0
movl %eax, %ecx
movl 0xac(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq 0xc0(%rsp), %rax
movl %ecx, 0x360(%rax)
movq 0xc0(%rsp), %rdi
movl $0x0, 0xf0(%rsp)
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
callq 0xfad0
movl %eax, %ecx
movl 0xa8(%rsp), %eax
subl %ecx, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
imull 0xf4(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl 0xf0(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl $0x1, 0xec(%rsp)
movq 0xc0(%rsp), %rcx
movl 0xec(%rsp), %eax
movl 0x360(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x6425
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
callq 0xfad0
movl %eax, %ecx
movl 0xa4(%rsp), %eax
movl 0xec(%rsp), %edx
addl $0x2, %edx
imull %edx, %ecx
subl %ecx, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addl $0x1, %eax
imull 0xf4(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl 0xf0(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x638e
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
movl 0x360(%rdi), %eax
movl %eax, 0x80(%rsp)
callq 0xfad0
movl 0x80(%rsp), %ecx
movl %eax, %edx
movl 0x84(%rsp), %eax
imull %edx, %ecx
subl %ecx, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addl 0xf0(%rsp), %eax
movl %eax, 0xf0(%rsp)
movslq 0xf0(%rsp), %rax
movq %rax, 0x90(%rsp)
movl $0x28, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x88(%rsp)
callq 0x3030
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x98(%rsp)
cmpq $0x0, %rax
je 0x6520
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0xc110
jmp 0x6508
movq 0x70(%rsp), %rcx
movq 0x68(%rsp), %rax
addq $0x28, %rax
cmpq %rcx, %rax
movq %rax, 0x78(%rsp)
jne 0x64f7
movq 0xc0(%rsp), %rax
movq 0x98(%rsp), %rcx
movq %rcx, 0x368(%rax)
movslq 0xf0(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0x28, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x50(%rsp)
callq 0x3030
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq %rcx, 0x60(%rsp)
cmpq $0x0, %rax
je 0x65c4
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xc110
jmp 0x65ac
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rax
addq $0x28, %rax
cmpq %rcx, %rax
movq %rax, 0x48(%rsp)
jne 0x659b
movq 0xc0(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0x388(%rax)
movl $0x0, 0xd8(%rsp)
movl $0x0, 0xd4(%rsp)
movq %rax, %rdi
addq $0x370, %rdi            # imm = 0x370
movslq 0x360(%rax), %rsi
callq 0xc160
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
callq 0xfad0
movq 0xc0(%rsp), %rdi
movl %eax, %ecx
movl 0x34(%rsp), %eax
subl %ecx, %eax
movl %eax, 0xf4(%rsp)
addq $0x370, %rdi            # imm = 0x370
xorl %eax, %eax
movl %eax, %esi
callq 0xc200
movq %rax, %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc220
movl 0xf4(%rsp), %eax
movl %eax, 0xd0(%rsp)
cmpl $0x0, 0xd0(%rsp)
jle 0x674f
movq 0xc0(%rsp), %rdi
movq 0x368(%rdi), %rax
movslq 0xd8(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
addq $0x370, %rdi            # imm = 0x370
xorl %eax, %eax
movl %eax, %esi
callq 0xc200
movq %rax, %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc2c0
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movl 0xd0(%rsp), %eax
addl 0xd8(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
movl 0xd0(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x6664
movq 0x88(%rsp), %rsi
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
callq 0x3620
jmp 0x6996
movq 0x50(%rsp), %rsi
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
callq 0x3620
jmp 0x6996
movl $0x1, 0xcc(%rsp)
movq 0xc0(%rsp), %rcx
movl 0xcc(%rsp), %eax
movl 0x360(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x68a7
movq 0xc0(%rsp), %rdi
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
callq 0xfad0
movq 0xc0(%rsp), %rdi
movl %eax, %ecx
movl 0x24(%rsp), %eax
movl 0xcc(%rsp), %edx
addl $0x2, %edx
imull %edx, %ecx
subl %ecx, %eax
movl %eax, 0xf4(%rsp)
addq $0x370, %rdi            # imm = 0x370
movslq 0xcc(%rsp), %rsi
callq 0xc200
movq %rax, %rdi
movslq 0xf4(%rsp), %rsi
callq 0xc220
movl $0x0, 0xd4(%rsp)
movl 0xf4(%rsp), %eax
movl %eax, 0xc8(%rsp)
cmpl $0x0, 0xc8(%rsp)
jle 0x688f
movq 0xc0(%rsp), %rdi
movq 0x368(%rdi), %rax
movslq 0xd8(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
addq $0x370, %rdi            # imm = 0x370
movslq 0xcc(%rsp), %rsi
callq 0xc200
movq %rax, %rdi
movslq 0xd4(%rsp), %rsi
callq 0xc2c0
movq 0x18(%rsp), %rcx
movq %rcx, (%rax)
movl 0xc8(%rsp), %eax
addl 0xd8(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
movl 0xc8(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0x67f6
jmp 0x6891
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x675a
movq 0xc0(%rsp), %rax
movq %rax, %rdi
addq $0x370, %rdi            # imm = 0x370
movl 0x360(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xc200
movq %rax, %rdi
movl $0x1, %esi
callq 0xc220
movq 0xc0(%rsp), %rax
movq 0x368(%rax), %rcx
movslq 0xd8(%rsp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq %rcx, 0x8(%rsp)
movq %rax, %rdi
addq $0x370, %rdi            # imm = 0x370
movl 0x360(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0xc200
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xc2c0
movq 0x8(%rsp), %rcx
movq 0xc0(%rsp), %rdi
movq %rcx, (%rax)
movl 0xd8(%rsp), %eax
addl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movl 0x360(%rdi), %eax
movl %eax, 0x10(%rsp)
callq 0xfad0
movl 0x10(%rsp), %ecx
movl %eax, %edx
movl 0x14(%rsp), %eax
imull %edx, %ecx
subl %ecx, %eax
cmpl 0xf0(%rsp), %eax
jne 0x696f
jmp 0x698e
leaq 0x1374e(%rip), %rdi     # 0x1a0c4
leaq 0x1379c(%rip), %rsi     # 0x1a119
movl $0x71, %edx
leaq 0x137a1(%rip), %rcx     # 0x1a12a
callq 0x31e0
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xe0(%rsp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)