// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: collect_nc_ovlps(ul_vec_t*, unsigned int, kv_ul_ov_t*, ug_opt_t const*, asg_t const*, R_to_U*, long, unsigned long*, unsigned long*)
void collect_nc_ovlps(ul_vec_t *rch, uint32_t rch_i, kv_ul_ov_t *res, const ug_opt_t *uopt, const asg_t *rg, R_to_U *ri, int64_t ulid, uint64_t *mqs, uint64_t *mqe)
{
	uint64_t i, occ, nc, k; ma_hit_t_alloc* src; 
	int64_t min_ovlp = uopt->min_ovlp, max_hang = uopt->max_hang; ul_ov_t p;
	res->n = occ = nc = 0; (*mqs) = (*mqe) = (uint64_t)-1;
	i = rch_i;
	// for (i = rch_i; (i != (uint32_t)-1) && (!rg->seq[rch->bb.a[i].hid].del); i = rch->bb.a[i].pidx) {
	while(i != (uint32_t)-1) {
		if(rg->seq[rch->bb.a[i].hid].del) break;
		// if(ulid == 442462) {		
		// 	fprintf(stderr, "\n>>>[M::%s::i->%lu::id->%u::%.*s]\n", __func__, i, rch->bb.a[i].hid, 
		// 		(int)Get_NAME_LENGTH(R_INF, rch->bb.a[i].hid), Get_NAME(R_INF, rch->bb.a[i].hid));
		// 	fprintf(stderr, "*[M::%s::id->%u::%.*s]\tq::[%u, %u),\tt::[%u, %u),\tis_rev::%u,\tis_cr::%u\n", 
		// 			__func__, rch->bb.a[i].hid, 
		// 			(int)Get_NAME_LENGTH(R_INF, rch->bb.a[i].hid), Get_NAME(R_INF, rch->bb.a[i].hid), 
		// 			rch->bb.a[i].qs, rch->bb.a[i].qe, rch->bb.a[i].ts, rch->bb.a[i].te, rch->bb.a[i].rev,
		// 			!!(is_contain_r((*(ri)), rch->bb.a[i].hid)));
		// }
		
		if(is_contain_r((*ri), rch->bb.a[i].hid)) {
			src = &(uopt->sources[rch->bb.a[i].hid]);
			for (k = 0; k < src->length; k++) {
				if(rg->seq[Get_tn(src->buffer[k])].del) continue;///tn must exist
				// if(!extract_ccov(&(src->buffer[k]), &(rch->bb.a[i]), rg, &p, 1, min_ovlp, max_hang)) continue;
				// if(ulid == 442462) {	
				// 	fprintf(stderr, "+[M::%s::id->%u::%.*s]\tq::[%u, %u),\tql::%u,\tt::[%u, %u),\ttl::%u,\tis_rev::%u,\tis_cr::%u\n", 
				// 	__func__, src->buffer[k].tn, 
				// 	(int)Get_NAME_LENGTH(R_INF, src->buffer[k].tn), Get_NAME(R_INF, src->buffer[k].tn), 
				// 	Get_qs(src->buffer[k]), Get_qe(src->buffer[k]), rg->seq[Get_qn(src->buffer[k])].len,
				// 	Get_ts(src->buffer[k]), Get_te(src->buffer[k]), rg->seq[Get_tn(src->buffer[k])].len,
				// 	src->buffer[k].rev, !!(is_contain_r((*(ri)), src->buffer[k].tn)));
				// }
				if(!extract_nccov(&(src->buffer[k]), &(rch->bb.a[i]), rg, &p, 1, min_ovlp, max_hang)) continue;
				// if(ulid == 442462) {	
				// 	fprintf(stderr, "-[M::%s::id->%u::%.*s]\tq::[%u, %u),\tt::[%u, %u),\tis_rev::%u,\tis_cr::%u\n", 
				// 	__func__, p.tn, (int)Get_NAME_LENGTH(R_INF, p.tn), Get_NAME(R_INF, p.tn), 
				// 	p.qs, p.qe, p.ts, p.te, p.rev, !!(is_contain_r((*(ri)), p.tn)));
				// 	// fprintf(stderr, "cc[M::%s::id->%u::%.*s] q::[%u, %u), t::[%u, %u), is_cr::%u, del::%u\n", 
				// 	// __func__, p.tn, (int)Get_NAME_LENGTH(R_INF, p.tn), Get_NAME(R_INF, p.tn), 
				// 	// p.qs, p.qe, p.ts, p.te, !!(is_contain_r((*(ri)), p.tn)), rg->seq[p.tn].del);
				// }
				p.el = 0; p.tn <<= 1; p.tn |= p.rev; p.qn = i;//for linear chain
            	kv_push(ul_ov_t, *res, p);
			}
			nc++;
		}
		///push itself into the chain
		memset(&p, 0, sizeof(p));
		p.qn = 0; p.qs = rch->bb.a[i].qs; p.qe = rch->bb.a[i].qe; 
    	p.tn = rch->bb.a[i].hid; p.ts = rch->bb.a[i].ts; p.te = rch->bb.a[i].te; 
    	p.el = 1; p.rev = rch->bb.a[i].rev;
		if(p.rev) {///for linear chaining
			p.ts = rg->seq[p.tn].len - rch->bb.a[i].te;
			p.te = rg->seq[p.tn].len - rch->bb.a[i].ts;
		}
		p.el = 1; p.tn <<= 1; p.tn |= p.rev; p.qn = i;//for linear chain
		kv_push(ul_ov_t, *res, p);
		if(((*mqs) == ((uint64_t)-1)) || ((*mqs) > rch->bb.a[i].qs)) (*mqs) = rch->bb.a[i].qs;
		if(((*mqe) == ((uint64_t)-1)) || ((*mqe) < rch->bb.a[i].qe)) (*mqe) = rch->bb.a[i].qe;
		occ++; 

		i = rch->bb.a[i].pidx;
		if(i != ((uint32_t)-1)) {
			if(!is_rg_connect(rg, (uint64_t)((rch->bb.a[i].hid<<1)|(rch->bb.a[i].rev)), 
					(uint64_t)((rch->bb.a[rch->bb.a[i].aidx].hid<<1)|(rch->bb.a[rch->bb.a[i].aidx].rev)))) {
				i = ((uint32_t)-1);
			}
		}
	}
	assert(occ > 1 && nc > 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r15
movq %r8, %r14
movq %rcx, 0x40(%rsp)
movq %rdx, %r12
movq %rdi, 0x8(%rsp)
andq $0x0, 0x8(%rdx)
movq 0xa0(%rsp), %r13
orq $-0x1, (%r13)
movq 0x98(%rsp), %rax
orq $-0x1, (%rax)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %esi, %edi
xorl %ebp, %ebp
movq $0x0, 0x30(%rsp)
movq %r9, 0x48(%rsp)
cmpq %rcx, %rdi
je 0x18dcbf
movq 0x20(%r14), %rdx
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rcx
imulq $0x24, %rdi, %rbx
movl (%rcx,%rbx), %eax
cmpl $0x0, (%rdx,%rax,8)
js 0x18dcbf
cmpq %rax, (%r15)
jbe 0x18db54
movq 0x8(%r15), %rdx
cmpl $0x0, (%rdx,%rax,4)
js 0x18db54
movq %rdi, 0x38(%rsp)
movq %rbp, 0x50(%rsp)
imulq $0x18, %rax, %r15
movq 0x40(%rsp), %rax
addq 0x8(%rax), %r15
xorl %r13d, %r13d
xorl %ebp, %ebp
movl 0xc(%r15), %eax
cmpq %rax, %rbp
jae 0x18db2e
movq 0x20(%r14), %rax
movq (%r15), %rdi
movl 0xc(%rdi,%r13), %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x18db22
addq %r13, %rdi
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rsi
addq %rbx, %rsi
movq %r14, %rdx
leaq 0x14(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x18d7f8
testl %eax, %eax
je 0x18db22
movl 0x2c(%rsp), %eax
movl %eax, %ecx
andl $0xbfffffff, %ecx       # imm = 0xBFFFFFFF
movl %ecx, 0x2c(%rsp)
shldl $0x1, %eax, 0x20(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0x14(%rsp)
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x18db02
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r12)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r12)
imulq $0x1c, %rcx, %rcx
movups 0x14(%rsp), %xmm0
movups 0x20(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq %rbp
addq $0x28, %r13
jmp 0x18da64
movq 0x50(%rsp), %rbp
incq %rbp
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rcx
movl (%rcx,%rbx), %eax
movq 0x48(%rsp), %r15
movq 0xa0(%rsp), %r13
movq 0x38(%rsp), %rdi
movq 0x4(%rcx,%rbx), %rdx
movq %rdx, 0x18(%rsp)
movl 0xc(%rcx,%rbx), %edx
movl %edx, 0x24(%rsp)
movl 0x10(%rcx,%rbx), %esi
movl %esi, 0x28(%rsp)
movl 0x20(%rcx,%rbx), %ecx
btl $0x5, %ecx
jae 0x18dba3
movq %rdi, %r9
movq 0x20(%r14), %rdi
movl %eax, %r8d
movl (%rdi,%r8,8), %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edi
movl %edi, %r8d
subl %esi, %r8d
movl %r8d, 0x24(%rsp)
subl %edx, %edi
movl %edi, 0x28(%rsp)
movq %r9, %rdi
andl $-0x20, %ecx
shll $0x1a, %ecx
leal 0x40000000(%rcx), %edx
movl %edx, 0x2c(%rsp)
shldl $0x1, %ecx, %eax
movl %eax, 0x20(%rsp)
movl %edi, 0x14(%rsp)
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq 0x10(%r12), %rcx
jne 0x18dbf7
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x10(%r12)
imulq $0x1c, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r12)
movq 0x8(%r12), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r12)
imulq $0x1c, %rcx, %rcx
movups 0x14(%rsp), %xmm0
movups 0x20(%rsp), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq 0x98(%rsp), %rax
movq (%rax), %rdx
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movl 0x4(%rax,%rbx), %ecx
cmpq $-0x1, %rdx
je 0x18dc3a
cmpq %rcx, %rdx
jbe 0x18dc45
movq 0x98(%rsp), %rdx
movq %rcx, (%rdx)
movq (%r13), %rdx
movl 0x8(%rax,%rbx), %ecx
cmpq $-0x1, %rdx
je 0x18dc58
cmpq %rcx, %rdx
jae 0x18dc5c
movq %rcx, (%r13)
incq 0x30(%rsp)
movl 0x14(%rax,%rbx), %ebx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rbx
je 0x18da0a
imulq $0x24, %rbx, %rcx
movl 0x20(%rax,%rcx), %edx
shrl $0x5, %edx
andl $0x1, %edx
movl (%rax,%rcx), %esi
movl 0x1c(%rax,%rcx), %ecx
leal (%rdx,%rsi,2), %esi
imulq $0x24, %rcx, %rcx
movl (%rax,%rcx), %edx
movl 0x20(%rax,%rcx), %eax
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rdx,2), %edx
movq %r14, %rdi
callq 0x1a36ae
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
cmoveq %rcx, %rbx
movq %rbx, %rdi
jmp 0x18da0a
cmpq $0x2, 0x30(%rsp)
jb 0x18dcdb
testq %rbp, %rbp
je 0x18dcdb
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6791e(%rip), %rdi     # 0x1f5600
leaq 0x63e3a(%rip), %rsi     # 0x1f1b23
leaq 0x67922(%rip), %rcx     # 0x1f5612
movl $0x45df, %edx           # imm = 0x45DF
callq 0x2110