// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: refine_contain_consensus_chain(asg_t const*, ul_vec_t*, R_to_U*, asg64_v*, unsigned long)
uint32_t refine_contain_consensus_chain(const asg_t *rg, ul_vec_t *rch, R_to_U *ri, asg64_v *bu, uint64_t ulid)
{
	bu->n = 0; 
	if(rch->bb.n == 1 && rch->bb.a[0].base) return bu->n;///no alignment
	if(rch->bb.n == 0) return bu->n;///no alignment
	uint64_t i, m, nc, occ, is_end; int64_t k, kn;
	kv_resize(uint64_t, *bu, rch->bb.n); 
	memset(bu->a, -1, (sizeof((*(bu->a)))*rch->bb.n));
	for (k = rch->bb.n-1; k >= 0; k--) {
		// if(ulid == 2 || ulid == 46 || ulid == 475 || ulid == 3274 || ulid == 3360) {
		// 	fprintf(stderr, "[M::%s::] ulid::%lu, k::%ld, q::[%u, %u), t::[%u, %u), del::%u, pidx::%u, pconn::%u, aidx::%u, aconn::%u\n", 
		// 		__func__, ulid, k, 
		// 		rch->bb.a[k].qs, rch->bb.a[k].qe, rch->bb.a[k].ts, rch->bb.a[k].te, rg->seq[rch->bb.a[k].hid].del,
		// 		rch->bb.a[k].pidx, 
		// 		((rch->bb.a[k].pidx!=((uint32_t)-1))&&is_rg_connect(rg, (uint64_t)((rch->bb.a[rch->bb.a[k].pidx].hid<<1)|(rch->bb.a[rch->bb.a[k].pidx].rev)), (uint64_t)((rch->bb.a[k].hid<<1)|(rch->bb.a[k].rev))))?1:0,
		// 		rch->bb.a[k].aidx,
		// 		((rch->bb.a[k].aidx!=((uint32_t)-1))&&is_rg_connect(rg, (uint64_t)((rch->bb.a[k].hid<<1)|(rch->bb.a[k].rev)),(uint64_t)((rch->bb.a[rch->bb.a[k].aidx].hid<<1)|(rch->bb.a[rch->bb.a[k].aidx].rev))))?1:0);
		// }

		

		if(bu->a[k] != ((uint64_t)-1)) continue;
		is_end = 0;
		if((rch->bb.a[k].aidx == ((uint32_t)-1)) && (rch->bb.a[k].pidx != ((uint32_t)-1))) {///the end of a chain
			is_end = 1;
		} else if((rch->bb.a[k].aidx != ((uint32_t)-1)) && (rch->bb.a[k].pidx != ((uint32_t)-1))) {
			if(!is_rg_connect(rg, (uint64_t)((rch->bb.a[k].hid<<1)|(rch->bb.a[k].rev)), 
						(uint64_t)((rch->bb.a[rch->bb.a[k].aidx].hid<<1)|(rch->bb.a[rch->bb.a[k].aidx].rev)))) {
				is_end = 1;
			}
		}
		if(!is_end) continue;
		i = m = k; occ = 0;
		while (i != ((uint32_t)-1)) {
			//assert(bu->a[i] == ((uint64_t)-1));
			if(rg->seq[rch->bb.a[i].hid].del) {
				if(occ > 1) {
					bu->a[m] = occ;
					// if(debug_out) {
					// 	fprintf(stderr, "+[M::%s::] ulid::%lu, k::%ld, m::%lu, occ::%lu\n", 
					// 	__func__, ulid, k, m, occ);
					// }
				}
				// assert(i!=m);
				bu->a[i] = 0; m = (uint32_t)-1; occ = 0;
			} else {
				if(m == (uint32_t)-1) m = i;
				bu->a[i] = 0; 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);
				}
			}
		}

		if(occ > 1) {
			bu->a[m] = occ;
			// if(debug_out) {
			// 	fprintf(stderr, "+[M::%s::] ulid::%lu, k::%ld, m::%lu, occ::%lu\n", 
			// 	__func__, ulid, k, m, occ);
			// }
		}
	}

	kn = rch->bb.n;
	for (k = bu->n = 0; k < kn; k++) {
		if((bu->a[k] == ((uint64_t)-1)) || (bu->a[k] == 0)) continue;
		i = k; occ = nc = 0;
		while (i != (uint32_t)-1) {
			if(rg->seq[rch->bb.a[i].hid].del) break;

			occ++; if(is_contain_r((*ri), rch->bb.a[i].hid)) nc++; 

			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);
				}
			}
		}
		// for (i = k, occ = nc = 0; (i != (uint32_t)-1) && (!rg->seq[rch->bb.a[i].hid].del); i = rch->bb.a[i].pidx) {
		// 	occ++; if(is_contain_r((*ri), rch->bb.a[i].hid)) nc++; 
		// }
		assert(occ == bu->a[k]);
		if(nc) bu->a[bu->n++] = k;
		// if(debug_out) {
			// if(occ != rch->bb.n) {
			// 	fprintf(stderr, "-[M::%s::] ulid::%lu, nc::%lu, occ::%lu, k::%ld, rch->bb.n::%u\n", 
			// 	__func__, ulid, nc, occ, k, (uint32_t)rch->bb.n);
			// }
		// }
	}
	return bu->n;///# chains have contained reads
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
andq $0x0, (%rcx)
movq 0x20(%rsi), %rdx
xorl %eax, %eax
testq %rdx, %rdx
je 0x18d5f5
movq %rsi, %r8
movq %rdi, %r12
cmpq $0x1, %rdx
jne 0x18d2fa
movq 0x30(%r8), %rsi
testb $0x40, 0x20(%rsi)
jne 0x18d5f5
cmpq %rdx, 0x8(%rcx)
movq %rcx, 0x8(%rsp)
jae 0x18d365
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rdx
shrq $0x2, %rdx
orq %rax, %rdx
movq %rdx, %rax
shrq $0x4, %rax
orq %rdx, %rax
movq %rax, %rdx
shrq $0x8, %rdx
orq %rax, %rdx
movq %rdx, %rax
shrq $0x10, %rax
orq %rdx, %rax
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rcx)
movq 0x10(%rcx), %rdi
leaq 0x8(,%rax,8), %rsi
movq %rcx, %rbx
movq %r8, %r14
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%rbx)
movq 0x20(%r14), %rdx
jmp 0x18d36c
movq 0x10(%rcx), %rdi
movq %r8, %r14
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x20(%r14), %rcx
movq 0x8(%rsp), %r8
movq %r15, 0x18(%rsp)
movq %r14, %r9
movq %r14, 0x10(%rsp)
movq %rcx, %rbx
decq %rbx
js 0x18d4eb
movq 0x10(%r8), %rax
cmpq $-0x1, -0x8(%rax,%rcx,8)
movq %rbx, %rcx
jne 0x18d396
movq 0x30(%r9), %rax
imulq $0x24, %rbx, %rsi
movl 0x14(%rax,%rsi), %edi
movl 0x1c(%rax,%rsi), %edx
movq %rbx, %rcx
cmpq %r13, %rdx
je 0x18d40e
cmpl $-0x1, %edi
je 0x18d396
addq %rax, %rsi
movl (%rsi), %ecx
movl 0x20(%rsi), %esi
shrl $0x5, %esi
andl $0x1, %esi
leal (%rsi,%rcx,2), %esi
imulq $0x24, %rdx, %rcx
movl (%rax,%rcx), %edx
movl 0x20(%rax,%rcx), %eax
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rdx,2), %edx
movq %r12, %rdi
callq 0x1a36ae
movq 0x10(%rsp), %r9
movq 0x8(%rsp), %r8
movq %rbx, %rcx
testl %eax, %eax
jne 0x18d396
jmp 0x18d413
cmpl $-0x1, %edi
je 0x18d396
movq %rbx, %rcx
movq %rbx, %rbp
xorl %r14d, %r14d
cmpq %r13, %rcx
je 0x18d4c9
movq 0x20(%r12), %rsi
movq 0x30(%r9), %rax
imulq $0x24, %rcx, %rdx
movl (%rax,%rdx), %edi
cmpl $0x0, (%rsi,%rdi,8)
js 0x18d450
cmpq %r13, %rbp
cmoveq %rcx, %rbp
movq 0x10(%r8), %rsi
andq $0x0, (%rsi,%rcx,8)
incq %r14
jmp 0x18d46b
movq 0x10(%r8), %rsi
cmpq $0x2, %r14
jb 0x18d45e
movq %r14, (%rsi,%rbp,8)
andq $0x0, (%rsi,%rcx,8)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movl 0x14(%rax,%rdx), %r15d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %r13, %r15
je 0x18d41c
imulq $0x24, %r15, %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 %r12, %rdi
callq 0x1a36ae
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x18d4ba
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
jmp 0x18d41c
movq %rbx, %rcx
cmpq $0x2, %r14
movq 0x18(%rsp), %r15
jb 0x18d396
movq 0x10(%r8), %rax
movq %r14, (%rax,%rbp,8)
movq %rbx, %rcx
jmp 0x18d396
movq 0x20(%r9), %rsi
andq $0x0, (%r8)
xorl %ebp, %ebp
testq %rsi, %rsi
cmovleq %rbp, %rsi
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rsi, 0x20(%rsp)
cmpq %rsi, %rbp
je 0x18d5f2
movq 0x10(%r8), %rax
movq (%rax,%rbp,8), %rax
incq %rax
cmpq $0x2, %rax
jb 0x18d5ea
movq %rbp, %rcx
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq %r13, %rcx
je 0x18d5c8
movq 0x20(%r12), %rsi
movq 0x30(%r9), %rax
imulq $0x24, %rcx, %rcx
movl (%rax,%rcx), %edx
cmpl $0x0, (%rsi,%rdx,8)
js 0x18d5c8
cmpq %rdx, (%r15)
jbe 0x18d562
movq 0x8(%r15), %rsi
xorl %edi, %edi
cmpl $0x0, (%rsi,%rdx,4)
setns %dil
addq %rdi, %rbx
movl 0x14(%rax,%rcx), %r15d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %r13, %r15
je 0x18d5bb
imulq $0x24, %r15, %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 %r12, %rdi
callq 0x1a36ae
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testl %eax, %eax
je 0x18d5b1
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
incq %r14
movq 0x18(%rsp), %r15
jmp 0x18d52d
movq 0x10(%r8), %rax
cmpq %r14, (%rax,%rbp,8)
jne 0x18d604
testq %rbx, %rbx
movq 0x20(%rsp), %rsi
je 0x18d5ea
movq (%r8), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r8)
movq %rbp, (%rax,%rcx,8)
incq %rbp
jmp 0x18d507
movl (%r8), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x67f83(%rip), %rdi     # 0x1f558e
leaq 0x64511(%rip), %rsi     # 0x1f1b23
leaq 0x67f85(%rip), %rcx     # 0x1f559e
movl $0x453f, %edx           # imm = 0x453F
callq 0x2110