// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/ksw.c
// Function: ksw_align2
kswr_t ksw_align2(int qlen, uint8_t *query, int tlen, uint8_t *target, int m, const int8_t *mat, int o_del, int e_del, int o_ins, int e_ins, 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, 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, o_del, e_del, o_ins, e_ins, 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, o_del, e_del, o_ins, e_ins, 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 $0x58, %rsp
movl %r9d, %r13d
movq %r8, 0x20(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, %r15
movq %rdi, %rbx
movq 0xc0(%rsp), %r14
movl 0xb8(%rsp), %ebp
testq %r14, %r14
je 0x1eded
movq (%r14), %r12
testq %r12, %r12
jne 0x1ee1b
btl $0x10, %ebp
pushq $0x1
popq %rdi
sbbl $-0x1, %edi
movq %r15, %rdx
movl %r13d, %ecx
movq 0x90(%rsp), %r8
callq 0x1e1a0
movq %rax, %r12
testq %r14, %r14
je 0x1ee1b
cmpq $0x0, (%r14)
jne 0x1ee1b
movq %r12, (%r14)
movl %r13d, 0x8(%rsp)
movq %r15, 0x18(%rsp)
movzbl 0xb(%r12), %r13d
cmpl $0x2, %r13d
leaq -0x538(%rip), %rax      # 0x1e8fe
leaq -0xaaf(%rip), %r15      # 0x1e38e
cmoveq %rax, %r15
subq $0x8, %rsp
movq %rbx, %rdi
movq %r12, %rsi
movl 0xc(%rsp), %edx
movq 0x28(%rsp), %rcx
movl 0xa0(%rsp), %r8d
movl 0xa8(%rsp), %r9d
pushq %rbp
movl 0xc0(%rsp), %eax
pushq %rax
movl 0xc0(%rsp), %eax
pushq %rax
callq *%r15
addq $0x20, %rsp
testq %r14, %r14
jne 0x1ee89
movq %r12, %rdi
callq 0x3390
btl $0x13, %ebp
jae 0x1ef9a
movl %ebp, %eax
shrl $0x12, %eax
movl (%rbx), %r12d
movzwl %bp, %ecx
cmpl %ecx, %r12d
setl %cl
testb %cl, %al
jne 0x1ef9a
movl 0x8(%rbx), %eax
movq %rax, 0x30(%rsp)
leal 0x1(%rax), %ebp
movl %ebp, %edi
movl %ebp, 0x10(%rsp)
movq 0x18(%rsp), %rsi
callq 0x1efac
movl 0x4(%rbx), %eax
movq %rax, 0x28(%rsp)
leal 0x1(%rax), %edi
movl %edi, 0x14(%rsp)
movq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x1efac
movl %r13d, %edi
movl %ebp, %esi
movq 0x18(%rsp), %rbp
movq %rbp, %rdx
movl 0x8(%rsp), %ecx
movq 0x90(%rsp), %r8
callq 0x1e1a0
movq %rax, %rsi
movq %rax, 0x8(%rsp)
movl %r12d, %eax
orl $0x20000, %eax          # imm = 0x20000
subq $0x8, %rsp
leaq 0x44(%rsp), %r13
movq %r13, %rdi
movl 0xc(%rsp), %edx
movq %r14, %rcx
movl 0xa0(%rsp), %r8d
movl 0xa8(%rsp), %r9d
pushq %rax
movl 0xc0(%rsp), %eax
pushq %rax
movl 0xc0(%rsp), %eax
pushq %rax
callq *%r15
addq $0x20, %rsp
movl 0x4(%r13), %r15d
movl 0x8(%r13), %eax
movl %eax, 0x4(%rsp)
movl (%r13), %r13d
movl 0x10(%rsp), %edi
movq %rbp, %rsi
callq 0x1efac
movl 0x14(%rsp), %edi
movq %r14, %rsi
callq 0x1efac
movq 0x8(%rsp), %rdi
callq 0x3390
cmpl %r13d, %r12d
jne 0x1ef9a
movq 0x28(%rsp), %rax
subl %r15d, %eax
movl %eax, 0x14(%rbx)
movq 0x30(%rsp), %rax
subl 0x4(%rsp), %eax
movl %eax, 0x18(%rbx)
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq