// File: /workspace/github_c_makefile_star2_O3/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 0x23f89
movq (%r13), %r12
testq %r12, %r12
jne 0x23fba
btl $0x10, %ebx
movl $0x1, %edi
sbbl $-0x1, %edi
movq %r15, %rdx
movl %ebp, %ecx
movq 0x80(%rsp), %r8
callq 0x232c0
movq %rax, %r12
testq %r13, %r13
je 0x23fba
cmpq $0x0, (%r13)
jne 0x23fba
movq %r12, (%r13)
movl %ebp, 0x8(%rsp)
movzbl 0xb(%r12), %eax
movl %eax, 0x20(%rsp)
cmpl $0x2, %eax
leaq -0x58b(%rip), %rax      # 0x23a47
leaq -0xb1a(%rip), %rbp      # 0x234bf
cmoveq %rax, %rbp
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movl 0x14(%rsp), %edx
movq %r14, %rcx
movl 0x90(%rsp), %r8d
movl 0x98(%rsp), %r9d
pushq %rbx
movl 0xb0(%rsp), %eax
pushq %rax
movl 0xb0(%rsp), %eax
pushq %rax
callq *%rbp
addq $0x20, %rsp
testq %r13, %r13
jne 0x24024
movq %r12, %rdi
callq 0x3390
btl $0x13, %ebx
jae 0x241b4
btl $0x12, %ebx
jae 0x24043
movzwl %bx, %eax
movq (%rsp), %rcx
cmpl %eax, (%rcx)
jl 0x241b4
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 0x24085
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 0x24068
movq (%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, 0x10(%rsp)
leal 0x1(%rax), %r13d
sarl %r13d
movl %r13d, %ebx
testl %r13d, %r13d
jle 0x240c4
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 0x240a7
movl 0x20(%rsp), %edi
movq %r15, %rdx
movl 0x8(%rsp), %ecx
movq 0x80(%rsp), %r8
callq 0x232c0
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 0x24155
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 0x24138
movl 0x2c(%rsp), %ebp
testl %r13d, %r13d
jle 0x24182
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 0x24165
movl 0x30(%rsp), %r14d
movl 0x34(%rsp), %ebx
movq 0x20(%rsp), %rdi
callq 0x3390
cmpl %ebp, 0x8(%rsp)
jne 0x241b4
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