// File: readmapping[P]alfalfa/src/ksw.c
// Function: ksw_qinit
kswq_t *ksw_qinit(int size, int qlen, const uint8_t *query, int m, const int8_t *mat)
{
	kswq_t *q;
	int slen, a, tmp, p;

	size = size > 1? 2 : 1;
	p = 8 * (3 - size); // # values per __m128i
	slen = (qlen + p - 1) / p; // segmented length
	q = (kswq_t*)malloc(sizeof(kswq_t) + 256 + 16 * slen * (m + 4)); // a single block of memory
	q->qp = (__m128i*)(((size_t)q + sizeof(kswq_t) + 15) >> 4 << 4); // align memory
	q->H0 = q->qp + slen * m;
	q->H1 = q->H0 + slen;
	q->E  = q->H1 + slen;
	q->Hmax = q->E + slen;
	q->slen = slen; q->qlen = qlen; q->size = size;
	// compute shift
	tmp = m * m;
	for (a = 0, q->shift = 127, q->mdiff = 0; a < tmp; ++a) { // find the minimum and maximum score
		if (mat[a] < (int8_t)q->shift) q->shift = mat[a];
		if (mat[a] > (int8_t)q->mdiff) q->mdiff = mat[a];
	}
	q->max = q->mdiff;
	q->shift = 256 - q->shift; // NB: q->shift is uint8_t
	q->mdiff += q->shift; // this is the difference between the min and max scores
	// An example: p=8, qlen=19, slen=3 and segmentation:
	//  {{0,3,6,9,12,15,18,-1},{1,4,7,10,13,16,-1,-1},{2,5,8,11,14,17,-1,-1}}
	if (size == 1) {
		int8_t *t = (int8_t*)q->qp;
		for (a = 0; a < m; ++a) {
			int i, k, nlen = slen * p;
			const int8_t *ma = mat + a * m;
			for (i = 0; i < slen; ++i)
				for (k = i; k < nlen; k += slen) // p iterations
					*t++ = (k >= qlen? 0 : ma[query[k]]) + q->shift;
		}
	} else {
		int16_t *t = (int16_t*)q->qp;
		for (a = 0; a < m; ++a) {
			int i, k, nlen = slen * p;
			const int8_t *ma = mat + a * m;
			for (i = 0; i < slen; ++i)
				for (k = i; k < nlen; k += slen) // p iterations
					*t++ = (k >= qlen? 0 : ma[query[k]]);
		}
	}
	return q;
}

// Assembly:
subq $0x88, %rsp
movl %edi, 0x84(%rsp)
movl %esi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movl %ecx, 0x74(%rsp)
movq %r8, 0x68(%rsp)
movl 0x84(%rsp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x1, %edx
cmovgl %ecx, %eax
movl %eax, 0x84(%rsp)
movl $0x3, %eax
subl 0x84(%rsp), %eax
shll $0x3, %eax
movl %eax, 0x50(%rsp)
movl 0x80(%rsp), %eax
addl 0x50(%rsp), %eax
subl $0x1, %eax
cltd
idivl 0x50(%rsp)
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
shll $0x4, %eax
movl 0x74(%rsp), %ecx
addl $0x4, %ecx
imull %ecx, %eax
movslq %eax, %rdi
addq $0x138, %rdi            # imm = 0x138
callq 0x8560
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rcx
addq $0x38, %rcx
addq $0xf, %rcx
shrq $0x4, %rcx
shlq $0x4, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x5c(%rsp), %eax
imull 0x74(%rsp), %eax
cltq
shlq $0x4, %rax
addq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rcx
movslq 0x5c(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x20(%rax)
movq 0x60(%rsp), %rax
movq 0x20(%rax), %rcx
movslq 0x5c(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x28(%rax)
movq 0x60(%rsp), %rax
movq 0x28(%rax), %rcx
movslq 0x5c(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x30(%rax)
movl 0x5c(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, 0x4(%rax)
movl 0x80(%rsp), %ecx
movq 0x60(%rsp), %rax
movl %ecx, (%rax)
movl 0x84(%rsp), %eax
movb %al, %cl
movq 0x60(%rsp), %rax
movb %cl, 0xb(%rax)
movl 0x74(%rsp), %eax
imull 0x74(%rsp), %eax
movl %eax, 0x54(%rsp)
movl $0x0, 0x58(%rsp)
movq 0x60(%rsp), %rax
movb $0x7f, 0x8(%rax)
movq 0x60(%rsp), %rax
movb $0x0, 0x9(%rax)
movl 0x58(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x73c07
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x60(%rsp), %rcx
movsbl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x73bc8
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movb (%rax,%rcx), %cl
movq 0x60(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x60(%rsp), %rcx
movsbl 0x9(%rcx), %ecx
cmpl %ecx, %eax
jle 0x73bf8
movq 0x68(%rsp), %rax
movslq 0x58(%rsp), %rcx
movb (%rax,%rcx), %cl
movq 0x60(%rsp), %rax
movb %cl, 0x9(%rax)
jmp 0x73bfa
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x73b8e
movq 0x60(%rsp), %rax
movb 0x9(%rax), %cl
movq 0x60(%rsp), %rax
movb %cl, 0xa(%rax)
movq 0x60(%rsp), %rax
movzbl 0x8(%rax), %ecx
movl $0x100, %eax            # imm = 0x100
subl %ecx, %eax
movb %al, %cl
movq 0x60(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x60(%rsp), %rax
movzbl 0x8(%rax), %edx
movq 0x60(%rsp), %rax
movzbl 0x9(%rax), %ecx
addl %edx, %ecx
movb %cl, 0x9(%rax)
cmpl $0x1, 0x84(%rsp)
jne 0x73d53
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x73d4e
movl 0x5c(%rsp), %eax
imull 0x50(%rsp), %eax
movl %eax, 0x3c(%rsp)
movq 0x68(%rsp), %rax
movl 0x58(%rsp), %ecx
imull 0x74(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x73d3c
movl 0x44(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x73d2a
movl 0x40(%rsp), %eax
cmpl 0x80(%rsp), %eax
jl 0x73cdd
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x73cf8
movq 0x30(%rsp), %rax
movq 0x78(%rsp), %rcx
movslq 0x40(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movq 0x60(%rsp), %rcx
movzbl 0x8(%rcx), %ecx
addl %ecx, %eax
movb %al, %cl
movq 0x48(%rsp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, 0x48(%rsp)
movb %cl, (%rax)
movl 0x5c(%rsp), %eax
addl 0x40(%rsp), %eax
movl %eax, 0x40(%rsp)
jmp 0x73cbe
jmp 0x73d2c
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x73ca8
jmp 0x73d3e
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x73c6c
jmp 0x73e40
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x73e3e
movl 0x5c(%rsp), %eax
imull 0x50(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x68(%rsp), %rax
movl 0x58(%rsp), %ecx
imull 0x74(%rsp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x73e2c
movl 0x24(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x73e1a
movl 0x20(%rsp), %eax
cmpl 0x80(%rsp), %eax
jl 0x73dd6
xorl %eax, %eax
movl %eax, 0x8(%rsp)
jmp 0x73df1
movq 0x10(%rsp), %rax
movq 0x78(%rsp), %rcx
movslq 0x20(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movw %ax, %cx
movq 0x28(%rsp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, 0x28(%rsp)
movw %cx, (%rax)
movl 0x5c(%rsp), %eax
addl 0x20(%rsp), %eax
movl %eax, 0x20(%rsp)
jmp 0x73db7
jmp 0x73e1c
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x73da5
jmp 0x73e2e
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x73d69
jmp 0x73e40
movq 0x60(%rsp), %rax
addq $0x88, %rsp
retq
nopl (%rax)