// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %r8
movl 0x8(%r8), %r8d
movq -0x28(%rbp), %r9
addl 0xc(%r9), %r8d
movq -0x28(%rbp), %r9
movl 0xc(%r9), %r9d
movq -0x38(%rbp), %r10
movq (%r10), %r10
movq %r10, (%rsp)
callq *%rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jne 0x1b87e2
movq -0x48(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
movq -0x18(%rbp), %rcx
movl -0x1c(%rbp), %r8d
movq -0x38(%rbp), %rax
movq (%rax), %r9
callq 0x1c3060
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0xc(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x50(%rbp), %rax
movl 0x10(%rax), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x1b863a
movq 0x10(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x1b8638
movq -0x50(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b9e40
movb %al, -0x51(%rbp)
movsbl -0x51(%rbp), %eax
cmpl $0x44, %eax
je 0x1b860a
jmp 0x1b8638
movq -0x50(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b9e80
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %ecx
movq 0x10(%rbp), %rax
addl 0x8(%rax), %ecx
movl %ecx, 0x8(%rax)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1b85dd
jmp 0x1b863a
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x1b87d7
movq -0x50(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b9e40
movb %al, -0x5d(%rbp)
movsbl -0x5d(%rbp), %eax
addl $-0x3d, %eax
movl %eax, %ecx
movq %rcx, -0x70(%rbp)
subl $0x1b, %eax
ja 0x1b86af
movq -0x70(%rbp), %rax
leaq 0xad4cf9(%rip), %rcx    # 0xc8d384
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x64(%rbp)
jmp 0x1b86d5
movl $0x1, -0x64(%rbp)
jmp 0x1b86d5
movl $0x2, -0x64(%rbp)
jmp 0x1b86d5
movq 0xc6e922(%rip), %rax    # 0xe26fd8
movq (%rax), %rdi
movsbl -0x5d(%rbp), %edx
leaq 0xad51bf(%rip), %rsi    # 0xc8d883
movb $0x0, %al
callq 0x19e180
movl $0x1, %edi
callq 0x19e130
movq -0x50(%rbp), %rax
movq (%rax), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edi
callq 0x1b9e80
movl %eax, -0x68(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x64(%rbp), %eax
jne 0x1b873a
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
shrl $0x4, %eax
addl -0x68(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %edx
shll $0x4, %edx
orl -0x64(%rbp), %edx
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rbp), %rcx
movl 0x20(%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x1b87c3
movq 0x10(%rbp), %rax
movl 0x20(%rax), %eax
movq 0x10(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jne 0x1b879a
movq 0x10(%rbp), %rax
cmpl $0x0, 0x24(%rax)
je 0x1b8762
movq 0x10(%rbp), %rax
movl 0x24(%rax), %eax
shll %eax
movl %eax, -0x74(%rbp)
jmp 0x1b876c
movl $0x4, %eax
movl %eax, -0x74(%rbp)
jmp 0x1b876c
movl -0x74(%rbp), %ecx
movq 0x10(%rbp), %rax
movl %ecx, 0x24(%rax)
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq 0x10(%rbp), %rax
movslq 0x24(%rax), %rsi
shlq $0x2, %rsi
callq 0x19e1f0
movq %rax, %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0x68(%rbp), %edx
shll $0x4, %edx
orl -0x64(%rbp), %edx
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rbp), %rcx
movslq 0x20(%rcx), %rcx
movl %edx, (%rax,%rcx,4)
movq 0x10(%rbp), %rax
movl 0x20(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x20(%rax)
movl -0x64(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1b8648
movq -0x50(%rbp), %rdi
callq 0x1b9e10
jmp 0x1b87f8
movq 0x10(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq 0x10(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movq -0x48(%rbp), %rdi
callq 0x1c4590
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)