// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/inter.cpp
// Function: connect_detect(ul_ov_t*, long, long, long, All_reads*, int*, ul_idx_t const*, ug_opt_t const*, long, double, long*, int*, rtrace_iter*, kv_rtrace_t*, char*, UC_Read*, overlap_region_alloc*, bit_extz_t*, double, long, unsigned int*)
int64_t connect_detect(ul_ov_t *a, int64_t a_n, int64_t ai, int64_t aj, All_reads *ridx, int32_t *rch, 
const ul_idx_t *uref, const ug_opt_t *uopt, int64_t bw, double diff, int64_t *p, int32_t *f, rtrace_iter *tc,
kv_rtrace_t *trace, char* qstr, UC_Read *tu, overlap_region_alloc* ol, bit_extz_t *exz, double e_rate, 
int64_t trans_sc, uint32_t *is_fc)
{
	ul_ov_t *li = &(a[ai]), *lj = &(a[aj]), *lk;
	uint32_t li_v, lj_v, lk_v, lqs = INT32_MAX; int64_t qo, is_c, ak, afk, err, sc = INT32_MIN, wsc;
	li_v = (li->tn<<1)|li->rev; lj_v = (lj->tn<<1)|lj->rev; (*is_fc) = 0;
	if(li_v == lj_v) return INT32_MIN;
	//even this pair has a overlap, its length will be very small; just ignore
	if(lj->qe <= li->qs) return INT32_MIN;
	// if(lj->qs >= li->qs) continue;///no contain
	
	if((rch[aj]>>2) != ai) {
		qo = infer_rovlp(li, lj, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
		rch[aj] = (ai<<2); rch[aj] += 3;
		// fprintf(stderr, "[j::%ld] (id::%u) %.*s\tqo::%ld\n", aj, lj->tn, 
        //             (int)Get_NAME_LENGTH(R_INF, a[aj].tn), Get_NAME(R_INF, a[aj].tn), qo);
		if(get_ecov_contain_adv(uref, uopt, li_v^1, lj_v^1, bw, diff, qo, &is_c)) {
			rch[aj] = (ai<<2); rch[aj] += is_c; if(is_c) lj->el = 0;
		} else {
			// if(li->tn == 20171) {
			// 	fprintf(stderr, "[j::%ld] %.*s\tconnect::0\n", aj,
			// 			(int)Get_NAME_LENGTH(R_INF, a[aj].tn), Get_NAME(R_INF, a[aj].tn));
			// }
			return INT32_MIN;
		}
	}
	// if(li->tn == 20171) {
	// 	fprintf(stderr, "[j::%ld] %.*s\tconnect::%u\n", aj,
	// 			(int)Get_NAME_LENGTH(R_INF, a[aj].tn), Get_NAME(R_INF, a[aj].tn), rch_connect(rch[aj], ai));
	// }
	
	if(!rch_connect(rch[aj], ai)) return INT32_MIN;
	is_c = rch[aj]&1; ak = afk = aj;
	// fprintf(stderr, "+[j::%ld] %.*s\tis_c::%ld\n", aj,
    //                 (int)Get_NAME_LENGTH(R_INF, a[aj].tn), Get_NAME(R_INF, a[aj].tn), is_c);
	if(is_c) {
		lqs = a[aj].qs;
		for (ak = p[aj]; ak >= 0; ak = p[ak]) {
			if((rch[ak]>>2) != ai) {
				rch[ak] = (ak<<2); rch[ak] += 3; 
				lk = &(a[ak]); lk_v = (lk->tn<<1)|lk->rev;
				if(li_v == lk_v) break;
				if(lk->qe <= li->qs) break;
				qo = infer_rovlp(li, lk, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
				if(!get_ecov_contain_adv(uref, uopt, li_v^1, lk_v^1, bw, diff, qo, &is_c)) break;
				rch[ak] = (ai<<2); rch[ak] += is_c;
			}
			if(!rch_connect(rch[ak], ai)) break;
			if(lqs > a[ak].qs) lqs = a[ak].qs;
			is_c = rch[ak]&1; if(is_c == 0) break;
		}

		afk = ak;
		if((ak >= 0) && (!rch_connect(rch[ak], ai))) return INT32_MIN;///go to a disconnected node
		// if(ak >= 0) {
		// 	if(!rch_connect(rch[ak], ai)) return INT32_MIN;///go to a disconnected node
		// 	for (ak = p[ak]; ak >= 0 && a[afk].qe <= a[ak].qe + 256; ak = p[ak]) {
		// 		if((rch[ak]>>2) != ai) {
		// 			rch[ak] = (ak<<2); rch[ak] += 3; 
		// 			lk = &(a[ak]); lk_v = (lk->tn<<1)|lk->rev;
		// 			if(li_v == lk_v) return INT32_MIN;
		// 			if(lk->qe <= li->qs) return INT32_MIN;
		// 			qo = infer_rovlp(li, lk, NULL, NULL, ridx, NULL); ///overlap length in query (UL read)
		// 			if(!get_ecov_contain_adv(uref, uopt, li_v^1, lk_v^1, bw, diff, qo, &is_c)) return INT32_MIN;
		// 			rch[ak] = (ai<<2); rch[ak] += is_c;
		// 		}
		// 		if(!rch_connect(rch[ak], ai)) return INT32_MIN;
		// 	}
		// }
	}
	// if(li->tn == 6317) {
	// 	fprintf(stderr, "[M::%s::] [j::%ld] %.*s\tis_c::%ld\tafk::%ld\tlqs::%u\n", __func__, aj,
	// 					(int)Get_NAME_LENGTH(R_INF, a[aj].tn), Get_NAME(R_INF, a[aj].tn), is_c, afk, lqs);
	// }
	///afk >=0: reach to one non-contained read, lqs should <= li->qs
	///afk < 0: all reads are contained, lqs >= li->qs
	if((afk >= 0) || (lqs <= li->qs)) {
		lj = &(a[aj]);
		err = get_rid_backward_cigar_err(tc, li, trace, NULL, uref, qstr, tu, ol, NULL, exz, e_rate, lj->qe);
		sc = f[aj] + (li->qe - lj->qe) - (err*trans_sc); (*is_fc) = 0;
		if(afk < 0) {
			wsc = li->qe - li->qs; wsc -= (((int64_t)li->sec)*trans_sc);
			if(sc < wsc) sc = wsc;
		}
	} else {
		sc = li->qe - li->qs; sc -= (((int64_t)li->sec)*trans_sc); (*is_fc) = 1;
	}
	return sc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %rbx
addq %rdx, %rbx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %r15
addq %rcx, %r15
movl 0xc(%rdi,%rbx), %eax
movl 0x18(%rdi,%rbx), %r14d
shrdl $0x1f, %eax, %r14d
movl 0xc(%rdi,%r15), %eax
movl 0x18(%rdi,%r15), %ebp
shrdl $0x1f, %eax, %ebp
movq 0xe0(%rsp), %r13
movl $0x0, (%r13)
movq $-0x80000000, %rsi      # imm = 0x80000000
cmpl %ebp, %r14d
je 0x1be0e2
movq %rdi, %r12
addq %rdi, %rbx
addq %rdi, %r15
movl 0x8(%r15), %eax
cmpl 0x4(%rbx), %eax
jbe 0x1be0e2
movq %rdx, %r10
movl (%r9,%rcx,4), %eax
movl %eax, %edx
sarl $0x2, %edx
movslq %edx, %rdx
cmpq %r10, %rdx
movq %rcx, 0x30(%rsp)
je 0x1bde18
movsd %xmm1, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r10, %r13
movq %r8, 0x28(%rsp)
movq %r9, 0x18(%rsp)
xorl %r9d, %r9d
movsd %xmm0, 0x20(%rsp)
callq 0x6ea22
movq %r13, 0x8(%rsp)
leal 0x3(,%r13,4), %ecx
movq 0x18(%rsp), %r13
movq 0x30(%rsp), %rdx
movl %ecx, (%r13,%rdx,4)
movl %r14d, %edx
xorl $0x1, %edx
xorl $0x1, %ebp
subq $0x8, %rsp
leaq 0x8(%rsp), %rdi
movq 0x90(%rsp), %rsi
movl %ebp, %ecx
movq 0x98(%rsp), %r8
movsd 0x28(%rsp), %xmm0
movq %rax, %r9
pushq %rdi
callq 0x1bcf0f
addq $0x10, %rsp
testq %rax, %rax
je 0x1be0db
movq 0x8(%rsp), %r10
leal (,%r10,4), %eax
movq (%rsp), %rcx
addl %ecx, %eax
movq 0x30(%rsp), %rdx
movl %eax, (%r13,%rdx,4)
testq %rcx, %rcx
movq %rdx, %rcx
movsd 0x10(%rsp), %xmm1
movq $-0x80000000, %rsi      # imm = 0x80000000
movq %r13, %r9
movsd 0x20(%rsp), %xmm0
movq 0x28(%rsp), %r8
movq 0xe0(%rsp), %r13
je 0x1bde18
andb $-0x41, 0x1b(%r15)
movl (%r9,%rcx,4), %eax
movl %eax, %edx
notl %edx
testb $0x3, %dl
je 0x1be0e2
movl %eax, %edx
sarl $0x2, %edx
movslq %edx, %rdx
cmpq %r10, %rdx
jne 0x1be0e2
andl $0x1, %eax
movq %rax, (%rsp)
jne 0x1bde52
testq %rcx, %rcx
jns 0x1be035
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jmp 0x1bdfc6
movsd %xmm0, 0x20(%rsp)
movq 0x98(%rsp), %rsi
movl 0x4(%r15), %edi
movl %r14d, %eax
xorl $0x1, %eax
movl %eax, 0x40(%rsp)
leal (,%r10,4), %eax
movl %eax, 0x3c(%rsp)
movq %rcx, %r13
movsd %xmm1, 0x10(%rsp)
movq %r10, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x28(%rsp)
movq (%rsi,%r13,8), %r13
testq %r13, %r13
js 0x1bdfbe
movl (%r9,%r13,4), %eax
movl %eax, %ecx
sarl $0x2, %ecx
movslq %ecx, %rdx
cmpq %r10, %rdx
je 0x1bdf7a
leal 0x3(,%r13,4), %eax
movl %eax, (%r9,%r13,4)
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rsi
addq %r13, %rsi
movl 0xc(%r12,%rsi), %ecx
movl 0x18(%r12,%rsi), %ebp
shrdl $0x1f, %ecx, %ebp
cmpl %ebp, %r14d
je 0x1be00b
addq %r12, %rsi
movl 0x8(%rsi), %ecx
cmpl 0x4(%rbx), %ecx
jbe 0x1be00b
movl %edi, 0x44(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
callq 0x6ea22
xorl $0x1, %ebp
subq $0x8, %rsp
movq 0x90(%rsp), %rsi
movl 0x48(%rsp), %edx
movl %ebp, %ecx
movq 0x98(%rsp), %r8
movsd 0x28(%rsp), %xmm0
movq %rax, %r9
leaq 0x8(%rsp), %rax
pushq %rax
callq 0x1bcf0f
addq $0x10, %rsp
testq %rax, %rax
je 0x1bdff7
movl (%rsp), %eax
addl 0x3c(%rsp), %eax
movq 0x18(%rsp), %r9
movl %eax, (%r9,%r13,4)
movl %eax, %ecx
sarl $0x2, %ecx
movsd 0x10(%rsp), %xmm1
movq 0x8(%rsp), %r10
movq 0x28(%rsp), %r8
movl 0x44(%rsp), %edi
movq 0x98(%rsp), %rsi
movl %eax, %edx
notl %edx
testb $0x3, %dl
je 0x1be00b
movslq %ecx, %rcx
cmpq %r10, %rcx
jne 0x1be00b
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
movl 0x4(%r12,%rcx), %ecx
cmpl %ecx, %edi
cmovael %ecx, %edi
movl %eax, %ecx
andl $0x1, %ecx
movq %rcx, (%rsp)
testb $0x1, %al
jne 0x1bde92
jmp 0x1be00b
movq 0xe0(%rsp), %r13
movl 0x4(%rbx), %eax
movb $0x1, %bpl
cmpl %eax, %edi
jbe 0x1be037
movl 0x8(%rbx), %esi
movl 0x18(%rbx), %ecx
subl %eax, %esi
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
imulq 0xd8(%rsp), %rcx
subq %rcx, %rsi
movl $0x1, (%r13)
jmp 0x1be0e2
movq 0x18(%rsp), %rax
movl (%rax,%r13,4), %eax
movsd 0x10(%rsp), %xmm1
movq 0x8(%rsp), %r10
movl %eax, %ecx
notl %ecx
testb $0x3, %cl
movq 0xe0(%rsp), %r13
je 0x1be0db
sarl $0x2, %eax
cltq
cmpq %r10, %rax
movq $-0x80000000, %rsi      # imm = 0x80000000
jne 0x1be0e2
xorl %ebp, %ebp
movq 0xb8(%rsp), %r9
movq 0xb0(%rsp), %rdx
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %r14
movq 0x80(%rsp), %r8
movl 0x8(%r15), %eax
subq $0x8, %rsp
movq %rbx, %rsi
xorl %ecx, %ecx
movaps %xmm1, %xmm0
pushq %rax
pushq 0xe0(%rsp)
pushq $0x0
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x7fea8
addq $0x30, %rsp
movl 0x8(%rbx), %esi
movq 0x30(%rsp), %rcx
addl (%r14,%rcx,4), %esi
subl 0x8(%r15), %esi
movq 0xd8(%rsp), %rdx
imulq %rdx, %rax
subq %rax, %rsi
movl $0x0, (%r13)
testb %bpl, %bpl
je 0x1be0e2
movl 0x8(%rbx), %eax
movl 0x18(%rbx), %ecx
subl 0x4(%rbx), %eax
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
imulq %rdx, %rcx
subq %rcx, %rax
cmpq %rax, %rsi
cmovleq %rax, %rsi
jmp 0x1be0e2
movq $-0x80000000, %rsi      # imm = 0x80000000
movq %rsi, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq