// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: determine_containment_chain(ug_opt_t const*, unsigned long*, unsigned long*, kv_ul_ov_t*, int, long*, long, long, double, unsigned int, All_reads*, ma_ug_t*)
int64_t determine_containment_chain(const ug_opt_t *uopt, uint64_t *track, uint64_t *flag, kv_ul_ov_t *res, int32_t nc, int64_t *nsc, int64_t mm_idx, int64_t bw, double diff_ec_ul, uint32_t el, All_reads *ridx, ma_ug_t *ug)
{
	int64_t i, k, pk, ak, e, off = 128, qo, tt = 0, ii; ul_ov_t *li = NULL, *lk = NULL;
	uint32_t li_v, lk_v, is_c;
	if(nc<=0) return 0;
	for (k = tt = ak = 0; k < nc; k++) {
		if(!(flag[res->a[k].sec]&((uint64_t)0x80000000))) {
			flag[res->a[k].sec] |= ((uint64_t)0x80000000); tt++;
		} else {
			res->a[ak++].sec = res->a[k].sec;
		}
	}
	if(tt==nc) return nsc[0] - nsc[1];
	assert(ak>0);
	e = res->a[res->a[ak-1].sec].qe;//e is the smallest qe
	for (i = mm_idx, pk = 0; i >= 0;) {///i++, li->qe--
		li = &(res->a[i]); li_v = (li->tn<<1)|li->rev;
		if((track[i]&((uint64_t)0x7FFFFFFF)) == ((uint64_t)0x7FFFFFFF)) i = -1;
		else i = track[i]&((uint64_t)0x7FFFFFFF);
		if(li->qe + off < e || tt == nc) break;//128 is used to tolerate indels; 
		for (k = pk, ii = 0; k < ak; k++) {///k++, lk->qe--
			if(res->a[k].sec == ((uint32_t)0x3FFFFFFF)) continue;
			lk = &(res->a[res->a[k].sec]); lk_v = (lk->tn<<1)|lk->rev;
			if(li->qe + off >= lk->qe) {
				if(ii == 0) pk = k;
				if(li->qs <= lk->qs + off) {
					qo = infer_rovlp(li, lk, NULL, NULL, ridx, ug); ///overlap length in query (UL read)
					if(li_v != lk_v && get_ecov_adv_back(NULL, uopt, li_v^1, lk_v^1, bw, diff_ec_ul, qo, &is_c)) {
						if(is_c) {
							tt++; res->a[k].sec = ((uint32_t)0x3FFFFFFF);
							if(el) nsc[0] -= ((int64_t)(lk->te-lk->ts));
							else nsc[!(lk->el)] -= ((int64_t)(lk->te-lk->ts));
						}
					}
				}
				ii = 1;
			}
		}
	}

	assert(nsc[0]>=0 && nsc[1]>=0);
	return nsc[0] - nsc[1];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm0, 0x70(%rsp)
movq %r9, 0x10(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, 0x68(%rsp)
testl %r8d, %r8d
jle 0x163500
movq 0xb0(%rsp), %rbx
movl %r8d, %eax
movq %rax, 0x40(%rsp)
imulq $0x1c, %rax, %rax
xorl %r11d, %r11d
movl $0xc0000000, %esi       # imm = 0xC0000000
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq %r11, %rax
je 0x1634e8
movq (%rcx), %r8
movl 0x18(%r8,%r11), %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
movq (%rdx,%rdi,8), %r9
testl %r9d, %r9d
js 0x1634cb
btsq $0x1f, %r9
movq %r9, (%rdx,%rdi,8)
incq %r14
jmp 0x1634e2
imulq $0x1c, %rbp, %r9
incq %rbp
movl 0x18(%r8,%r9), %r10d
andl %esi, %r10d
orl %edi, %r10d
movl %r10d, 0x18(%r8,%r9)
addq $0x1c, %r11
jmp 0x1634a1
cmpq 0x40(%rsp), %r14
jne 0x163507
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
subq 0x8(%rcx), %rax
jmp 0x163727
xorl %eax, %eax
jmp 0x163727
testq %rbp, %rbp
jle 0x163755
movq (%rcx), %rax
imulq $0x1c, %rbp, %rdx
movl -0x4(%rax,%rdx), %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
imulq $0x1c, %rdx, %rdx
movl 0x8(%rax,%rdx), %eax
movq %rax, 0x50(%rsp)
xorl %r9d, %r9d
movl $0x3fffffff, %r12d      # imm = 0x3FFFFFFF
pushq $0x1
popq %r10
movq %rcx, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
testq %rbx, %rbx
js 0x16370e
movq (%rcx), %rax
imulq $0x1c, %rbx, %rsi
movl 0x8(%rax,%rsi), %edx
movq 0x58(%rsp), %rdi
movq (%rdi,%rbx,8), %rbx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andq %rdi, %rbx
cmpq %rdi, %rbx
pushq $-0x1
popq %rdi
cmoveq %rdi, %rbx
subq $-0x80, %rdx
cmpq 0x50(%rsp), %rdx
jb 0x16370e
cmpq 0x40(%rsp), %r14
je 0x16370e
movq %r14, 0x48(%rsp)
movq %rbx, 0x60(%rsp)
leaq (%rax,%rsi), %r15
movl 0xc(%rax,%rsi), %edx
movl 0x18(%rax,%rsi), %eax
shrdl $0x1f, %edx, %eax
movl %eax, 0x38(%rsp)
xorl $0x1, %eax
movl %eax, 0x34(%rsp)
imulq $0x1c, %r9, %r14
addq $0x18, %r14
movq %r9, %rbx
xorl %eax, %eax
cmpq %rbp, %rbx
jge 0x1636ff
movq (%rcx), %r8
movl (%r8,%r14), %edx
andq %r12, %rdx
cmpq %r12, %rdx
je 0x163661
imulq $0x1c, %rdx, %rdx
movl 0x8(%r8,%rdx), %esi
movl 0x8(%r15), %edi
subq $-0x80, %rdi
cmpq %rsi, %rdi
jb 0x163661
leaq (%r8,%rdx), %r13
testq %rax, %rax
cmoveq %rbx, %r9
movl 0x4(%r15), %eax
movl 0x4(%r13), %esi
subq $-0x80, %rsi
cmpq %rax, %rsi
jae 0x163613
movq %r10, %rax
jmp 0x163661
movq %r9, 0x28(%rsp)
movl 0xc(%r8,%rdx), %eax
movl 0x18(%r8,%rdx), %ebp
shrdl $0x1f, %eax, %ebp
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0xc8(%rsp), %r8
movq 0xd0(%rsp), %r9
callq 0x3d261
cmpl %ebp, 0x38(%rsp)
jne 0x16366d
pushq $0x1
popq %r10
movq %r10, %rax
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %r9
incq %rbx
addq $0x1c, %r14
jmp 0x1635c0
xorl $0x1, %ebp
leaq 0x3c(%rsp), %rcx
movq %rcx, (%rsp)
xorl %edi, %edi
movq 0x68(%rsp), %rsi
movl 0x34(%rsp), %edx
movl %ebp, %ecx
movq 0xb8(%rsp), %r8
movsd 0x70(%rsp), %xmm0
movq %rax, %r9
callq 0x162ece
testq %rax, %rax
je 0x16364b
cmpl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %rbp
movq 0x28(%rsp), %r9
pushq $0x1
popq %r10
je 0x16360e
movq (%rcx), %rax
orl $0x3fffffff, (%rax,%r14) # imm = 0x3FFFFFFF
movq 0x10(%rsp), %rdx
movq %rdx, %rax
cmpl $0x0, 0xc0(%rsp)
jne 0x1636ea
xorl %eax, %eax
testb $0x40, 0x1b(%r13)
sete %al
leaq (%rdx,%rax,8), %rax
incq 0x48(%rsp)
movl 0x14(%r13), %edx
subl 0x10(%r13), %edx
subq %rdx, (%rax)
jmp 0x16360e
movq 0x60(%rsp), %rbx
movq 0x48(%rsp), %r14
jmp 0x163545
movq 0x10(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
js 0x163736
movq 0x8(%rcx), %rcx
testq %rcx, %rcx
js 0x163736
subq %rcx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8ee8d(%rip), %rdi     # 0x1f25ca
leaq 0x8e3df(%rip), %rsi     # 0x1f1b23
leaq 0x8edd3(%rip), %rcx     # 0x1f251e
movl $0xe6d, %edx            # imm = 0xE6D
callq 0x2110
leaq 0x8edbd(%rip), %rdi     # 0x1f2519
leaq 0x8e3c0(%rip), %rsi     # 0x1f1b23
leaq 0x8edb4(%rip), %rcx     # 0x1f251e
movl $0xe52, %edx            # imm = 0xE52
callq 0x2110