// File: /workspace/github_c_makefile_star2_O3/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)
movq 0x4(%rdi), %rax
movq %rax, 0xc(%r12)
cmpl $0x1, 0x24(%r15)
jne 0x204f81
movq (%r13), %r9
movq %rdi, 0x10(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
callq 0x20a41b
movq %rax, %r14
movq 0xc(%rax), %rax
movq %rax, 0x4(%r12)
cmpl $0x1, 0x20(%r15)
jne 0x204e88
movl $0x0, 0x8(%r12)
cmpl $0x0, 0x8(%r14)
jle 0x204e88
xorl %ebx, %ebx
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x205a62
cmpb $0x44, %al
jne 0x204e88
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x205a77
addl %eax, 0x8(%r12)
incq %rbx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jl 0x204e5d
cmpl $0x0, 0x8(%r14)
jle 0x204f72
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x205a62
movzbl %al, %edi
movl $0x0, %r15d
cmpl $0x48, %edi
jle 0x204ecf
cmpl $0x58, %edi
je 0x204ee3
cmpl $0x4d, %edi
je 0x204ee3
cmpl $0x49, %edi
jne 0x204f9d
movl $0x1, %r15d
jmp 0x204ee3
cmpl $0x3d, %edi
je 0x204ee3
cmpl $0x44, %edi
jne 0x204f9d
movl $0x2, %r15d
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x205a77
movl %eax, %ebp
cmpl %r15d, %r13d
jne 0x204f12
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 0x204f5f
movl 0x20(%r12), %ecx
movq 0x18(%r12), %rax
cmpl 0x24(%r12), %ecx
jne 0x204f4e
leal (%rcx,%rcx), %edx
testl %ecx, %ecx
movl $0x4, %ecx
cmovel %ecx, %edx
movl %edx, 0x24(%r12)
movslq %edx, %rsi
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x19e1e0
movq %rax, 0x18(%r12)
movl 0x20(%r12), %ecx
shll $0x4, %ebp
orl %r15d, %ebp
movslq %ecx, %rcx
movl %ebp, (%rax,%rcx,4)
incl 0x20(%r12)
incq %rbx
movslq 0x8(%r14), %rax
movl %r15d, %r13d
cmpq %rax, %rbx
jl 0x204e9b
movq %r14, %rdi
callq 0x205a4d
movq 0x10(%rsp), %rdi
jmp 0x204f8a
movq $-0x1, 0x4(%r12)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20afbc
callq 0x19e78f