// File: BenLangmead[P]bowtie/./diff_sample.h
// Function: DifferenceCoverSample<SString<char>>::buildSPrime(EList<unsigned int, 128>&, unsigned long)
void DifferenceCoverSample<TStr>::buildSPrime(
	EList<TIndexOffU>& sPrime,
	size_t padding)
{
	const TStr& t = this->text();
	const EList<uint32_t>& ds = this->ds();
	TIndexOffU tlen = (TIndexOffU)t.length();
	uint32_t v = this->v();
	uint32_t d = this->d();
	assert_gt(v, 2);
	assert_lt(d, v);
	// Record where each d section should begin in sPrime
	TIndexOffU tlenDivV = this->divv(tlen);
	uint32_t tlenModV = this->modv(tlen);
	TIndexOffU sPrimeSz = 0;
	assert(_doffs.empty());
	_doffs.resize(d+1);
	for(uint32_t di = 0; di < d; di++) {
		// mu mapping
		TIndexOffU sz = tlenDivV + ((ds[di] <= tlenModV) ? 1 : 0);
		assert_geq(sz, 0);
		_doffs[di] = sPrimeSz;
		sPrimeSz += sz;
	}
	_doffs[d] = sPrimeSz;
#ifndef NDEBUG
	if(tlenDivV > 0) {
		for(size_t i = 0; i < d; i++) {
			assert_gt(_doffs[i+1], _doffs[i]);
			TIndexOffU diff = _doffs[i+1] - _doffs[i];
			assert(diff == tlenDivV || diff == tlenDivV+1);
		}
	}
#endif
	assert_eq(_doffs.size(), d+1);
	// Size sPrime appropriately
	sPrime.resizeExact((size_t)sPrimeSz + padding);
	sPrime.fill(OFF_MASK);
	// Slot suffixes from text into sPrime according to the mu
	// mapping; where the mapping would leave a blank, insert a 0
	TIndexOffU added = 0;
	TIndexOffU i = 0;
	for(uint64_t ti = 0; ti <= tlen; ti += v) {
		for(uint32_t di = 0; di < d; di++) {
			TIndexOffU tti = (TIndexOffU)(ti + ds[di]);
			if(tti > tlen) break;
			TIndexOffU spi = _doffs[di] + i;
			assert_lt(spi, _doffs[di+1]);
			assert_leq(tti, tlen);
			assert_lt(spi, sPrimeSz);
			assert_eq(OFF_MASK, sPrime[spi]);
			sPrime[spi] = tti; added++;
		}
		i++;
	}
	assert_eq(added, sPrimeSz);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x78(%rbp)
callq 0x2a550
movq -0x78(%rbp), %rdi
movq %rax, -0x20(%rbp)
callq 0x2b1e0
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x14c10
movq -0x78(%rbp), %rdi
movl %eax, -0x2c(%rbp)
callq 0x2a570
movq -0x78(%rbp), %rdi
movl %eax, -0x30(%rbp)
callq 0x2b200
movq -0x78(%rbp), %rdi
movl %eax, -0x34(%rbp)
movl -0x2c(%rbp), %esi
callq 0x2b220
movq -0x78(%rbp), %rdi
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %esi
callq 0x2b240
movq -0x78(%rbp), %rdi
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
addq $0x58, %rdi
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x25d50
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x2a697
movl -0x38(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x28(%rbp), %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x19d10
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl -0x80(%rbp), %eax
movl (%rcx), %esi
movl -0x3c(%rbp), %r8d
xorl %ecx, %ecx
movl $0x1, %edx
cmpl %r8d, %esi
cmovbel %edx, %ecx
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x7c(%rbp)
addq $0x58, %rdi
movl -0x44(%rbp), %eax
movl %eax, %esi
callq 0x14410
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x48(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2a62c
movq -0x78(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, -0x84(%rbp)
addq $0x58, %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
callq 0x14410
movl -0x84(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %esi
addq -0x18(%rbp), %rsi
callq 0x2a3c0
movq -0x10(%rbp), %rdi
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
leaq -0x4c(%rbp), %rsi
callq 0x2a440
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
cmpq %rcx, %rax
ja 0x2a7b7
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x2a79e
movq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x28(%rbp), %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x19d10
movq %rax, %rcx
movq -0x90(%rbp), %rax
movl (%rcx), %ecx
addq %rcx, %rax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jbe 0x2a74e
jmp 0x2a79e
movq -0x78(%rbp), %rdi
addq $0x58, %rdi
movl -0x64(%rbp), %eax
movl %eax, %esi
callq 0x14410
movl (%rax), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x10(%rbp), %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x14410
movl -0x94(%rbp), %ecx
movl %ecx, (%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2a70d
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movl -0x30(%rbp), %eax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x2a6f6
addq $0xa0, %rsp
popq %rbp
retq