// File: /workspace/github_c_makefile_star2_O2/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 0x1a6ae4
movq (%r13), %r9
movq %rdi, 0x10(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
movq %r14, %rcx
movl 0xc(%rsp), %r8d
callq 0x1ab553
movq %rax, %r14
movq 0xc(%rax), %rax
movq %rax, 0x4(%r12)
cmpl $0x1, 0x20(%r15)
jne 0x1a6aef
andl $0x0, 0x8(%r12)
xorl %ebx, %ebx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jge 0x1a6aef
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x1a7953
cmpb $0x44, %al
jne 0x1a6aef
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x1a7968
addl %eax, 0x8(%r12)
incq %rbx
jmp 0x1a6ab7
orq $-0x1, 0x4(%r12)
jmp 0x1a6bc9
pushq $-0x1
popq %r15
xorl %ebx, %ebx
movslq 0x8(%r14), %rax
cmpq %rax, %rbx
jge 0x1a6bbc
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x1a7953
xorl %r13d, %r13d
cmpb $0x3d, %al
je 0x1a6b37
movzbl %al, %ecx
cmpl $0x44, %ecx
je 0x1a6b33
cmpl $0x58, %ecx
je 0x1a6b37
cmpl $0x4d, %ecx
je 0x1a6b37
cmpl $0x49, %ecx
jne 0x1a6bdc
pushq $0x1
jmp 0x1a6b35
pushq $0x2
popq %r13
movq (%r14), %rax
movl (%rax,%rbx,4), %edi
callq 0x1a7968
movl %eax, %ebp
cmpl %r13d, %r15d
jne 0x1a6b66
movq 0x18(%r12), %rax
movslq 0x20(%r12), %rcx
shll $0x4, %ebp
addl -0x4(%rax,%rcx,4), %ebp
andl $-0x10, %ebp
orl %r15d, %ebp
movl %ebp, -0x4(%rax,%rcx,4)
jmp 0x1a6bb1
movl 0x20(%r12), %ecx
movq 0x18(%r12), %rax
cmpl 0x24(%r12), %ecx
jne 0x1a6ba0
leal (%rcx,%rcx), %edx
testl %ecx, %ecx
pushq $0x4
popq %rcx
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 %r13d, %ebp
movslq %ecx, %rcx
movl %ebp, (%rax,%rcx,4)
incl 0x20(%r12)
incq %rbx
movl %r13d, %r15d
jmp 0x1a6af5
movq %r14, %rdi
callq 0x1a793e
movq 0x10(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1abdad
movsbl %al, %edx
movq 0x5943f2(%rip), %rax    # 0x73afd8
movq (%rax), %rdi
leaq 0x3c1c8f(%rip), %rsi    # 0x56887f
xorl %eax, %eax
callq 0x19e180
pushq $0x1
popq %rdi
callq 0x19e130