// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq $0x80, -0x70(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
cmpl $0x0, -0x2c(%rbp)
jg 0x2925e4
movq $0x0, -0x8(%rbp)
jmp 0x292aaa
movq $0x0, -0x60(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jge 0x2926da
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x50(%rbp), %rdx
addq %rdx, %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movl %eax, %eax
movl %eax, %edx
movabsq $0x80000000, %rax       # imm = 0x80000000
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
jne 0x29267e
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x50(%rbp), %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
movabsq $0x80000000, %rdx       # imm = 0x80000000
orq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x2926c7
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x50(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x60(%rbp)
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl %edx, %ecx
movl %ecx, 0x18(%rax)
jmp 0x2926c9
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2925fc
movq -0x80(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jne 0x2926ff
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
subq 0x8(%rcx), %rax
movq %rax, -0x8(%rbp)
jmp 0x292aaa
cmpq $0x0, -0x60(%rbp)
jle 0x292708
jmp 0x292727
leaq 0xfeb16(%rip), %rdi     # 0x391225
leaq 0xfe119(%rip), %rsi     # 0x39082f
movl $0xe52, %edx            # imm = 0xE52
leaq 0xfeb08(%rip), %rcx     # 0x39122a
callq 0x2130
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
movq -0x60(%rbp), %rdx
subq $0x1, %rdx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x68(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
cmpq $0x0, -0x48(%rbp)
jl 0x292a46
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x48(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x90(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
jne 0x2927d3
movq $-0x1, -0x48(%rbp)
jmp 0x2927e9
movq -0x18(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq %rax, -0x48(%rbp)
movq -0x90(%rbp), %rax
movl 0x8(%rax), %eax
addq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x29280a
movq -0x80(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
jne 0x29280f
jmp 0x292a46
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x88(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x60(%rbp), %rax
jge 0x292a41
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x50(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
cmpl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jne 0x292853
jmp 0x292a30
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
imulq $0x1c, -0x50(%rbp), %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl 0xc(%rax), %eax
shll %eax
movq -0x98(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1f, %ecx
orl %ecx, %eax
movl %eax, -0xa0(%rbp)
movq -0x90(%rbp), %rax
movl 0x8(%rax), %eax
addq -0x70(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jl 0x292a2e
cmpq $0x0, -0x88(%rbp)
jne 0x2928d6
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x98(%rbp), %rcx
movl 0x4(%rcx), %ecx
addq -0x70(%rbp), %rcx
cmpq %rcx, %rax
jg 0x292a23
movq -0x90(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq 0x28(%rbp), %r8
movq 0x30(%rbp), %r9
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x71a90
movq %rax, -0x78(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0xa0(%rbp), %eax
je 0x292a21
movq -0x10(%rbp), %rsi
movl -0x9c(%rbp), %edx
xorl $0x1, %edx
movl -0xa0(%rbp), %ecx
xorl $0x1, %ecx
movq 0x18(%rbp), %r8
movsd -0x40(%rbp), %xmm0
movq -0x78(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
leaq -0xa4(%rbp), %rax
movq %rax, (%rsp)
callq 0x2918c0
cmpq $0x0, %rax
je 0x292a21
cmpl $0x0, -0xa4(%rbp)
je 0x292a1f
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
imulq $0x1c, -0x50(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
andl $0xc0000000, %ecx       # imm = 0xC0000000
orl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
movl %ecx, 0x18(%rax)
cmpl $0x0, 0x20(%rbp)
je 0x2929d7
movq -0x98(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x98(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %edx
movq -0x38(%rbp), %rax
movq (%rax), %rcx
subq %rdx, %rcx
movq %rcx, (%rax)
jmp 0x292a1d
movq -0x98(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x98(%rbp), %rcx
subl 0x10(%rcx), %eax
movl %eax, %eax
movl %eax, %esi
movq -0x38(%rbp), %rax
movq -0x98(%rbp), %rcx
movl 0x18(%rcx), %ecx
shrl $0x1e, %ecx
andl $0x1, %ecx
cmpl $0x0, %ecx
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movq (%rax,%rcx,8), %rdx
subq %rsi, %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x292a1f
jmp 0x292a21
jmp 0x292a23
movq $0x1, -0x88(%rbp)
jmp 0x292a30
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x292822
jmp 0x29276d
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, (%rcx)
movb %al, -0xa5(%rbp)
jl 0x292a6a
movq -0x38(%rbp), %rax
cmpq $0x0, 0x8(%rax)
setge %al
movb %al, -0xa5(%rbp)
movb -0xa5(%rbp), %al
testb $0x1, %al
jne 0x292a76
jmp 0x292a78
jmp 0x292a97
leaq 0xfe857(%rip), %rdi     # 0x3912d6
leaq 0xfdda9(%rip), %rsi     # 0x39082f
movl $0xe6d, %edx            # imm = 0xE6D
leaq 0xfe798(%rip), %rcx     # 0x39122a
callq 0x2130
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
subq 0x8(%rcx), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)