// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/ksw.c
// Function: ksw_align
kswr_t ksw_align(int qlen, uint8_t *query, int tlen, uint8_t *target, int m, const int8_t *mat, int gapo, int gape, int xtra, kswq_t **qry)
{
	int size;
	kswq_t *q;
	kswr_t r, rr;
	kswr_t (*func)(kswq_t*, int, const uint8_t*, int, int, int);

	q = (qry && *qry)? *qry : ksw_qinit((xtra&KSW_XBYTE)? 1 : 2, qlen, query, m, mat);
	if (qry && *qry == 0) *qry = q;
	func = q->size == 2? ksw_i16 : ksw_u8;
	size = q->size;
	r = func(q, tlen, target, gapo, gape, xtra);
	if (qry == 0) free(q);
	if ((xtra&KSW_XSTART) == 0 || ((xtra&KSW_XSUBO) && r.score < (xtra&0xffff))) return r;
	revseq(r.qe + 1, query); revseq(r.te + 1, target); // +1 because qe/te points to the exact end, not the position after the end
	q = ksw_qinit(size, r.qe + 1, query, m, mat);
	rr = func(q, tlen, target, gapo, gape, KSW_XSTOP | r.score);
	revseq(r.qe + 1, query); revseq(r.te + 1, target);
	free(q);
	if (r.score == rr.score)
		r.tb = r.te - rr.te, r.qb = r.qe - rr.qe;
	return r;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %ebp
movq %r8, 0x30(%rsp)
movl %ecx, 0x14(%rsp)
movq %rdx, %r13
movq %rdi, %rbx
movq 0xc0(%rsp), %r14
movl 0xb8(%rsp), %r15d
testq %r14, %r14
je 0x119b2
movq (%r14), %r12
testq %r12, %r12
jne 0x119e0
btl $0x10, %r15d
pushq $0x1
popq %rdi
sbbl $-0x1, %edi
movq %r13, %rdx
movl %ebp, %ecx
movq 0xa0(%rsp), %r8
callq 0x10ec8
movq %rax, %r12
testq %r14, %r14
je 0x119e0
cmpq $0x0, (%r14)
jne 0x119e0
movq %r12, (%r14)
movl %ebp, 0x18(%rsp)
movq %r13, 0x28(%rsp)
movzbl 0xb(%r12), %r13d
cmpl $0x2, %r13d
leaq -0x4a4(%rip), %rax      # 0x11556
leaq -0x979(%rip), %rbp      # 0x11088
cmoveq %rax, %rbp
movl %r15d, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movq 0x30(%rsp), %rcx
movl 0xa8(%rsp), %r8d
movl 0xb0(%rsp), %r9d
callq *%rbp
testq %r14, %r14
jne 0x11a37
movq %r12, %rdi
callq 0x2040
btl $0x13, %r15d
jae 0x11b37
movl %r15d, %eax
shrl $0x12, %eax
movl (%rbx), %r12d
movzwl %r15w, %ecx
cmpl %ecx, %r12d
setl %cl
testb %cl, %al
jne 0x11b37
movl 0x8(%rbx), %eax
movq %rax, 0x40(%rsp)
leal 0x1(%rax), %r15d
movl %r15d, %edi
movl %r15d, 0x20(%rsp)
movq 0x28(%rsp), %rsi
callq 0x11b49
movl 0x4(%rbx), %eax
movq %rax, 0x38(%rsp)
leal 0x1(%rax), %edi
movl %edi, 0x24(%rsp)
movq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x11b49
movl %r13d, %edi
movl %r15d, %esi
movq 0x28(%rsp), %r15
movq %r15, %rdx
movl 0x18(%rsp), %ecx
movq 0xa0(%rsp), %r8
callq 0x10ec8
movq %rax, %rsi
movq %rax, 0x18(%rsp)
movl %r12d, %eax
orl $0x20000, %eax          # imm = 0x20000
movl %eax, (%rsp)
leaq 0x4c(%rsp), %r13
movq %r13, %rdi
movl 0x14(%rsp), %edx
movq %r14, %rcx
movl 0xa8(%rsp), %r8d
movl 0xb0(%rsp), %r9d
callq *%rbp
movl 0x4(%r13), %ebp
movl 0x8(%r13), %eax
movl %eax, 0x14(%rsp)
movl (%r13), %r13d
movl 0x20(%rsp), %edi
movq %r15, %rsi
callq 0x11b49
movl 0x24(%rsp), %edi
movq %r14, %rsi
callq 0x11b49
movq 0x18(%rsp), %rdi
callq 0x2040
cmpl %r13d, %r12d
jne 0x11b37
movq 0x38(%rsp), %rax
subl %ebp, %eax
movl %eax, 0x14(%rbx)
movq 0x40(%rsp), %rax
subl 0x14(%rsp), %eax
movl %eax, 0x18(%rbx)
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq