// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movl %r9d, %ebp
movq %r8, %r14
movl %ecx, 0x1c(%rsp)
movq %rdx, %r15
movq %rdi, 0x10(%rsp)
movq 0xb0(%rsp), %r13
movl 0xa8(%rsp), %ebx
testq %r13, %r13
je 0x1446a
movq (%r13), %r12
testq %r12, %r12
jne 0x1449b
btl $0x10, %ebx
movl $0x1, %edi
sbbl $-0x1, %edi
movq %r15, %rdx
movl %ebp, %ecx
movq 0x90(%rsp), %r8
callq 0x13910
movq %rax, %r12
testq %r13, %r13
je 0x1449b
cmpq $0x0, (%r13)
jne 0x1449b
movq %r12, (%r13)
movl %ebp, 0x18(%rsp)
movzbl 0xb(%r12), %eax
movl %eax, 0x30(%rsp)
cmpl $0x2, %eax
leaq -0x4ea(%rip), %rax      # 0x13fc9
leaq -0x9d2(%rip), %rbp      # 0x13ae8
cmoveq %rax, %rbp
movl %ebx, (%rsp)
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movl 0x1c(%rsp), %edx
movq %r14, %rcx
movl 0x98(%rsp), %r8d
movl 0xa0(%rsp), %r9d
callq *%rbp
testq %r13, %r13
jne 0x144ef
movq %r12, %rdi
callq 0x2040
btl $0x13, %ebx
jae 0x1466e
btl $0x12, %ebx
jae 0x1450f
movzwl %bx, %eax
movq 0x10(%rsp), %rcx
cmpl %eax, (%rcx)
jl 0x1466e
movq 0x10(%rsp), %rax
movl 0x8(%rax), %eax
movq %rax, 0x28(%rsp)
leal 0x1(%rax), %esi
movl %esi, %eax
sarl %eax
movl %eax, %r12d
movl %eax, 0x38(%rsp)
testl %eax, %eax
jle 0x14552
xorl %eax, %eax
movq 0x28(%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 0x14535
movq 0x10(%rsp), %rax
movl 0x4(%rax), %eax
movq %rax, 0x20(%rsp)
leal 0x1(%rax), %r13d
sarl %r13d
movl %r13d, %ebx
testl %r13d, %r13d
jle 0x14592
xorl %eax, %eax
movq 0x20(%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 0x14575
movl 0x30(%rsp), %edi
movq %r15, %rdx
movl 0x18(%rsp), %ecx
movq 0x90(%rsp), %r8
callq 0x13910
movq %rax, %rsi
movq 0x10(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x18(%rsp)
orl $0x20000, %eax          # imm = 0x20000
movl %eax, (%rsp)
leaq 0x3c(%rsp), %rdi
movq %rsi, 0x30(%rsp)
movl 0x1c(%rsp), %edx
movq %r14, %rcx
movl 0x98(%rsp), %r8d
movl 0xa0(%rsp), %r9d
callq *%rbp
cmpl $0x0, 0x38(%rsp)
jle 0x1460e
xorl %eax, %eax
movq 0x28(%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 0x145f1
movl 0x3c(%rsp), %ebp
testl %r13d, %r13d
jle 0x1463b
xorl %eax, %eax
movq 0x20(%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 0x1461e
movl 0x40(%rsp), %r14d
movl 0x44(%rsp), %ebx
movq 0x30(%rsp), %rdi
callq 0x2040
cmpl %ebp, 0x18(%rsp)
jne 0x1466e
movq 0x20(%rsp), %rcx
subl %r14d, %ecx
movq 0x10(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x28(%rsp), %rcx
subl %ebx, %ecx
movl %ecx, 0x18(%rax)
movq 0x10(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop