// File: /workspace/github_cpp_makefile_star2_O3/readmapping[P]alfalfa/src/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 0x3f68e
movq (%r13), %r12
testq %r12, %r12
jne 0x3f6bf
btl $0x10, %ebx
movl $0x1, %edi
sbbl $-0x1, %edi
movq %r15, %rdx
movl %ebp, %ecx
movq 0x90(%rsp), %r8
callq 0x3eb34
movq %rax, %r12
testq %r13, %r13
je 0x3f6bf
cmpq $0x0, (%r13)
jne 0x3f6bf
movq %r12, (%r13)
movl %ebp, 0x18(%rsp)
movzbl 0xb(%r12), %eax
movl %eax, 0x30(%rsp)
cmpl $0x2, %eax
leaq -0x4ea(%rip), %rax      # 0x3f1ed
leaq -0x9d2(%rip), %rbp      # 0x3ed0c
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 0x3f713
movq %r12, %rdi
callq 0x7380
btl $0x13, %ebx
jae 0x3f892
btl $0x12, %ebx
jae 0x3f733
movzwl %bx, %eax
movq 0x10(%rsp), %rcx
cmpl %eax, (%rcx)
jl 0x3f892
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 0x3f776
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 0x3f759
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 0x3f7b6
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 0x3f799
movl 0x30(%rsp), %edi
movq %r15, %rdx
movl 0x18(%rsp), %ecx
movq 0x90(%rsp), %r8
callq 0x3eb34
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 0x3f832
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 0x3f815
movl 0x3c(%rsp), %ebp
testl %r13d, %r13d
jle 0x3f85f
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 0x3f842
movl 0x40(%rsp), %r14d
movl 0x44(%rsp), %ebx
movq 0x30(%rsp), %rdi
callq 0x7380
cmpl %ebp, 0x18(%rsp)
jne 0x3f892
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