// File: /workspace/github_c_makefile_star2_O2/lh3[P]minimap2/align.c
// Function: mm_align_skeleton
mm_reg1_t *mm_align_skeleton(void *km, const mm_mapopt_t *opt, const mm_idx_t *mi, int qlen, const char *qstr, int *n_regs_, mm_reg1_t *regs, mm128_t *a)
{
	extern unsigned char seq_nt4_table[256];
	int32_t i, n_regs = *n_regs_, n_a;
	uint8_t *qseq0[2];
	ksw_extz_t ez;

	// encode the query sequence
	qseq0[0] = (uint8_t*)kmalloc(km, qlen * 2);
	qseq0[1] = qseq0[0] + qlen;
	for (i = 0; i < qlen; ++i) {
		qseq0[0][i] = seq_nt4_table[(uint8_t)qstr[i]];
		qseq0[1][qlen - 1 - i] = qseq0[0][i] < 4? 3 - qseq0[0][i] : 4;
	}

	// align through seed hits
	n_a = mm_squeeze_a(km, n_regs, regs, a);
	memset(&ez, 0, sizeof(ksw_extz_t));
	for (i = 0; i < n_regs; ++i) {
		mm_reg1_t r2; // only used for inversion
		if ((opt->flag&MM_F_SPLICE) && (opt->flag&MM_F_SPLICE_FOR) && (opt->flag&MM_F_SPLICE_REV)) { // then do two rounds of alignments for both strands
			mm_reg1_t s[2], s2[2], *r;
			s[0] = s[1] = regs[i];
			mm_align1(km, opt, mi, qlen, qseq0, &s[0], &s2[0], n_a, a, &ez, MM_F_SPLICE_FOR); // assume the transcript is on the + strand of the genome
			if ((opt->flag&MM_F_SR_RNA) && regs[i].qe - regs[i].qs == regs[i].re - regs[i].rs && s[0].qe - s[0].qs == s[0].re - s[0].rs && s[0].qs == 0 && s[0].qe == qlen) {
				regs[i] = s[0], r2 = s2[0];
				regs[i].p->trans_strand = 0;
			} else {
				int which, trans_strand;
				mm_align1(km, opt, mi, qlen, qseq0, &s[1], &s2[1], n_a, a, &ez, MM_F_SPLICE_REV); // assume the transcript on the - strand
				if (s[0].p->dp_score > s[1].p->dp_score) which = 0, trans_strand = 1;
				else if (s[0].p->dp_score < s[1].p->dp_score) which = 1, trans_strand = 2;
				else trans_strand = 3, which = (qlen + s[0].p->dp_score) & 1; // randomly choose a strand, effectively
				if (which == 0) {
					regs[i] = s[0], r2 = s2[0];
					free(s[1].p);
				} else {
					regs[i] = s[1], r2 = s2[1];
					free(s[0].p);
				}
				r = &regs[i];
				r->p->trans_strand = trans_strand;
				if (r->is_spliced) {
					if (trans_strand == 1 || trans_strand == 2) // this is an *approximate* way to tell if there are splice signals.
						r->p->dp_max += (opt->a + opt->b) + ((opt->a + opt->b) >> 1);
					else if (trans_strand == 3)
						r->p->dp_max -= opt->a + opt->b;
				}
			}
		} else { // one round of alignment
			mm_align1(km, opt, mi, qlen, qseq0, &regs[i], &r2, n_a, a, &ez, opt->flag);
			if (opt->flag&MM_F_SPLICE)
				regs[i].p->trans_strand = opt->flag&MM_F_SPLICE_FOR? 1 : 2;
		}
		if (r2.cnt > 0) regs = mm_insert_reg(&r2, i, &n_regs, regs);
		if (i > 0 && regs[i].split_inv && !(opt->flag & MM_F_NO_INV)) {
			if (mm_align1_inv(km, opt, mi, qlen, qseq0, &regs[i-1], &regs[i], &r2, &ez)) {
				regs = mm_insert_reg(&r2, i, &n_regs, regs);
				++i; // skip the inserted INV alignment
			}
		}
	}
	*n_regs_ = n_regs;
	kfree(km, qseq0[0]);
	kfree(km, ez.cigar);
	mm_filter_regs(opt, qlen, n_regs_, regs);
	if (!(opt->flag&(MM_F_SR|MM_F_SR_RNA|MM_F_ALL_CHAINS)) && !opt->split_prefix && qlen >= opt->rank_min_len) {
		mm_update_dp_max(qlen, *n_regs_, regs, opt->rank_frac, opt->a, opt->b);
		mm_filter_regs(opt, qlen, n_regs_, regs);
	}
	mm_hit_sort(km, n_regs_, regs, opt->alt_drop);
	return regs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %r8, %rbx
movl %ecx, %ebp
movq %rdx, 0x38(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movq 0x2a0(%rsp), %r13
movq %r9, 0xd0(%rsp)
movl (%r9), %r14d
movl %r14d, 0x10(%rsp)
leal (,%rbp,2), %eax
movslq %eax, %rsi
callq 0x10afc
leaq 0x58(%rsp), %r8
movq %rax, -0x8(%r8)
movslq %ebp, %rcx
movq %rax, %rdx
addq %rcx, %rdx
xorl %esi, %esi
testl %ecx, %ecx
movl $0x0, %edi
movq %rbp, 0x28(%rsp)
cmovgl %ebp, %edi
movq %rdx, (%r8)
decl %ecx
leaq 0x1d637(%rip), %r8      # 0x32810
pushq $0x4
popq %r9
cmpq %rsi, %rdi
je 0x1520f
movzbl (%rbx,%rsi), %r10d
movb (%r8,%r10), %r10b
movb %r10b, (%rax,%rsi)
movb $0x3, %r11b
subb %r10b, %r11b
cmpb $0x4, %r10b
movzbl %r11b, %r10d
cmovael %r9d, %r10d
movslq %ecx, %rcx
movb %r10b, (%rdx,%rcx)
incq %rsi
decl %ecx
jmp 0x151dd
movq %r15, %rdi
movl %r14d, %esi
movq %r13, %rdx
movq 0x2a8(%rsp), %rcx
callq 0x1a130
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
leaq 0xe0(%rsp), %rax
movapd %xmm0, (%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, 0x20(%rax)
andq $0x0, 0x30(%rax)
xorl %ebp, %ebp
movq %r12, 0x68(%rsp)
movq 0x28(%rsp), %rbx
movq %r15, 0xd8(%rsp)
cmpl %r14d, %ebp
jge 0x159ea
movq (%r12), %rax
movl %eax, %edx
notl %edx
movslq %ebp, %rcx
imulq $0x50, %rcx, %r14
testw $0x380, %dx             # imm = 0x380
jne 0x1538c
movq %r13, 0x8(%rsp)
addq %r13, %r14
pushq $0xa
popq %rcx
leaq 0x170(%rsp), %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x120(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
pushq $0x50
popq %rdx
callq 0x2340
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
movl %ebx, %ecx
leaq 0x58(%rsp), %r8
movq %r13, %r9
pushq $0x100                  # imm = 0x100
leaq 0xf0(%rsp), %rax
pushq %rax
pushq 0x2c0(%rsp)
pushq 0x60(%rsp)
leaq 0x1e8(%rsp), %rax
pushq %rax
callq 0x15ab6
addq $0x30, %rsp
movabsq $0x8000000000, %rax     # imm = 0x8000000000
testq %rax, (%r12)
je 0x15404
movl 0x14(%r14), %eax
subl 0x10(%r14), %eax
movl 0x1c(%r14), %ecx
subl 0x18(%r14), %ecx
cmpl %ecx, %eax
jne 0x15404
movl 0x134(%rsp), %eax
cmpl %ebx, %eax
jne 0x15404
movl 0x130(%rsp), %ecx
testl %ecx, %ecx
jne 0x15404
subl %ecx, %eax
movl 0x13c(%rsp), %ecx
subl 0x138(%rsp), %ecx
cmpl %ecx, %eax
jne 0x15404
pushq $0xa
popq %rax
movq %rax, %rcx
movq %r14, %rdi
leaq 0x120(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rcx
leaq 0x70(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x48(%r14), %rax
andl $0x3fffffff, 0x14(%rax) # imm = 0x3FFFFFFF
jmp 0x1553b
addq %r13, %r14
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
movl %ebx, %ecx
leaq 0x58(%rsp), %r8
movq %r14, %r9
pushq %rax
leaq 0xf0(%rsp), %rax
pushq %rax
pushq 0x2c0(%rsp)
pushq 0x60(%rsp)
leaq 0x98(%rsp), %rax
pushq %rax
callq 0x15ab6
addq $0x30, %rsp
movq (%r12), %rax
testb %al, %al
jns 0x1554a
btl $0x8, %eax
movl $0x40000000, %eax       # imm = 0x40000000
movl $0x80000000, %ecx       # imm = 0x80000000
cmovael %ecx, %eax
movq 0x48(%r14), %rcx
movl 0x14(%rcx), %edx
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl %esi, %edx
orl %eax, %edx
movl %edx, 0x14(%rcx)
jmp 0x1554a
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %rsi
movq 0x40(%rsp), %rdx
movl %ebx, %ecx
leaq 0x58(%rsp), %r8
leaq 0x178(%rsp), %r9
pushq $0x200                  # imm = 0x200
leaq 0xf0(%rsp), %rax
pushq %rax
pushq 0x2c0(%rsp)
pushq 0x60(%rsp)
leaq 0x238(%rsp), %rax
pushq %rax
callq 0x15ab6
addq $0x30, %rsp
movq 0x168(%rsp), %rax
movq 0x1b8(%rsp), %rcx
movl 0x4(%rax), %ebx
movl 0x4(%rcx), %r13d
cmpl %r13d, %ebx
jle 0x1548c
movl $0x40000000, %r15d      # imm = 0x40000000
xorl %r12d, %r12d
leaq 0x120(%rsp), %rsi
leaq 0x1c0(%rsp), %rax
leaq 0x1b8(%rsp), %rdx
jmp 0x154c1
setl %r12b
movl $0x80000000, %r15d      # imm = 0x80000000
jl 0x154a9
movq 0x28(%rsp), %rax
addl %ebx, %eax
movl $0xc0000000, %r15d      # imm = 0xC0000000
testb $0x1, %al
je 0x1546f
leaq 0x170(%rsp), %rsi
leaq 0x210(%rsp), %rax
leaq 0x168(%rsp), %rdx
pushq $0xa
popq %r8
movq %r8, %rcx
movq %r14, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r8, %rcx
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq (%rdx), %rdi
callq 0x2040
movq 0x48(%r14), %rax
movl 0x14(%rax), %ecx
movl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl %edx, %ecx
orl %r15d, %ecx
movl %ecx, 0x14(%rax)
testb $0x8, 0x3f(%r14)
movq 0xd8(%rsp), %r15
je 0x1553b
cmpl %r13d, %ebx
setle %dl
xorb $0x1, %r12b
movq 0x68(%rsp), %rax
movl 0x64(%rax), %ecx
movl 0x68(%rax), %eax
testb %r12b, %dl
jne 0x15532
addl %ecx, %eax
movl %eax, %ecx
sarl %ecx
addl %eax, %ecx
movq 0x48(%r14), %rax
addl %ecx, 0x8(%rax)
jmp 0x1553b
movq 0x48(%r14), %rdx
addl %ecx, %eax
subl %eax, 0x8(%rdx)
movq 0x68(%rsp), %r12
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %rbx
cmpl $0x0, 0x74(%rsp)
jle 0x15568
leaq 0x70(%rsp), %rdi
movl %ebp, %esi
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0x17f21
movq %rax, %r13
testl %ebp, %ebp
jle 0x159de
movl %ebp, %eax
imulq $0x50, %rax, %r14
testb $0x1, 0x3f(%r13,%r14)
je 0x159de
testb $0x2, 0x4(%r12)
jne 0x159de
leal -0x1(%rbp), %eax
imulq $0x50, %rax, %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movl 0x3c(%r13,%rdi), %eax
btl $0x8, %eax
jae 0x159de
addq %r13, %r14
movl 0x3c(%r14), %ecx
btl $0x9, %ecx
jae 0x159de
addq %r13, %rdi
movl 0x20(%rdi), %edx
cmpl %edx, (%rdi)
setne %sil
cmpl $-0x2, %edx
setne %dl
testb %dl, %sil
jne 0x159de
movl 0x20(%r14), %edx
cmpl %edx, (%r14)
setne %sil
cmpl $-0x2, %edx
setne %dl
testb %dl, %sil
jne 0x159de
movl 0x8(%rdi), %edx
cmpl 0x8(%r14), %edx
jne 0x159de
shrl $0xa, %eax
andl $0x1, %eax
btl $0xa, %ecx
movl %eax, %ecx
sbbl $0x0, %ecx
jne 0x159de
testl %eax, %eax
movq %rdi, %rax
cmoveq %r14, %rax
movq %r14, %rcx
cmoveq %rdi, %rcx
movl 0x10(%rax), %esi
subl 0x14(%rcx), %esi
movl 0x34(%r12), %eax
cmpl %eax, %esi
jl 0x159de
movl 0x1c(%r12), %ecx
cmpl %ecx, %esi
jg 0x159de
movslq 0x18(%r14), %r8
movslq 0x1c(%rdi), %rdx
subq %rdx, %r8
cmpl %eax, %r8d
setl %al
cmpl %ecx, %r8d
setg %cl
orb %al, %cl
jne 0x159de
movl %esi, 0x4(%rsp)
movsbl 0x64(%r12), %esi
movsbl 0x68(%r12), %edx
movsbl 0x7c(%r12), %ecx
movq %r8, 0x20(%rsp)
movsbl 0x80(%r12), %r8d
movq %rdi, 0x30(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x17ff9
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x10afc
movq %rax, %r8
movq 0x30(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x30(%rsp), %rax
movl 0x1c(%rax), %edx
movl 0x18(%r14), %ecx
movq 0x38(%rsp), %rdi
movq %r8, 0x8(%rsp)
callq 0x612e
movq 0x30(%rsp), %rax
testb $0x4, 0x3d(%rax)
jne 0x1570a
movl %ebx, %eax
subl 0x10(%r14), %eax
leaq 0x58(%rsp), %rcx
jmp 0x15713
movl 0x14(%r14), %eax
leaq 0x50(%rsp), %rcx
movl 0x4(%rsp), %edi
movslq %eax, %rsi
addq (%rcx), %rsi
movq %rsi, 0x18(%rsp)
callq 0x181b8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x181b8
movq %r15, %rdi
pushq $0x2
popq %rsi
movl 0x4(%rsp), %edx
movq 0x18(%rsp), %rcx
pushq $0x5
popq %r8
leaq 0x120(%rsp), %r9
callq 0x1bce4
movq %rax, 0xc8(%rsp)
movl 0x6c(%r12), %ecx
movl 0x70(%r12), %r8d
subq $0x8, %rsp
movq %rax, %rdi
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0x1c8(%rsp), %r9
leaq 0x1c(%rsp), %rax
pushq %rax
callq 0x1bec3
popq %rcx
popq %rdx
movl %eax, 0x4c(%rsp)
movq %r15, %rdi
movq 0xc8(%rsp), %rsi
callq 0x10a07
movl 0x4(%rsp), %edi
movq 0x18(%rsp), %rsi
callq 0x181b8
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x181b8
movl 0x4c(%rsp), %eax
cmpl 0x9c(%r12), %eax
jl 0x15904
movl 0x1c0(%rsp), %eax
leal 0x1(%rax), %edx
notl %eax
movl 0x4(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0x1c0(%rsp)
movslq 0x14(%rsp), %r8
incq %r8
movslq 0x20(%rsp), %r9
subq %r8, %r9
movl %r9d, 0x14(%rsp)
movslq %ecx, %rcx
addq 0x18(%rsp), %rcx
addq 0x8(%rsp), %r9
cvtsi2sdl 0x14(%r12), %xmm0
mulsd 0x13d8d(%rip), %xmm0    # 0x295a8
cvttsd2si %xmm0, %eax
movl 0x90(%r12), %r10d
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %rsi
leaq 0xe8(%rsp), %r11
pushq %r11
pushq $0x40
pushq %r10
pushq $-0x1
pushq %rax
leaq 0x150(%rsp), %rax
pushq %rax
pushq $0x0
callq 0x181df
addq $0x40, %rsp
movl 0x104(%rsp), %esi
testl %esi, %esi
je 0x15904
movq 0x110(%rsp), %rdx
leaq 0x70(%rsp), %rdi
callq 0x185b3
movl 0xe0(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0xb8(%rsp), %rcx
movl %eax, 0x4(%rcx)
orl $-0x1, 0x70(%rsp)
orl $-0x1, 0x90(%rsp)
movl 0xac(%rsp), %eax
movl $0x800, %ecx            # imm = 0x800
orl %ecx, %eax
movl %eax, 0xac(%rsp)
movq 0x30(%rsp), %rdi
movl 0x3c(%rdi), %edx
movl $0x400, %ecx            # imm = 0x400
andl %ecx, %edx
andl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
orl %edx, %eax
xorl %ecx, %eax
movl %eax, 0xac(%rsp)
movl 0x8(%rdi), %eax
movl %eax, 0x78(%rsp)
movl $0xbf800000, 0xb4(%rsp) # imm = 0xBF800000
movslq 0x1c0(%rsp), %rax
movl 0xe4(%rsp), %ecx
testl %edx, %edx
jne 0x15916
movl 0x10(%r14), %edx
subl %eax, %edx
notl %ecx
addl %edx, %ecx
movl %ecx, %esi
jmp 0x15921
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x10a07
jmp 0x159de
movl 0x14(%r14), %esi
addl %eax, %esi
leal (%rcx,%rsi), %edx
incl %edx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movl %edx, 0x84(%rsp)
movl %esi, 0x80(%rsp)
movslq 0x14(%rsp), %rcx
movl 0x1c(%rdi), %edx
addl %ecx, %edx
movl %edx, 0x88(%rsp)
movl 0xe8(%rsp), %esi
addl %esi, %edx
incl %edx
movl %edx, 0x8c(%rsp)
addq %rax, %r9
movq %r8, %rdx
addq %rcx, %rdx
movsbl 0x6c(%r12), %r8d
movq %r9, %rsi
movsbl 0x70(%r12), %r9d
movq (%r12), %rax
movl %eax, %r10d
andl $0x4000000, %r10d       # imm = 0x4000000
movabsq $0x8000000000, %rcx     # imm = 0x8000000000
addq $0x1000, %rcx           # imm = 0x1000
xorl %r11d, %r11d
testq %rcx, %rax
sete %r11b
leaq 0x70(%rsp), %r14
movq %r14, %rdi
leaq 0x120(%rsp), %rcx
pushq %r11
pushq %r10
callq 0x18659
addq $0x10, %rsp
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x10a07
movq %r14, %rdi
movl %ebp, %esi
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0x17f21
movq %rax, %r13
incl %ebp
incl %ebp
movl 0x10(%rsp), %r14d
jmp 0x1525d
movq %rbx, %rbp
movq 0xd0(%rsp), %rbx
movl %r14d, (%rbx)
movq 0x50(%rsp), %rsi
movq %r15, %rdi
callq 0x10a07
movq 0x110(%rsp), %rsi
movq %r15, %rdi
callq 0x10a07
movq %r12, %rdi
movl %ebp, %esi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1a040
movabsq $0x8000000000, %rax     # imm = 0x8000000000
addq $0x801000, %rax         # imm = 0x801000
testq %rax, (%r12)
jne 0x15a8c
cmpq $0x0, 0x100(%r12)
jne 0x15a8c
movq 0x28(%rsp), %rax
cmpl %eax, 0xb0(%r12)
jg 0x15a8c
movl (%rbx), %esi
movss 0xb4(%r12), %xmm0
movl 0x64(%r12), %ecx
movl 0x68(%r12), %r8d
movq 0x28(%rsp), %r14
movl %r14d, %edi
movq %r13, %rdx
callq 0x14f4d
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x1a040
movss 0x60(%r12), %xmm0
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x19a6a
movq %r13, %rax
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq