// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %rbx
movl %ecx, 0x8(%rsp)
movq %rdx, %r14
movl %esi, %ebp
xorl %eax, %eax
movl %edi, 0xc(%rsp)
cmpl $0x2, %edi
setge %al
leal 0x8(,%rax,8), %r12d
movl %eax, %r13d
incl %r13d
xorl $0x18, %r12d
leal (%r12,%rbp), %eax
decl %eax
cltd
idivl %r12d
movl %eax, %r15d
movl %ecx, %eax
shll $0x4, %eax
addl $0x40, %eax
imull %r15d, %eax
movslq %eax, %rdi
addq $0x138, %rdi            # imm = 0x138
leaq 0x64c6(%rip), %rsi      # 0x246c8
leaq 0x64c5(%rip), %rcx      # 0x246ce
pushq $0x48
popq %rdx
callq 0x1e045
leaq 0x47(%rax), %rcx
andq $-0x10, %rcx
movq %rcx, 0x10(%rax)
movl %r15d, %edx
movl 0x8(%rsp), %r8d
imull %r8d, %edx
movslq %edx, %rsi
shlq $0x4, %rsi
addq %rcx, %rsi
movq %rsi, 0x18(%rax)
movslq %r15d, %rdx
movq %rdx, %rdi
shlq $0x4, %rdi
addq %rdi, %rsi
movq %rsi, 0x20(%rax)
addq %rdi, %rsi
movq %rsi, 0x28(%rax)
addq %rdi, %rsi
movq %rsi, 0x30(%rax)
movl %edx, 0x4(%rax)
movq %rbp, 0x10(%rsp)
movl %ebp, (%rax)
movl %r8d, %ebp
movb %r13b, 0xb(%rax)
movl %r8d, %esi
imull %esi, %esi
movw $0x7f, 0x8(%rax)
movb $0x7f, %r8b
xorl %edi, %edi
xorl %r9d, %r9d
cmpq %r9, %rsi
je 0x1e2aa
movb (%rbx,%r9), %r11b
cmpb %r8b, %r11b
jge 0x1e296
movb %r11b, 0x8(%rax)
movb (%rbx,%r9), %r10b
movl %r11d, %r8d
jmp 0x1e299
movl %r11d, %r10d
cmpb %dil, %r10b
jle 0x1e2a5
movb %r10b, 0x9(%rax)
movl %r10d, %edi
incq %r9
jmp 0x1e27b
movl %edi, %r9d
subb %r8b, %r9b
negb %r8b
imull %r15d, %r12d
xorl %esi, %esi
testl %r15d, %r15d
cmovlel %esi, %r15d
testl %ebp, %ebp
cmovgl %ebp, %esi
movb %dil, 0xa(%rax)
movb %r8b, 0x8(%rax)
movb %r9b, 0x9(%rax)
movslq %r12d, %rdi
movslq 0x10(%rsp), %r8
movslq %ebp, %r9
cmpl $0x2, 0xc(%rsp)
jge 0x1e334
xorl %r10d, %r10d
cmpq %rsi, %r10
je 0x1e37f
movq %r10, %r11
imulq %r9, %r11
addq %rbx, %r11
xorl %r12d, %r12d
cmpq %r15, %r12
je 0x1e32f
movq %r12, %r13
cmpq %rdi, %r13
jge 0x1e32a
cmpq %r8, %r13
jge 0x1e319
movzbl (%r14,%r13), %ebp
movb (%r11,%rbp), %bpl
jmp 0x1e31b
xorl %ebp, %ebp
addb 0x8(%rax), %bpl
movb %bpl, (%rcx)
incq %rcx
addq %rdx, %r13
jmp 0x1e304
incq %r12
jmp 0x1e2fc
incq %r10
jmp 0x1e2e6
xorl %r10d, %r10d
cmpq %rsi, %r10
je 0x1e37f
movq %r10, %r11
imulq %r9, %r11
addq %rbx, %r11
xorl %r12d, %r12d
cmpq %r15, %r12
je 0x1e37a
movq %r12, %r13
cmpq %rdi, %r13
jge 0x1e375
cmpq %r8, %r13
jge 0x1e367
movzbl (%r14,%r13), %ebp
movsbl (%r11,%rbp), %ebp
jmp 0x1e369
xorl %ebp, %ebp
movw %bp, (%rcx)
addq $0x2, %rcx
addq %rdx, %r13
jmp 0x1e351
incq %r12
jmp 0x1e349
incq %r10
jmp 0x1e337
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq