// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: gl_chain_advance_back(kv_ul_ov_t*, ul_ov_t*, ul_idx_t const*, ug_opt_t const*, long, double, long, long, unsigned long*, unsigned long*, unsigned long*, float, All_reads*, ma_ug_t*, void*)
int64_t gl_chain_advance_back(kv_ul_ov_t *res, ul_ov_t *ex, const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, 
double diff_ec_ul, int64_t qlen, int64_t max_skip, uint64_t *srt, uint64_t *idx, uint64_t *track, float trans_allow, 
All_reads *ridx, ma_ug_t *ug, void *km)
{
	uint32_t li_v, lj_v, rev_n, is_c, nc, s_nc;
	int64_t mm_ovlp, x, i, j, k, sc, csc, mm_sc, mm_idx, qo, trans_scl = (int64_t)(((float)(1))/trans_allow), nsc[2];
	ul_ov_t *li = NULL, *lj = NULL, rev_t;
	radix_sort_ul_ov_srt_qe(res->a, res->a + res->n);
	for (i = s_nc = 0; i < (int64_t)res->n; ++i) {
		li = &(res->a[i]); li_v = (li->tn<<1)|li->rev;
		mm_ovlp = uref?max_ovlp(uref->ug->g, li_v^1):max_ovlp_src(uopt, li_v^1);
		x = (li->qs + mm_ovlp)*diff_ec_ul; 
		if(x < bw) x = bw; 
		x += li->qs + mm_ovlp;
		if (x > qlen+1) x = qlen+1;
		x = find_ul_ov_max(i, res->a, x); 
		csc = uref?retrieve_u_cov_region(uref, li->tn, 0, li->ts, li->te, NULL):li->te-li->ts;
		if(!(li->el)) {
			csc *= -trans_scl; //trans overlaps
			if(csc >= 0) csc = -1;
		}
		mm_sc = csc; mm_idx = -1; nc = nsc[0] = nsc[1] = 0;
		for (j = x; j >= 0; --j) { // collect potential destination vertices
			lj = &(res->a[j]); lj_v = (lj->tn<<1)|lj->rev;
			// if((lj->qe+gapLen) <= li->qs) break; 
			if(lj->qe <= li->qs) break;//even this pair has a overlap, its length will be very small; just ignore
			// if(lj->qs >= li->qs) continue; // lj is contained in li on the query coordinate
			qo = infer_rovlp(li, lj, NULL, NULL, ridx, ug); ///overlap length in query (UL read)
			if(li_v != lj_v && get_ecov_adv_back(uref, uopt, li_v^1, lj_v^1, bw, diff_ec_ul, qo, &is_c)) {
				if(!is_c) {
					sc = csc + pop_sc(track[j]);
					if(sc > mm_sc) mm_sc = sc, mm_idx = j;
				} else if(!uref) {///with uref, retrieve_u_cov_region has already consider contained reads
					res->a[nc++].sec = j; 
					if(li->el) nsc[0] += lj->te-lj->ts;
					else nsc[!(lj->el)] += lj->te-lj->ts;
				}
			}
		}
		
		if(nc && (!uref) && mm_idx>=0) {///deal with containments
			mm_sc += determine_containment_chain(uopt, track, srt, res, nc, nsc, mm_idx, bw, diff_ec_ul, li->el, ridx, ug);
			s_nc++;
		}

		track[i] = push_sc_pre(mm_sc, mm_idx);
		srt[i] = track[i]>>32; srt[i] <<= 32; srt[i] |= i;
		// fprintf(stderr, "[M::utg%.6d%c] qs->%u; qe->%u\n\n", li->tn+1, "lc"[uref->ug->u.a[li->tn].circ], li->qs, li->qe);
	}

	if(s_nc) {
		for (i = 0; i < (int64_t)res->n; ++i) {
			if(srt[i]&((uint64_t)0x80000000)) srt[i]-=((uint64_t)0x80000000);
		}
	}
	
	int64_t n_v, n_u, n_v0, le, lnv; 
	radix_sort_gfa64(srt, srt+res->n); //ex->n = res->n;
	for (k = (int64_t)res->n-1, n_v = n_u = 0; k >= 0; --k) {
		n_v0 = n_v; i = (uint32_t)srt[k];
		if(i>=0 && (res->a[i].el)) { ///chain must start from cis alignments
			for (le = -1; i >= 0 && (track[i]&((uint64_t)0x80000000)) == 0;) {
				if(res->a[i].el) {
					le = -1;
				}else if(n_v>n_v0 && ex[n_v-1].el) {
					le = i; lnv = n_v;///cut the cis alignments in the end
				}
				ex[n_v++] = res->a[i]; track[i] |= ((uint64_t)0x80000000);
				// fprintf(stderr, "[M::utg%.6d%c] qs->%u; qe->%u\n", res->a[i].tn+1, "lc"[uref->ug->u.a[res->a[i].tn].circ], res->a[i].qs, res->a[i].qe);
				i = pop_pre(track[i]);
			}
		}
		if(n_v0 == n_v) continue;
		if(le >= 0) {
			i = le; n_v = lnv;
		}
		if(n_v0 == n_v) continue;
		///keep the whole score; do not cut score like minigraph
		// sc = pop_sc(srt[k]);
		sc = (i<0?(pop_sc(srt[k])):(pop_sc(srt[k])-pop_sc(track[i])));
		if(sc < 0) {
			n_v = n_v0;
			continue;
		}
		// idx[n_u++] = push_sc_pre(sc, n_v-n_v0);	
		idx[n_u++] = ((uint64_t)sc<<32)|(n_v-n_v0);	
	}

	for (k = 0, n_v = n_v0 = 0; k < n_u; k++) {
		n_v0 = n_v; n_v += (uint32_t)idx[k];
		res->a[k].qn = idx[k]>>32;
		res->a[k].ts = n_v0; res->a[k].te = n_v;
		
		rev_n = ((uint32_t)idx[k])>>1; 
		///we need to consider contained reads; so determining qs is not such easy
		res->a[k].qs = (uint32_t)-1; res->a[k].qe = ex[n_v0].qe;
		for (i = 0; i < rev_n; i++) {
			rev_t = ex[n_v0+i]; 
			ex[n_v0+i] = ex[n_v0+rev_n-i-1];
			ex[n_v0+rev_n-i-1] = rev_t;
			if(res->a[k].qs > ex[n_v0+i].qs) res->a[k].qs = ex[n_v0+i].qs;
			if(res->a[k].qs > ex[n_v0+rev_n-i-1].qs) res->a[k].qs = ex[n_v0+rev_n-i-1].qs;
		}
		if(i < ((uint32_t)idx[k]) && res->a[k].qs < ex[n_v0+i].qs) {
			res->a[k].qs = ex[n_v0+i].qs;
		}
	}
	res->n = n_u;
	radix_sort_ul_ov_srt_qn(res->a, res->a + res->n);
	return n_v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r9, %r14
movsd %xmm0, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, %r12
movq %rsi, 0x30(%rsp)
movq %rdi, %rbx
movq 0x108(%rsp), %r13
movq 0xf8(%rsp), %rbp
movss 0x7c1ab(%rip), %xmm0    # 0x1e00d0
divss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movq %rax, 0x78(%rsp)
movq (%rdi), %rdi
imulq $0x1c, 0x8(%rbx), %rsi
addq %rdi, %rsi
callq 0x156f5c
incq %r14
movq %r14, 0x80(%rsp)
xorl %ecx, %ecx
movl $0x0, 0x2c(%rsp)
movq %r12, 0x20(%rsp)
movq %rbx, (%rsp)
movq 0x8(%rbx), %rax
cmpq %rax, %rcx
jge 0x1642c2
movq (%rbx), %r14
movq %rcx, 0x58(%rsp)
imulq $0x1c, %rcx, %r15
movl 0xc(%r14,%r15), %eax
movl 0x18(%r14,%r15), %ebp
shrdl $0x1f, %eax, %ebp
testq %r12, %r12
je 0x163fa4
movq (%r12), %rax
movq 0x18(%rax), %rdi
movl %ebp, %esi
xorl $0x1, %esi
callq 0x15d450
movq %r14, %rsi
jmp 0x163fb6
movl %ebp, %esi
xorl $0x1, %esi
movq 0x38(%rsp), %rdi
callq 0x15d49a
movq (%rbx), %rsi
addq %r15, %r14
movl 0x4(%r14), %ecx
addq %rax, %rcx
cvtsi2sd %rcx, %xmm0
mulsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %rdx
movq 0x40(%rsp), %rax
cmpq %rax, %rdx
cmovleq %rax, %rdx
addq %rcx, %rdx
movq 0x80(%rsp), %rax
cmpq %rax, %rdx
cmovgeq %rax, %rdx
movq 0x58(%rsp), %rdi
callq 0x160717
movl %eax, %r15d
movl 0x10(%r14), %ecx
testq %r12, %r12
je 0x16401e
movl 0xc(%r14), %esi
movl 0x14(%r14), %r8d
movq %r12, %rdi
xorl %edx, %edx
xorl %r9d, %r9d
callq 0xa240
movq %rax, %rdx
jmp 0x164024
movl 0x14(%r14), %edx
subl %ecx, %edx
pushq $-0x1
popq %rax
testb $0x40, 0x1b(%r14)
jne 0x16403f
imulq 0x78(%rsp), %rdx
negq %rdx
cmpq $-0x1, %rdx
cmovgeq %rax, %rdx
movslq %r15d, %r12
movl %ebp, %ebx
xorl $0x1, %ebp
movl %ebp, 0x50(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
imulq $0x1c, %r12, %r13
movl $0x0, 0xc(%rsp)
movq %rdx, 0x88(%rsp)
movq %rdx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
testq %r12, %r12
js 0x1641cf
movq (%rsp), %rax
movq (%rax), %rbp
movl 0x8(%rbp,%r13), %eax
cmpl 0x4(%r14), %eax
jbe 0x1641cf
movq %rbp, %rsi
addq %r13, %rsi
movl 0xc(%rbp,%r13), %eax
movl 0x18(%rbp,%r13), %r15d
shrdl $0x1f, %eax, %r15d
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x110(%rsp), %r8
movq 0x118(%rsp), %r9
callq 0x3d261
cmpl %r15d, %ebx
je 0x1641c3
xorl $0x1, %r15d
subq $0x8, %rsp
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x58(%rsp), %edx
movl %r15d, %ecx
movq 0x48(%rsp), %r8
movsd 0x50(%rsp), %xmm0
movq %rax, %r9
leaq 0x5c(%rsp), %rax
pushq %rax
callq 0x162ece
popq %rcx
popq %rdx
testq %rax, %rax
je 0x1641c3
cmpl $0x0, 0x54(%rsp)
je 0x164171
cmpq $0x0, 0x20(%rsp)
jne 0x1641c3
movq (%rsp), %rax
movq (%rax), %rax
movl 0xc(%rsp), %edx
movl %edx, %ecx
incl %edx
movl %edx, 0xc(%rsp)
imulq $0x1c, %rcx, %rcx
movl %r12d, %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movl 0x18(%rax,%rcx), %esi
movl $0xc0000000, %edi       # imm = 0xC0000000
andl %edi, %esi
orl %edx, %esi
movl %esi, 0x18(%rax,%rcx)
movl 0x14(%rbp,%r13), %eax
subl 0x10(%rbp,%r13), %eax
testb $0x40, 0x1b(%r14)
jne 0x1641be
xorl %ecx, %ecx
btl $0x1e, 0x18(%rbp,%r13)
setae %cl
addq %rax, 0x60(%rsp,%rcx,8)
jmp 0x1641c3
movq 0x108(%rsp), %rax
movq (%rax,%r12,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq -0x80000000(%rcx), %rdx
negq %rcx
testq %rax, %rax
cmovsq %rdx, %rcx
addq 0x88(%rsp), %rcx
movq 0x10(%rsp), %rax
cmpq %rax, %rcx
cmovgq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmovgq %r12, %rax
movq %rax, 0x18(%rsp)
jmp 0x1641c3
addq %rax, 0x60(%rsp)
decq %r12
addq $-0x1c, %r13
jmp 0x164073
movq 0x20(%rsp), %r12
testq %r12, %r12
jne 0x164256
cmpl $0x0, 0xc(%rsp)
movq 0x108(%rsp), %r13
movq 0xf8(%rsp), %rbp
movq (%rsp), %rbx
je 0x16426a
movq 0x18(%rsp), %r15
testq %r15, %r15
js 0x16426f
movl 0x18(%r14), %eax
shrl $0x1e, %eax
andl $0x1, %eax
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movl 0x14(%rsp), %r8d
leaq 0x68(%rsp), %r9
movsd 0x50(%rsp), %xmm0
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq %rax
pushq 0x60(%rsp)
pushq %r15
callq 0x163454
addq $0x30, %rsp
movq 0x10(%rsp), %rdx
addq %rax, %rdx
incl 0x2c(%rsp)
jmp 0x164274
movq 0x108(%rsp), %r13
movq 0xf8(%rsp), %rbp
movq (%rsp), %rbx
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %rdx
movl %edx, %eax
movl $0x80000000, %ecx       # imm = 0x80000000
orl %ecx, %eax
movl %edx, %ecx
negl %ecx
testq %rdx, %rdx
cmovnsq %rax, %rcx
shlq $0x20, %rcx
testq %r15, %r15
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmovsq %rax, %r15
orq %rcx, %r15
movq 0x58(%rsp), %rcx
movq %r15, (%r13,%rcx,8)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
andq %rax, %r15
orq %rcx, %r15
movq %r15, (%rbp,%rcx,8)
incq %rcx
jmp 0x163f61
cmpl $0x0, 0x2c(%rsp)
je 0x1642ee
xorl %ecx, %ecx
cmpq %rax, %rcx
jge 0x1642ee
movq (%rbp,%rcx,8), %rdx
testl %edx, %edx
jns 0x1642e9
addq $-0x80000000, %rdx      # imm = 0x80000000
movq %rdx, (%rbp,%rcx,8)
movq 0x8(%rbx), %rax
incq %rcx
jmp 0x1642cb
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x156b0d
movq 0x8(%rbx), %r15
xorl %edi, %edi
pushq $-0x1
popq %rcx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq $0x0, (%rsp)
movl $0x80000000, %r9d       # imm = 0x80000000
imulq $0x1c, %rdi, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
testq %r15, %r15
jle 0x16447a
movq %r15, %r11
movl -0x8(%rbp,%r15,8), %r14d
movq (%rbx), %rax
imulq $0x1c, %r14, %r10
testb $0x40, 0x1b(%rax,%r10)
jne 0x164353
movq %rdi, %r10
jmp 0x1643d6
movq 0x20(%rsp), %r15
movq %rdi, %r10
movq %rcx, %r8
testq %r14, %r14
js 0x1643d3
testb $-0x80, 0x3(%r13,%r14,8)
jne 0x1643d6
movq (%rbx), %r12
imulq $0x1c, %r14, %r13
testb $0x40, 0x1b(%r12,%r13)
jne 0x16438e
cmpq %rdi, %r10
jle 0x164391
testb $0x40, -0x1(%r15)
cmovneq %r14, %r8
cmovneq %r10, %rsi
jmp 0x164391
movq %rcx, %r8
incq %r10
movups (%r12,%r13), %xmm0
movups 0xc(%r12,%r13), %xmm1
movups %xmm1, 0xc(%r15)
movups %xmm0, (%r15)
movq 0x108(%rsp), %r13
movq (%r13,%r14,8), %rax
movq %rax, %r12
orq %r9, %r12
movq %r12, (%r13,%r14,8)
andq %rdx, %rax
cmpq %rdx, %rax
cmoveq %rcx, %rax
addq $0x1c, %r15
movq %rax, %r14
jmp 0x16435e
movq %rcx, %r14
leaq -0x1(%r11), %r15
cmpq %r10, %rdi
je 0x16432b
testq %r8, %r8
cmovnsq %r8, %r14
cmovnsq %rsi, %r10
movq %r10, %r12
subq %rdi, %r12
je 0x16432b
movq -0x8(%rbp,%r11,8), %rax
movq %rax, %r11
shrq $0x20, %r11
leaq -0x80000000(%r11), %r13
negq %r11
testq %rax, %rax
cmovsq %r13, %r11
testq %r14, %r14
js 0x164443
movq 0x108(%rsp), %rax
movq (%rax,%r14,8), %rax
movq %rax, %r14
shrq $0x20, %r14
leaq -0x80000000(%r14), %r13
negq %r14
testq %rax, %rax
cmovsq %r13, %r14
subq %r14, %r11
testq %r11, %r11
movq 0x108(%rsp), %r13
js 0x16432b
shlq $0x20, %r11
orq %r12, %r11
movq 0x100(%rsp), %rax
movq (%rsp), %rdi
movq %r11, (%rax,%rdi,8)
incq %rdi
movq %rdi, (%rsp)
movq %r10, %rdi
jmp 0x16431d
movq 0x30(%rsp), %rax
leaq -0x1c(%rax), %rcx
xorl %edx, %edx
pushq $-0x1
popq %rax
xorl %r14d, %r14d
cmpq (%rsp), %rdx
je 0x1645aa
movq 0x100(%rsp), %rsi
movq (%rsi,%rdx,8), %r8
movl %r8d, %edi
addq %r14, %rdi
movq %r8, %rsi
shrq $0x20, %rsi
movq (%rbx), %r10
imulq $0x1c, %rdx, %r9
movl %esi, (%r10,%r9)
movl %r14d, 0x10(%r10,%r9)
movl %edi, 0x14(%r10,%r9)
shrl %r8d
orl $-0x1, 0x4(%r10,%r9)
imulq $0x1c, %r14, %r14
movq 0x30(%rsp), %rsi
leaq (%rsi,%r14), %r11
movl 0x8(%rsi,%r14), %esi
movl %esi, 0x8(%r10,%r9)
imulq $0x1c, %r8, %r15
addq %rcx, %r14
movq %r11, %r12
movl %eax, %esi
testq %r15, %r15
je 0x16457e
movups (%r12), %xmm0
movups 0xc(%r12), %xmm1
movups %xmm1, 0x9c(%rsp)
movaps %xmm0, 0x90(%rsp)
movups (%r14,%r15), %xmm0
movups 0xc(%r14,%r15), %xmm1
movups %xmm1, 0xc(%r12)
movups %xmm0, (%r12)
movups 0x9c(%rsp), %xmm0
movups %xmm0, 0xc(%r14,%r15)
movaps 0x90(%rsp), %xmm0
movups %xmm0, (%r14,%r15)
movq (%rbx), %r10
leaq (%r10,%r9), %r13
addq $0x4, %r13
movl (%r13), %esi
movl 0x4(%r12), %ebp
cmpl %ebp, %esi
jbe 0x164562
movl %ebp, (%r13)
movl %ebp, %esi
movl 0x4(%r14,%r15), %ebp
cmpl %ebp, %esi
jbe 0x164571
movl %ebp, (%r13)
movl %ebp, %esi
addq $-0x1c, %r15
addq $0x1c, %r12
jmp 0x1644ef
movq 0x100(%rsp), %r14
cmpl (%r14,%rdx,8), %r8d
jae 0x16459f
imulq $0x1c, %r8, %r8
movl 0x4(%r11,%r8), %r8d
cmpl %r8d, %esi
jae 0x16459f
movl %r8d, 0x4(%r10,%r9)
incq %rdx
movq %rdi, %r14
jmp 0x16448b
movq (%rsp), %rax
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
imulq $0x1c, %rax, %rsi
addq %rdi, %rsi
callq 0x15775d
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq