// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %r9d, %ebp
movq %r8, %r14
movl %ecx, 0xc(%rsp)
movq %rdx, %r15
movq %rdi, (%rsp)
movq 0xb0(%rsp), %r13
movl 0xa8(%rsp), %ebx
testq %r13, %r13
je 0x23a38
movq (%r13), %r12
testq %r12, %r12
jne 0x23a59
btl $0x10, %ebx
movl $0x1, %edi
sbbl $-0x1, %edi
movq %r15, %rdx
movl %ebp, %ecx
movq 0x80(%rsp), %r8
callq 0x22dd8
movq %rax, %r12
movl %ebp, 0x8(%rsp)
movl 0xa0(%rsp), %r11d
movl 0x98(%rsp), %r10d
movl 0x90(%rsp), %r9d
movl 0x88(%rsp), %r8d
testq %r13, %r13
je 0x23a8d
cmpq $0x0, (%r13)
jne 0x23a8d
movq %r12, (%r13)
movzbl 0xb(%r12), %eax
movl %eax, 0x20(%rsp)
cmpl $0x2, %eax
leaq -0x57e(%rip), %rax      # 0x23523
leaq -0xae0(%rip), %rbp      # 0x22fc8
cmoveq %rax, %rbp
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movq %r14, %rcx
pushq %rbx
pushq %r11
pushq %r10
callq *%rbp
addq $0x20, %rsp
testq %r13, %r13
jne 0x23ad7
movq %r12, %rdi
callq 0x3390
btl $0x13, %ebx
jae 0x23c67
btl $0x12, %ebx
jae 0x23af6
movzwl %bx, %eax
movq (%rsp), %rcx
cmpl %eax, (%rcx)
jl 0x23c67
movq (%rsp), %rax
movl 0x8(%rax), %eax
movq %rax, 0x18(%rsp)
leal 0x1(%rax), %esi
movl %esi, %eax
sarl %eax
movl %eax, %r12d
movl %eax, 0x28(%rsp)
testl %eax, %eax
jle 0x23b38
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movb (%r15,%rax), %dl
movslq %ecx, %rcx
movb (%r15,%rcx), %dil
movb %dil, (%r15,%rax)
movb %dl, (%r15,%rcx)
incq %rax
decl %ecx
cmpq %rax, %r12
jne 0x23b1b
movq (%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, 0x10(%rsp)
leal 0x1(%rax), %r13d
sarl %r13d
movl %r13d, %ebx
testl %r13d, %r13d
jle 0x23b77
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movb (%r14,%rax), %dl
movslq %ecx, %rcx
movb (%r14,%rcx), %dil
movb %dil, (%r14,%rax)
movb %dl, (%r14,%rcx)
incq %rax
decl %ecx
cmpq %rax, %rbx
jne 0x23b5a
movl 0x20(%rsp), %edi
movq %r15, %rdx
movl 0x8(%rsp), %ecx
movq 0x80(%rsp), %r8
callq 0x22dd8
movq %rax, %rsi
movq (%rsp), %rax
movl (%rax), %eax
movl %eax, 0x8(%rsp)
orl $0x20000, %eax          # imm = 0x20000
subq $0x8, %rsp
leaq 0x34(%rsp), %rdi
movq %rsi, 0x28(%rsp)
movl 0x14(%rsp), %edx
movq %r14, %rcx
movl 0x90(%rsp), %r8d
movl 0x98(%rsp), %r9d
pushq %rax
movl 0xb0(%rsp), %eax
pushq %rax
movl 0xb0(%rsp), %eax
pushq %rax
callq *%rbp
addq $0x20, %rsp
cmpl $0x0, 0x28(%rsp)
jle 0x23c08
xorl %eax, %eax
movq 0x18(%rsp), %rcx
movb (%r15,%rax), %dl
movslq %ecx, %rcx
movb (%r15,%rcx), %sil
movb %sil, (%r15,%rax)
movb %dl, (%r15,%rcx)
incq %rax
decl %ecx
cmpq %rax, %r12
jne 0x23beb
movl 0x2c(%rsp), %ebp
testl %r13d, %r13d
jle 0x23c35
xorl %eax, %eax
movq 0x10(%rsp), %rcx
movb (%r14,%rax), %dl
movslq %ecx, %rcx
movb (%r14,%rcx), %sil
movb %sil, (%r14,%rax)
movb %dl, (%r14,%rcx)
incq %rax
decl %ecx
cmpq %rax, %rbx
jne 0x23c18
movl 0x30(%rsp), %r14d
movl 0x34(%rsp), %ebx
movq 0x20(%rsp), %rdi
callq 0x3390
cmpl %ebp, 0x8(%rsp)
jne 0x23c67
movq 0x10(%rsp), %rcx
subl %r14d, %ecx
movq (%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x18(%rsp), %rcx
subl %ebx, %ecx
movl %ecx, 0x18(%rax)
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq