// File: /workspace/github_c_makefile_star2_O1/nh13[P]ksw/src/main.c
// Function: align_with_parasail
void align_with_parasail(char *query, int query_length, char *target, int target_length, main_opt_t *opt, void *library_data, alignment_t *alignment)
{
	parasail_data_t *parasail_data = (parasail_data_t*)library_data;
	int i;
	parasail_result_t *parasail_result;
	parasail_cigar_t *parasail_cigar;
	parasail_result = parasail_data->func(query, query_length, target, target_length, opt->gap_open + opt->gap_extend, opt->gap_extend, parasail_data->matrix);

	// set the score
	alignment->score = parasail_result->score;

	// set end of alignmnet
	alignment->qle = parasail_result->end_query;
	alignment->tle = parasail_result->end_ref;

	// add the cigar, and if so, set the alignment beginning
	if (opt->add_cigar == 1) {
		parasail_cigar = parasail_result_get_cigar(parasail_result, query, query_length, target, target_length, parasail_data->matrix);
		alignment->qlb = parasail_cigar->beg_query;
		alignment->tlb = parasail_cigar->beg_ref;
		// NB: recompute beg_ref using leading deletions for glocal alignment
		// beg_ref can be wrong for glocal.  See: https://github.com/jeffdaily/parasail/issues/97
		if (opt->alignment_mode == Glocal) {
			alignment->tlb = 0;
			for (i = 0; i < parasail_cigar->len; ++i) {
				char op = parasail_cigar_decode_op(parasail_cigar->seq[i]);
				if (op != 'D') break;
				uint32_t len = parasail_cigar_decode_len(parasail_cigar->seq[i]);
				alignment->tlb += len;
			}
		}
		int prev_op_int = -1;
		for (i = 0; i < parasail_cigar->len; ++i) {
			char op = parasail_cigar_decode_op(parasail_cigar->seq[i]);
			int op_int;
			// "MIDNSHP=XB"
			switch (op) {
				case 'M':
				case '=':
				case 'X':
					op_int = 0; break;
				case 'I':
					op_int = 1; break;
				case 'D':
					op_int = 2; break;
				default:
					fprintf(stderr, "Unknown cigar type: %c\n", op);
					exit(1);
			}
			uint32_t len = parasail_cigar_decode_len(parasail_cigar->seq[i]);
			if (prev_op_int == op_int) { // add to the previous
				len += alignment->cigar[alignment->n_cigar-1] >> 4; 
				alignment->cigar[alignment->n_cigar-1] = len<<4 | op_int;
			}
			else { // new cigar element
				if (alignment->n_cigar == alignment->m_cigar) {
					alignment->m_cigar = alignment->m_cigar ? (alignment->m_cigar)<<1 : 4;
					alignment->cigar = (uint32_t*)realloc(alignment->cigar, alignment->m_cigar*sizeof(uint32_t));
				}
				alignment->cigar[alignment->n_cigar] = len<<4 | op_int;
				alignment->n_cigar++;
			}
			prev_op_int = op_int;
		}
		parasail_cigar_free(parasail_cigar);
	}
	else {
		alignment->qlb = -1;
		alignment->tlb = -1;
	}
	parasail_result_free(parasail_result);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, %r13
movq %r8, %r15
movq %rdx, %r14
movl %esi, %ebp
movq %rdi, %rbx
movq 0x50(%rsp), %r12
movl 0xc(%r8), %r9d
movl 0x8(%r8), %r8d
addl %r9d, %r8d
movq (%r13), %rax
movq %rax, (%rsp)
movl %ecx, 0xc(%rsp)
callq *0x8(%r13)
movq %rax, %rdi
movl (%rax), %eax
movl %eax, (%r12)
movl 0x4(%rdi), %eax
movl %eax, 0xc(%r12)
movl 0x8(%rdi), %eax
movl %eax, 0x10(%r12)
cmpl $0x1, 0x24(%r15)
jne 0x204daa
movq (%r13), %r9
movq %rdi, 0x10(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
callq 0x209298
movq %rax, %r14
movl 0xc(%rax), %eax
movl %eax, 0x4(%r12)
movl 0x10(%r14), %eax
movl %eax, 0x8(%r12)
cmpl $0x1, 0x20(%r15)
jne 0x204cb2
movl $0x0, 0x8(%r12)
cmpl $0x0, 0x8(%r14)
jle 0x204cb2
xorl %ebx, %ebx
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x2058db
cmpb $0x44, %al
jne 0x204cb2
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x2058f0
addl %eax, 0x8(%r12)
incq %rbx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jl 0x204c87
cmpl $0x0, 0x8(%r14)
jle 0x204d9b
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x2058db
movzbl %al, %edi
movl $0x0, %r13d
cmpl $0x48, %edi
jle 0x204cf9
cmpl $0x58, %edi
je 0x204d0d
cmpl $0x4d, %edi
je 0x204d0d
cmpl $0x49, %edi
jne 0x204dc6
movl $0x1, %r13d
jmp 0x204d0d
cmpl $0x3d, %edi
je 0x204d0d
cmpl $0x44, %edi
jne 0x204dc6
movl $0x2, %r13d
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x2058f0
movl %eax, %ebp
cmpl %r13d, %r15d
jne 0x204d3c
movq 0x18(%r12), %rax
movslq 0x20(%r12), %rcx
shll $0x4, %ebp
addl -0x4(%rax,%rcx,4), %ebp
andl $-0x10, %ebp
orl %r13d, %ebp
movl %ebp, -0x4(%rax,%rcx,4)
jmp 0x204d88
movl 0x24(%r12), %eax
cmpl %eax, 0x20(%r12)
jne 0x204d70
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x4, %eax
cmovel %eax, %ecx
movl %ecx, 0x24(%r12)
movq 0x18(%r12), %rdi
movslq %ecx, %rsi
shlq $0x2, %rsi
callq 0x19e1e0
movq %rax, 0x18(%r12)
shll $0x4, %ebp
orl %r13d, %ebp
movq 0x18(%r12), %rax
movslq 0x20(%r12), %rcx
movl %ebp, (%rax,%rcx,4)
incl 0x20(%r12)
incq %rbx
movslq 0x8(%r14), %rax
movl %r13d, %r15d
cmpq %rax, %rbx
jl 0x204cc5
movq %r14, %rdi
callq 0x2058c6
movq 0x10(%rsp), %rdi
jmp 0x204db3
movq $-0x1, 0x4(%r12)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x209999
callq 0x19e75c