// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movq %r8, %rbx
movl %ecx, %r12d
movq %rdx, %r14
movl %esi, %r15d
xorl %eax, %eax
movl %edi, 0x4(%rsp)
cmpl $0x2, %edi
setge %al
leal 0x8(,%rax,8), %r13d
incl %eax
movl %eax, (%rsp)
xorl $0x18, %r13d
leal (%r15,%r13), %eax
decl %eax
cltd
idivl %r13d
movl %eax, %ebp
movl %ecx, %eax
shll $0x4, %eax
addl $0x40, %eax
imull %ebp, %eax
movslq %eax, %rdi
addq $0x138, %rdi            # imm = 0x138
leaq 0x67e4(%rip), %rsi      # 0x29618
leaq 0x67e3(%rip), %rcx      # 0x2961e
movl $0x48, %edx
callq 0x22d2b
leaq 0x47(%rax), %rcx
andq $-0x10, %rcx
movq %rcx, 0x10(%rax)
movl %ebp, %edx
imull %r12d, %edx
movslq %edx, %rdx
shlq $0x4, %rdx
addq %rcx, %rdx
movq %rdx, 0x18(%rax)
movslq %ebp, %rsi
movq %rsi, %rdi
shlq $0x4, %rdi
addq %rdi, %rdx
movq %rdx, 0x20(%rax)
addq %rdi, %rdx
movq %rdx, 0x28(%rax)
addq %rdi, %rdx
movq %rdx, 0x30(%rax)
movl %esi, 0x4(%rax)
movl %r15d, (%rax)
movl (%rsp), %edx
movb %dl, 0xb(%rax)
movw $0x7f, 0x8(%rax)
testl %r12d, %r12d
je 0x22eda
movl %r12d, %edx
imull %edx, %edx
movb 0x8(%rax), %sil
movb 0x9(%rax), %dil
cmpl $0x1, %edx
adcl $0x0, %edx
xorl %r8d, %r8d
movb (%rbx,%r8), %r9b
cmpb %sil, %r9b
jge 0x22ec2
movb %r9b, 0x8(%rax)
movl %r9d, %esi
movb (%rbx,%r8), %r9b
cmpb %dil, %r9b
jle 0x22ed2
movb %r9b, 0x9(%rax)
movl %r9d, %edi
incq %r8
cmpq %r8, %rdx
jne 0x22eb2
movb 0x8(%rax), %dl
movb 0x9(%rax), %sil
movb %sil, 0xa(%rax)
subb %dl, %sil
negb %dl
movb %dl, 0x8(%rax)
movb %sil, 0x9(%rax)
cmpl $0x2, 0x4(%rsp)
jge 0x22f5c
testl %r12d, %r12d
jle 0x22fb9
imull %ebp, %r13d
movl %ebp, %edx
movl %r12d, %esi
xorl %edi, %edi
testl %ebp, %ebp
jle 0x22f52
movq %rdi, %r8
imulq %rsi, %r8
addq %rbx, %r8
xorl %r9d, %r9d
cmpl %r9d, %r13d
jle 0x22f4a
movq %r9, %r10
cmpl %r15d, %r10d
jge 0x22f35
movzbl (%r14,%r10), %r11d
movb (%r8,%r11), %r11b
jmp 0x22f38
xorl %r11d, %r11d
addb 0x8(%rax), %r11b
movb %r11b, (%rcx)
incq %rcx
addq %rdx, %r10
cmpl %r10d, %r13d
jg 0x22f25
incq %r9
cmpq %rdx, %r9
jne 0x22f1d
incq %rdi
cmpq %rsi, %rdi
jne 0x22f0c
jmp 0x22fb9
testl %r12d, %r12d
jle 0x22fb9
imull %ebp, %r13d
movl %ebp, %edx
movl %r12d, %esi
xorl %edi, %edi
testl %ebp, %ebp
jle 0x22fb1
movq %rdi, %r8
imulq %rsi, %r8
addq %rbx, %r8
xorl %r9d, %r9d
cmpl %r9d, %r13d
jle 0x22fa9
movq %r9, %r10
cmpl %r15d, %r10d
jge 0x22f96
movzbl (%r14,%r10), %r11d
movsbl (%r8,%r11), %r11d
jmp 0x22f99
xorl %r11d, %r11d
movw %r11w, (%rcx)
addq $0x2, %rcx
addq %rdx, %r10
cmpl %r10d, %r13d
jg 0x22f85
incq %r9
cmpq %rdx, %r9
jne 0x22f7d
incq %rdi
cmpq %rsi, %rdi
jne 0x22f6c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq