// File: /workspace/github_cpp_makefile_star2_O2/BenLangmead[P]bowtie/./zbox.h
// Function: void calcZ<S2bDnaString>(S2bDnaString const&, unsigned int, EList<unsigned int, 128>&, bool, bool)
void calcZ(const T& s,
		TIndexOffU off,
	   EList<TIndexOffU>& z,
           bool verbose = false,
           bool sanityCheck = false)
{
	size_t lCur = 0, rCur = 0;
	size_t zlen = z.size();
	size_t slen = s.length();
	assert_gt(zlen, 0);
	assert_eq(z[0], 0);
	//assert_leq(zlen, slen);
	for (size_t k = 1; k < zlen && k+off < slen; k++) {
		assert_lt(lCur, k);
		assert(z[lCur] == 0 || z[lCur] == rCur - lCur + 1);
		if(k > rCur) {
			// compare starting at k with prefix starting at 0
			size_t ki = k;
			while(off+ki < s.length() && s[off+ki] == s[off+ki-k]) ki++;
			z[k] = (TIndexOffU)(ki - k);
			assert_lt(off+z[k], slen);
			if(z[k] > 0) {
				lCur = k;
				rCur = k + z[k] - 1;
			}
		} else {
			// position k is contained in a Z-box
			size_t betaLen = rCur - k + 1;
			size_t kPrime = k - lCur;
			assert_eq(s[off+k], s[off+kPrime]);
			if(z[kPrime] < betaLen) {
				z[k] = z[kPrime];
				assert_lt(off+z[k], slen);
				// lCur, rCur unchanged
			} else if (z[kPrime] > 0) {
				int q = 0;
				while (off+q+rCur+1 < s.length() && s[off+q+rCur+1] == s[off+betaLen+q]) q++;
				z[k] = (TIndexOffU)(betaLen + q);
				assert_lt(off+z[k], slen);
				rCur = rCur + q;
				assert_geq(k, lCur);
				lCur = k;
			} else {
				z[k] = 0;
				assert_lt(off+z[k], slen);
				// lCur, rCur unchanged
			}
		}
	}
#ifndef NDEBUG
	if(sanityCheck) {
		// Recalculate Z-boxes using naive quadratic-time algorithm and
		// compare to linear-time result
		assert_eq(0, z[0]);
		for(size_t i = 1; i < z.size(); i++) {
			size_t j;
			for(j = i; off+j < s.length(); j++) {
				if(s[off+j] != s[off+j-i]) break;
			}
			assert_eq(j-i, z[i]);
		}
	}
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, -0x20(%rsp)
movq 0x18(%rdx), %rax
movq %rax, -0x8(%rsp)
movq %rdi, -0x28(%rsp)
movq 0x18(%rdi), %rax
movq %rax, -0x10(%rsp)
movl %esi, %r9d
addl %esi, %esi
movl %esi, -0x2c(%rsp)
movabsq $0x1, %r10
xorl %edi, %edi
movl $0x2, %ebx
movq %r9, %r14
xorl %r15d, %r15d
xorl %edx, %edx
cmpq -0x8(%rsp), %r10
jae 0x220bf
leaq (%r10,%r9), %rax
cmpq -0x10(%rsp), %rax
jae 0x220bf
movq %r15, %rax
subq %r10, %rax
jae 0x21fda
movq -0x28(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rax), %r8
xorl %eax, %eax
movq %r9, %r11
movl -0x2c(%rsp), %ecx
movl %ecx, %r13d
leaq (%r10,%r11), %rcx
cmpq %r8, %rcx
jae 0x22000
shrq $0x4, %rcx
movl (%rsi,%rcx,4), %ebp
leal (%rbx,%r13), %ecx
andb $0x1e, %cl
shrl %cl, %ebp
movq %r11, %rcx
shrq $0x4, %rcx
movl (%rsi,%rcx,4), %r12d
movl %r13d, %ecx
andb $0x1e, %cl
shrl %cl, %r12d
xorl %ebp, %r12d
testb $0x3, %r12b
jne 0x22000
incl %eax
addl $0x2, %r13d
incq %r11
jmp 0x21f99
incq %rax
movq %r10, %rcx
subq %rdx, %rcx
movq -0x20(%rsp), %rsi
movq 0x8(%rsi), %r13
movl (%r13,%rcx,4), %ecx
cmpq %rcx, %rax
jbe 0x22027
movl %ecx, (%r13,%r10,4)
jmp 0x220a7
movq -0x20(%rsp), %rsi
movq 0x8(%rsi), %rcx
movl %eax, (%rcx,%r10,4)
movq 0x8(%rsi), %rax
movl (%rax,%r10,4), %eax
testq %rax, %rax
je 0x220a7
leaq (%r10,%rax), %r15
decq %r15
jmp 0x220a4
testl %ecx, %ecx
je 0x220b7
movq %rdi, -0x18(%rsp)
leaq 0x1(%r15), %r12
movq -0x28(%rsp), %rax
movq 0x8(%rax), %rbp
movq 0x18(%rax), %r8
leal (%r14,%r15), %eax
addl %eax, %eax
leaq (%r15,%r14), %r11
xorl %esi, %esi
leal (%r9,%rsi), %ecx
addq %r12, %rcx
cmpq %r8, %rcx
jae 0x2208f
movq %rcx, %rdx
shrq $0x4, %rdx
movl (%rbp,%rdx,4), %edx
addl %ecx, %ecx
shrl %cl, %edx
leaq (%r11,%rsi), %rcx
shrq $0x4, %rcx
movl (%rbp,%rcx,4), %edi
movl %eax, %ecx
andb $0x1e, %cl
shrl %cl, %edi
xorl %edx, %edi
testb $0x3, %dil
jne 0x2208f
incq %rsi
addl $0x2, %eax
jmp 0x22051
movl %esi, %eax
movq -0x18(%rsp), %rdi
leal (%rdi,%r15), %ecx
addl %esi, %ecx
movl %ecx, (%r13,%r10,4)
addq %rax, %r15
movq %r10, %rdx
incq %r10
decq %r14
decl %edi
addl $0x2, %ebx
jmp 0x21f5e
andl $0x0, (%r13,%r10,4)
jmp 0x220a7
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq