// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movl -0x4(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x1, %edx
cmovgl %ecx, %eax
movl %eax, -0x4(%rbp)
movl $0x3, %eax
subl -0x4(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x38(%rbp)
movl -0x8(%rbp), %eax
addl -0x38(%rbp), %eax
subl $0x1, %eax
cltd
idivl -0x38(%rbp)
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
shll $0x4, %eax
movl -0x14(%rbp), %ecx
addl $0x4, %ecx
imull %ecx, %eax
movslq %eax, %rdi
addq $0x138, %rdi            # imm = 0x138
leaq 0x95e8(%rip), %rsi      # 0x3b4f8
movl $0x48, %edx
leaq 0x95e2(%rip), %rcx      # 0x3b4fe
callq 0x31ca0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
addq $0x38, %rcx
addq $0xf, %rcx
shrq $0x4, %rcx
shlq $0x4, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movl -0x2c(%rbp), %eax
imull -0x14(%rbp), %eax
cltq
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
movslq -0x2c(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movl -0x2c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x8(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x4(%rbp), %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0xb(%rax)
movl -0x14(%rbp), %eax
imull -0x14(%rbp), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movb $0x7f, 0x8(%rax)
movq -0x28(%rbp), %rax
movb $0x0, 0x9(%rax)
movl -0x30(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x3205b
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x28(%rbp), %rcx
movsbl 0x8(%rcx), %ecx
cmpl %ecx, %eax
jge 0x32024
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x28(%rbp), %rcx
movsbl 0x9(%rcx), %ecx
cmpl %ecx, %eax
jle 0x3204e
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movb (%rax,%rcx), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x9(%rax)
jmp 0x32050
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x31ff2
movq -0x28(%rbp), %rax
movb 0x9(%rax), %cl
movq -0x28(%rbp), %rax
movb %cl, 0xa(%rax)
movq -0x28(%rbp), %rax
movzbl 0x8(%rax), %ecx
movl $0x100, %eax            # imm = 0x100
subl %ecx, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x8(%rax)
movq -0x28(%rbp), %rax
movzbl 0x8(%rax), %edx
movq -0x28(%rbp), %rax
movzbl 0x9(%rax), %ecx
addl %edx, %ecx
movb %cl, 0x9(%rax)
cmpl $0x1, -0x4(%rbp)
jne 0x3216e
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x32169
movl -0x2c(%rbp), %eax
imull -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x14(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x32159
movl -0x44(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0x3214c
movl -0x48(%rbp), %eax
cmpl -0x8(%rbp), %eax
jl 0x3210a
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x32121
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x48(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x28(%rbp), %rcx
movzbl 0x8(%rcx), %ecx
addl %ecx, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb %cl, (%rax)
movl -0x2c(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0x320f3
jmp 0x3214e
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x320e5
jmp 0x3215b
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x320b3
jmp 0x32231
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x3222f
movl -0x2c(%rbp), %eax
imull -0x38(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x14(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x3221f
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jge 0x32212
movl -0x68(%rbp), %eax
cmpl -0x8(%rbp), %eax
jl 0x321d8
xorl %eax, %eax
movl %eax, -0x80(%rbp)
jmp 0x321ef
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq -0x68(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movw %ax, %cx
movq -0x60(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x60(%rbp)
movw %cx, (%rax)
movl -0x2c(%rbp), %eax
addl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
jmp 0x321c1
jmp 0x32214
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x321b3
jmp 0x32221
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x32181
jmp 0x32231
movq -0x28(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nop