// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, 0x28(%rsp)
movq %rdi, 0x10(%rsp)
movq $0x0, (%rcx)
movq 0x20(%rsi), %rdx
xorl %ebp, %ebp
testq %rdx, %rdx
je 0x1da928
movq %rsi, %rbx
cmpq $0x1, %rdx
jne 0x1da4ed
movq 0x30(%rbx), %rax
testb $0x40, 0x20(%rax)
jne 0x1da928
cmpq %rdx, 0x8(%rcx)
movq %rbx, %r13
jae 0x1da559
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
callq 0x2370
movq %rax, %rdi
movq %rbx, %r12
movq %rax, 0x10(%rbx)
movq %r13, %rbx
movq 0x20(%r13), %rdx
jmp 0x1da560
movq %rcx, %r12
movq 0x10(%rcx), %rdi
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x20e0
movq 0x20(%rbx), %rax
decq %rax
js 0x1da91c
movq 0x10(%r12), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %rbp
cmpq $-0x1, (%rcx,%rax,8)
je 0x1da5a2
leaq -0x1(%rax), %rsi
testq %rax, %rax
movq %rsi, %rax
jg 0x1da58a
jmp 0x1da746
movq 0x30(%rbx), %rsi
leaq (%rax,%rax,8), %r8
movl 0x14(%rsi,%r8,4), %r9d
movl 0x1c(%rsi,%r8,4), %edi
cmpq %rdx, %rdi
je 0x1da645
cmpl $-0x1, %r9d
je 0x1da591
leaq (%rsi,%r8,4), %r8
movl (%r8), %r9d
movl 0x20(%r8), %r8d
shrl $0x5, %r8d
andl $0x1, %r8d
leal (%r8,%r9,2), %r8d
movq 0x28(%rbp), %r9
movq (%r9,%r8,8), %r8
movq %r8, %r9
andq %rdx, %r9
je 0x1da63a
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(%rbp), %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 0x1da631
cmpl %edi, -0x4(%r8,%r10)
je 0x1da591
addq $0x18, %r10
cmpq %r10, %r9
jne 0x1da61f
cmpq %rdx, %rax
je 0x1da591
jmp 0x1da65d
cmpl $-0x1, %r9d
sete %dil
cmpq %rdx, %rax
sete %r8b
orb %dil, %r8b
jne 0x1da591
movq 0x20(%rbp), %rdi
xorl %r9d, %r9d
movq %rax, %r8
movq %rax, %r10
leaq (%r10,%r10,8), %r11
movl (%rsi,%r11,4), %ebx
cmpl $0x0, (%rdi,%rbx,8)
js 0x1da68c
cmpq %rdx, %r8
cmoveq %r10, %r8
movq $0x0, (%rcx,%r10,8)
incq %r9
jmp 0x1da6a7
cmpq $0x2, %r9
jb 0x1da696
movq %r9, (%rcx,%r8,8)
movq $0x0, (%rcx,%r10,8)
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
movl 0x14(%rsi,%r11,4), %r10d
cmpq %rdx, %r10
je 0x1da730
leaq (%r10,%r10,8), %r11
movl 0x20(%rsi,%r11,4), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
movl (%rsi,%r11,4), %r14d
leal (%rbx,%r14,2), %ebx
movq 0x28(%rbp), %r14
movq (%r14,%rbx,8), %rbx
movq %rbx, %r14
andq %rdx, %r14
je 0x1da730
movl 0x1c(%rsi,%r11,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(%rbp), %r15
shrq $0x20, %rbx
shlq $0x3, %r14
leaq (%r14,%r14,2), %r14
leaq (%rbx,%rbx,2), %rbx
leaq (%r15,%rbx,8), %rbx
addq $0xc, %rbx
xorl %r15d, %r15d
cmpl $0x0, (%rbx,%r15)
js 0x1da727
cmpl %r11d, -0x4(%rbx,%r15)
je 0x1da66a
addq $0x18, %r15
cmpq %r15, %r14
jne 0x1da715
cmpq $0x2, %r9
movq %r13, %rbx
jb 0x1da591
movq %r9, (%rcx,%r8,8)
jmp 0x1da591
movq 0x20(%rbx), %rax
movq $0x0, (%r12)
testq %rax, %rax
jle 0x1da926
movq %r12, %r10
movq 0x10(%r12), %rcx
xorl %edx, %edx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rcx, 0x20(%rsp)
movq %rbx, 0x8(%rsp)
movq %r12, (%rsp)
movq %rax, 0x18(%rsp)
movq (%rcx,%rdx,8), %rdi
leaq 0x1(%rdi), %rsi
cmpq $0x2, %rsi
jb 0x1da8fe
cmpq %r13, %rdx
je 0x1da939
movq 0x20(%rbp), %r8
movq 0x30(%rbx), %r11
leaq (%rdx,%rdx,8), %rsi
movl (%r11,%rsi,4), %r14d
cmpl $0x0, (%r8,%r14,8)
js 0x1da8db
movq 0x28(%rsp), %rsi
movq (%rsi), %r15
movq %rdx, %rbp
xorl %r12d, %r12d
xorl %r9d, %r9d
movq %r15, 0x30(%rsp)
cmpq %r14, %r15
jbe 0x1da7e5
movq 0x28(%rsp), %rsi
movq 0x8(%rsi), %rsi
xorl %ebx, %ebx
cmpl $0x0, (%rsi,%r14,4)
setns %bl
addq %rbx, %r12
incq %r9
leaq (,%rbp,8), %rsi
addq %rbp, %rsi
movl 0x14(%r11,%rsi,4), %ebp
cmpq %r13, %rbp
je 0x1da90c
leaq (,%rbp,8), %r14
addq %rbp, %r14
movl 0x20(%r11,%r14,4), %esi
shrl $0x5, %esi
andl $0x1, %esi
movl (%r11,%r14,4), %ebx
leal (%rsi,%rbx,2), %esi
movq 0x10(%rsp), %r10
movq 0x28(%r10), %rbx
movq (%rbx,%rsi,8), %r15
movq %r15, %rsi
andq %r13, %rsi
je 0x1da90c
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movl 0x1c(%r11,%r14,4), %ebx
leaq (%rbx,%rbx,8), %rbx
movl (%r11,%rbx,4), %r13d
movl 0x20(%r11,%rbx,4), %ebx
shrl $0x5, %ebx
andl $0x1, %ebx
leal (%rbx,%r13,2), %ebx
movq 0x8(%r10), %r13
shrq $0x20, %r15
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%r15,%r15,2), %r15
leaq 0xc(,%r15,8), %r15
addq %r13, %r15
xorl %r13d, %r13d
cmpl $0x0, (%r15,%r13)
js 0x1da88c
cmpl %ebx, -0x4(%r15,%r13)
je 0x1da897
addq $0x18, %r13
cmpq %r13, %rsi
jne 0x1da87e
jmp 0x1da8c3
movl (%r11,%r14,4), %r14d
cmpl $0x0, (%r8,%r14,8)
movq %rcx, %r10
movq %rax, %rbx
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x30(%rsp), %r15
jns 0x1da7ca
jmp 0x1da8e1
movq %rcx, %r10
movq %rax, %rbx
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x1da8e1
xorl %r12d, %r12d
xorl %r9d, %r9d
cmpq %rdi, %r9
movq 0x10(%rsp), %rbp
jne 0x1da939
testq %r12, %r12
je 0x1da8fe
movq (%r10), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, (%r10)
movq %rdx, (%rcx,%rsi,8)
incq %rdx
cmpq %rax, %rdx
jne 0x1da77e
jmp 0x1da917
movq (%rsp), %r10
movq 0x8(%rsp), %rbx
jmp 0x1da8e1
movl (%r10), %ebp
jmp 0x1da928
movq $0x0, (%r12)
jmp 0x1da928
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x68d86(%rip), %rdi     # 0x2436c6
leaq 0x65314(%rip), %rsi     # 0x23fc5b
leaq 0x68d88(%rip), %rcx     # 0x2436d6
movl $0x453f, %edx           # imm = 0x453F
callq 0x2110