// File: 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:
subq $0xb8, %rsp
movq %rdi, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq 0xe0(%rsp), %rax
movl 0xd8(%rsp), %eax
movl 0xd0(%rsp), %eax
movl 0xc8(%rsp), %eax
movq 0xc0(%rsp), %rax
movl %esi, 0xb4(%rsp)
movq %rdx, 0xa8(%rsp)
movl %ecx, 0xa4(%rsp)
movq %r8, 0x98(%rsp)
movl %r9d, 0x94(%rsp)
cmpq $0x0, 0xe0(%rsp)
je 0x762f7
movq 0xe0(%rsp), %rax
cmpq $0x0, (%rax)
je 0x762f7
movq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
jmp 0x7633c
movl 0xd8(%rsp), %ecx
andl $0x10000, %ecx          # imm = 0x10000
movl $0x2, %edi
movl $0x1, %eax
cmpl $0x0, %ecx
cmovnel %eax, %edi
movl 0xb4(%rsp), %esi
movq 0xa8(%rsp), %rdx
movl 0x94(%rsp), %ecx
movq 0xc0(%rsp), %r8
callq 0x73a10
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0xe0(%rsp)
je 0x76375
movq 0xe0(%rsp), %rax
cmpq $0x0, (%rax)
jne 0x76375
movq 0x88(%rsp), %rcx
movq 0xe0(%rsp), %rax
movq %rcx, (%rax)
movq 0x88(%rsp), %rax
movzbl 0xb(%rax), %edx
leaq -0x2538(%rip), %rax     # 0x73e50
leaq -0x111f(%rip), %rcx     # 0x75270
cmpl $0x2, %edx
cmoveq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0x88(%rsp), %rax
movzbl 0xb(%rax), %eax
movl %eax, 0x90(%rsp)
movq 0x60(%rsp), %rax
movq 0x88(%rsp), %rsi
movl 0xa4(%rsp), %edx
movq 0x98(%rsp), %rcx
movl 0xc8(%rsp), %r8d
movl 0xd0(%rsp), %r9d
movl 0xd8(%rsp), %r10d
leaq 0x44(%rsp), %rdi
movl %r10d, (%rsp)
callq *%rax
movq 0x18(%rsp), %rax
movq 0x44(%rsp), %rcx
movq %rcx, (%rax)
movq 0x4c(%rsp), %rcx
movq %rcx, 0x8(%rax)
movq 0x54(%rsp), %rcx
movq %rcx, 0x10(%rax)
movl 0x5c(%rsp), %ecx
movl %ecx, 0x18(%rax)
cmpq $0x0, 0xe0(%rsp)
jne 0x7642b
movq 0x88(%rsp), %rdi
callq 0x8470
movl 0xd8(%rsp), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x76465
movl 0xd8(%rsp), %eax
andl $0x40000, %eax          # imm = 0x40000
cmpl $0x0, %eax
je 0x7646a
movq 0x18(%rsp), %rax
movl (%rax), %eax
movl 0xd8(%rsp), %ecx
andl $0xffff, %ecx           # imm = 0xFFFF
cmpl %ecx, %eax
jge 0x7646a
jmp 0x765a4
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
addl $0x1, %edi
movq 0xa8(%rsp), %rsi
callq 0x765c0
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edi
addl $0x1, %edi
movq 0x98(%rsp), %rsi
callq 0x765c0
movq 0x18(%rsp), %rax
movl 0x90(%rsp), %edi
movl 0x8(%rax), %esi
addl $0x1, %esi
movq 0xa8(%rsp), %rdx
movl 0x94(%rsp), %ecx
movq 0xc0(%rsp), %r8
callq 0x73a10
movq 0x18(%rsp), %rdi
movq %rax, 0x88(%rsp)
movq 0x60(%rsp), %rax
movq 0x88(%rsp), %rsi
movl 0xa4(%rsp), %edx
movq 0x98(%rsp), %rcx
movl 0xc8(%rsp), %r8d
movl 0xd0(%rsp), %r9d
movl (%rdi), %r10d
orl $0x20000, %r10d         # imm = 0x20000
leaq 0x28(%rsp), %rdi
movl %r10d, (%rsp)
callq *%rax
movq 0x18(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x6c(%rsp)
movq 0x30(%rsp), %rcx
movq %rcx, 0x74(%rsp)
movq 0x38(%rsp), %rcx
movq %rcx, 0x7c(%rsp)
movl 0x40(%rsp), %ecx
movl %ecx, 0x84(%rsp)
movl 0x8(%rax), %edi
addl $0x1, %edi
movq 0xa8(%rsp), %rsi
callq 0x765c0
movq 0x18(%rsp), %rax
movl 0x4(%rax), %edi
addl $0x1, %edi
movq 0x98(%rsp), %rsi
callq 0x765c0
movq 0x88(%rsp), %rdi
callq 0x8470
movq 0x18(%rsp), %rax
movl (%rax), %eax
cmpl 0x6c(%rsp), %eax
jne 0x765a2
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
subl 0x70(%rsp), %ecx
movl %ecx, 0x14(%rax)
movl 0x8(%rax), %ecx
subl 0x74(%rsp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x765a4
movq 0x20(%rsp), %rax
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)