// File: /workspace/github_cpp_makefile_star2_O1/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 %rcx, %rbp
movq $0x0, (%rcx)
movq 0x20(%rsi), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x1d08f4
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
cmpq $0x1, %rcx
jne 0x1d0500
testq %rcx, %rcx
je 0x1d08f4
movq 0x30(%rbx), %rdx
movb 0x20(%rdx), %dl
andb $0x40, %dl
jne 0x1d08f4
cmpq %rcx, 0x8(%rbp)
jae 0x1d0557
decq %rcx
movq %rcx, %rax
shrq %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq 0x10(%rbp), %rdi
movq 0x20(%rbx), %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x20(%rbx), %rax
decq %rax
movq %rbx, 0x8(%rsp)
js 0x1d076e
movq 0x10(%rbp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq $-0x1, (%rcx,%rax,8)
je 0x1d05a0
leaq -0x1(%rax), %rsi
testq %rax, %rax
movq %rsi, %rax
jg 0x1d0588
jmp 0x1d076e
movq 0x30(%rbx), %rsi
leaq (%rax,%rax,8), %rdi
leaq (%rsi,%rdi,4), %r8
movl 0x1c(%r8), %edi
cmpq %rdx, %rdi
je 0x1d0633
cmpl $-0x1, 0x14(%r8)
je 0x1d058f
movl (%r8), %r9d
movl 0x20(%r8), %r8d
shrl $0x5, %r8d
andl $0x1, %r8d
leal (%r8,%r9,2), %r8d
movq 0x28(%r12), %r9
movq (%r9,%r8,8), %r8
movq %r8, %r9
andq %rdx, %r9
je 0x1d063e
leaq (%rdi,%rdi,8), %rdi
movl (%rsi,%rdi,4), %r10d
movl 0x20(%rsi,%rdi,4), %edi
shrl $0x5, %edi
andl $0x1, %edi
leal (%rdi,%r10,2), %edi
movq 0x8(%r12), %r10
shrq $0x20, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r8,%r8,2), %r8
leaq (%r10,%r8,8), %r8
addq $0xc, %r8
xorl %r10d, %r10d
cmpl $0x0, (%r8,%r10)
js 0x1d0628
cmpl %edi, -0x4(%r8,%r10)
je 0x1d058f
addq $0x18, %r10
cmpq %r10, %r9
jne 0x1d0616
jmp 0x1d063e
cmpl $-0x1, 0x14(%r8)
je 0x1d0760
cmpq %rdx, %rax
jne 0x1d064e
movq %rax, %rdi
xorl %r8d, %r8d
jmp 0x1d0748
movq 0x20(%r12), %r9
xorl %r8d, %r8d
movq %rax, %rdi
movq %rax, %r11
leaq (%r11,%r11,8), %r10
movl (%rsi,%r10,4), %ebx
cmpl $0x0, (%r9,%rbx,8)
js 0x1d067f
cmpq %rdx, %rdi
cmoveq %r11, %rdi
movq $0x0, (%rcx,%r11,8)
incq %r8
jmp 0x1d0699
cmpq $0x2, %r8
jb 0x1d0689
movq %r8, (%rcx,%rdi,8)
movq $0x0, (%rcx,%r11,8)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movl 0x14(%rsi,%r10,4), %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpq %rdx, %r10
je 0x1d073e
leaq (%r10,%r10,8), %r15
movl 0x20(%rsi,%r15,4), %r11d
shrl $0x5, %r11d
andl $0x1, %r11d
movl (%rsi,%r15,4), %ebx
leal (%r11,%rbx,2), %r11d
movq 0x28(%r12), %rbx
movq (%rbx,%r11,8), %r13
movq %r13, %rbx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
andq %rdx, %rbx
je 0x1d073e
movl 0x1c(%rsi,%r15,4), %r11d
leaq (%r11,%r11,8), %r11
movl (%rsi,%r11,4), %r15d
movl 0x20(%rsi,%r11,4), %r11d
shrl $0x5, %r11d
andl $0x1, %r11d
leal (%r11,%r15,2), %r11d
movq 0x8(%r12), %r15
shrq $0x20, %r13
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rbx
leaq (%r13,%r13,2), %r13
leaq (%r15,%r13,8), %r15
addq $0xc, %r15
xorl %r13d, %r13d
cmpl $0x0, (%r15,%r13)
js 0x1d072a
cmpl %r11d, -0x4(%r15,%r13)
je 0x1d073b
addq $0x18, %r13
cmpq %r13, %rbx
jne 0x1d071c
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
jmp 0x1d073e
movq %r10, %r11
cmpl $-0x1, %r11d
jne 0x1d065c
cmpq $0x2, %r8
movq 0x8(%rsp), %rbx
jb 0x1d058f
movq %r8, (%rcx,%rdi,8)
jmp 0x1d058f
cmpl $-0x1, %edi
jne 0x1d05b5
jmp 0x1d058f
movq 0x20(%rbx), %r9
movq $0x0, (%rbp)
testq %r9, %r9
jle 0x1d08f1
movq 0x10(%rbp), %r10
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %rbp, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %r10, 0x10(%rsp)
movq (%r10,%rdx,8), %rdi
leaq 0x1(%rdi), %rax
cmpq $0x2, %rax
jae 0x1d07b8
incq %rdx
cmpq %r9, %rdx
jne 0x1d079d
jmp 0x1d08f1
cmpq %rsi, %rdx
jne 0x1d07c7
movb $0x1, %al
xorl %r8d, %r8d
jmp 0x1d08cf
movq 0x20(%r12), %r10
movq 0x30(%rbx), %r11
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %rdx, %rbp
leaq (,%rbp,8), %rbx
addq %rbp, %rbx
movl (%r11,%rbx,4), %eax
cmpl $0x0, (%r10,%rax,8)
js 0x1d08b5
cmpq %rax, (%r14)
jbe 0x1d080b
movq 0x8(%r14), %r15
xorl %r13d, %r13d
cmpl $0x0, (%r15,%rax,4)
setns %r13b
addq %r13, %r9
movl 0x14(%r11,%rbx,4), %r13d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq %rsi, %r13
je 0x1d08a9
leaq (,%r13,8), %rax
addq %r13, %rax
movl 0x20(%r11,%rax,4), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
movl (%r11,%rax,4), %r15d
leal (%rbx,%r15,2), %ebx
movq 0x28(%r12), %r15
movq (%r15,%rbx,8), %r15
movq %r15, %rbx
andq %rsi, %rbx
je 0x1d08a9
movl 0x1c(%r11,%rax,4), %eax
leaq (%rax,%rax,8), %rax
movl (%r11,%rax,4), %ebp
movl 0x20(%r11,%rax,4), %eax
shrl $0x5, %eax
andl $0x1, %eax
leal (%rax,%rbp,2), %ebp
movq 0x8(%r12), %rcx
shrq $0x20, %r15
shlq $0x3, %rbx
leaq (%rbx,%rbx,2), %rax
leaq (%r15,%r15,2), %rbx
leaq (%rcx,%rbx,8), %rbx
addq $0xc, %rbx
xorl %r15d, %r15d
cmpl $0x0, (%rbx,%r15)
js 0x1d0896
cmpl %ebp, -0x4(%rbx,%r15)
je 0x1d08a6
addq $0x18, %r15
cmpq %r15, %rax
jne 0x1d0888
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1d08a9
movq %r13, %rbp
incq %r8
cmpl $-0x1, %ebp
jne 0x1d07d9
testq %r9, %r9
sete %al
movq 0x20(%rsp), %rbp
movq 0x8(%rsp), %rbx
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r10
cmpq %rdi, %r8
jne 0x1d0903
testb %al, %al
jne 0x1d07ab
movq (%rbp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rbp)
movq %rdx, (%r10,%rax,8)
jmp 0x1d07ab
movl (%rbp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66aec(%rip), %rdi     # 0x2373f6
leaq 0x6307a(%rip), %rsi     # 0x23398b
leaq 0x66aee(%rip), %rcx     # 0x237406
movl $0x453f, %edx           # imm = 0x453F
callq 0x2110