// File: /workspace/github_cpp_makefile_star2_O3/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 0x4861
callq 0x3340
movq $0x0, 0x370(%rbx)
movq 0x378(%rbx), %rdi
testq %rdi, %rdi
je 0x487d
callq 0x3340
movq $0x0, 0x378(%rbx)
movq 0x3b0(%rbx), %rdi
testq %rdi, %rdi
je 0x4899
callq 0x3340
movq $0x0, 0x3b0(%rbx)
movq 0x3b8(%rbx), %rdi
testq %rdi, %rdi
je 0x48b5
callq 0x3340
movq $0x0, 0x3b8(%rbx)
movl %r14d, 0x360(%rbx)
movl 0x368(%rbx), %esi
movl %esi, %ecx
imull %r12d, %ecx
movl %r12d, %eax
cmpl %r14d, %ecx
jle 0x48d6
movl %r14d, %eax
cltd
idivl %esi
movl %eax, 0x364(%rbx)
movq %r14, 0x18(%rsp)
leal 0x1(%r14), %r15d
movl %r15d, %r14d
subl %esi, %r14d
leal 0x1(%r14), %r13d
imull %r14d, %r13d
movl %r13d, %eax
shrl $0x1f, %eax
addl %r13d, %eax
sarl %eax
movl %eax, 0x3c4(%rbx)
movl %r15d, %ebp
subl %ecx, %ebp
leal -0x1(%r12), %ecx
movl %ecx, 0xc(%rsp)
imull %ecx, %ebp
movq %r12, (%rsp)
cmpl $0x2, %r12d
cmovll %r14d, %ebp
movl %ebp, 0x3c0(%rbx)
cltq
movl $0x28, %ecx
movq %rax, 0x10(%rsp)
mulq %rcx
movq $-0x1, %r12
cmovnoq %rax, %r12
movq %r12, %rdi
callq 0x3030
incl %r13d
cmpl $0x2, %r13d
ja 0x4963
movq %rax, 0x378(%rbx)
movq %r12, %rdi
callq 0x3030
jmp 0x49cd
movq 0x10(%rsp), %rcx
leaq (%rcx,%rcx,4), %r13
leaq (%rax,%r13,8), %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
movq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x4977
movq %rax, 0x378(%rbx)
movq %r12, %rdi
callq 0x3030
leaq (%rax,%r13,8), %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
movq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x49af
movq %rax, 0x3b8(%rbx)
movslq %ebp, %r13
movl $0x28, %ecx
movq %r13, %rax
mulq %rcx
movq $-0x1, %r12
cmovnoq %rax, %r12
movq %r12, %rdi
callq 0x3030
testl %ebp, %ebp
je 0x4a61
leaq (%r13,%r13,4), %r13
leaq (%rax,%r13,8), %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
movq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x4a09
movq %rax, 0x370(%rbx)
movq %r12, %rdi
callq 0x3030
leaq (%rax,%r13,8), %rcx
pcmpeqd %xmm0, %xmm0
movq %rax, %rdx
movq $-0x1, (%rdx)
movb $0x0, 0x8(%rdx)
movdqu %xmm0, 0xc(%rdx)
movdqu %xmm0, 0x18(%rdx)
addq $0x28, %rdx
cmpq %rcx, %rdx
jne 0x4a41
jmp 0x4a70
movq %rax, 0x370(%rbx)
movq %r12, %rdi
callq 0x3030
movq (%rsp), %r12
movq %rax, 0x3b0(%rbx)
leaq 0x398(%rbx), %rdi
movslq %r14d, %rsi
callq 0x6208
testl %r14d, %r14d
jle 0x4ac7
movl %r14d, %eax
shlq $0x3, %rax
xorl %edx, %edx
xorl %ecx, %ecx
movl %ecx, %esi
leaq (%rsi,%rsi,4), %rsi
shlq $0x3, %rsi
addq 0x378(%rbx), %rsi
movq 0x398(%rbx), %rdi
movq %rsi, (%rdi,%rdx)
addl %r14d, %ecx
addq $0x8, %rdx
decl %r14d
cmpq %rdx, %rax
jne 0x4a9a
jmp 0x4ac9
xorl %ecx, %ecx
cmpl 0x3c4(%rbx), %ecx
jne 0x4bb8
leaq 0x380(%rbx), %rdi
cmpl $0x2, %r12d
jl 0x4b55
movq %r12, %r14
movl 0xc(%rsp), %r12d
movq %r12, %rsi
callq 0x6208
movl 0x368(%rbx), %eax
imull %r14d, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movslq %edx, %rdx
leaq (%rdx,%rdx,4), %rsi
shlq $0x3, %rsi
addq 0x370(%rbx), %rsi
movq 0x380(%rbx), %rdi
movq %rsi, (%rdi,%rcx,8)
addl %r15d, %edx
subl %eax, %edx
incq %rcx
cmpq %rcx, %r12
jne 0x4b00
cmpl %edx, 0x3c0(%rbx)
movq (%rsp), %r12
je 0x4b70
leaq 0x7624(%rip), %rdi      # 0xc161
leaq 0x75e5(%rip), %rsi      # 0xc129
leaq 0x75f1(%rip), %rcx      # 0xc13c
movl $0x92, %edx
callq 0x3190
movl $0x1, %esi
callq 0x6208
movq 0x370(%rbx), %rax
movq 0x380(%rbx), %rcx
movq %rax, (%rcx)
leaq 0x3c8(%rbx), %rdi
movslq %r12d, %rsi
callq 0x623a
movl $0x0, 0x400(%rbx)
addq $0x3e8, %rbx            # imm = 0x3E8
movslq 0x18(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq $0x0, (%rdx)
movq %rbx, %rdi
callq 0x627e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7551(%rip), %rdi      # 0xc110
leaq 0x7563(%rip), %rsi      # 0xc129
leaq 0x756f(%rip), %rcx      # 0xc13c
movl $0x84, %edx
callq 0x3190
nop