// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x18(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jne 0x2e4b05
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movb 0x20(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x2e4b05
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x4(%rbp)
jmp 0x2e51b0
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x2e4b1f
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x4(%rbp)
jmp 0x2e51b0
jmp 0x2e4b21
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x2e4c02
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x28(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x2e4c04
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
shlq $0x3, %rdx
movl $0xff, %esi
callq 0x2100
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
subq $0x1, %rax
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jl 0x2e4f3f
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x2e4c55
jmp 0x2e4f2e
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rcx
addq %rcx, %rax
cmpl $-0x1, 0x1c(%rax)
jne 0x2e4c96
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rcx
addq %rcx, %rax
cmpl $-0x1, 0x14(%rax)
je 0x2e4c96
movq $0x1, -0x58(%rbp)
jmp 0x2e4d74
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rcx
addq %rcx, %rax
cmpl $-0x1, 0x1c(%rax)
je 0x2e4d72
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rcx
addq %rcx, %rax
cmpl $-0x1, 0x14(%rax)
je 0x2e4d72
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x60(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x60(%rbp), %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x60(%rbp), %rdx
addq %rdx, %rcx
movl 0x1c(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x18(%rbp), %rdx
movq 0x30(%rdx), %rdx
imulq $0x24, -0x60(%rbp), %r8
addq %r8, %rdx
movl 0x1c(%rdx), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x30dcb0
cmpl $0x0, %eax
jne 0x2e4d70
movq $0x1, -0x58(%rbp)
jmp 0x2e4d72
jmp 0x2e4d74
cmpq $0x0, -0x58(%rbp)
jne 0x2e4d80
jmp 0x2e4f2e
movq -0x60(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x38(%rbp)
je 0x2e4f11
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2e4e0a
cmpq $0x1, -0x50(%rbp)
jbe 0x2e4de3
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
jmp 0x2e4e3d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x40(%rbp)
jne 0x2e4e1d
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq %rax, -0x38(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x38(%rbp)
je 0x2e4f0c
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0x1c(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x18(%rbp), %rdx
movq 0x30(%rdx), %rdx
imulq $0x24, -0x38(%rbp), %r8
addq %r8, %rdx
movl 0x1c(%rdx), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x30dcb0
cmpl $0x0, %eax
jne 0x2e4f0a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x38(%rbp)
jmp 0x2e4f0c
jmp 0x2e4d94
cmpq $0x1, -0x50(%rbp)
jbe 0x2e4f2c
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
jmp 0x2e4f2e
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2e4c32
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x2e51a6
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
cmpq $-0x1, (%rax,%rcx,8)
je 0x2e4f92
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x60(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x2e4f97
jmp 0x2e5195
movq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x38(%rbp)
je 0x2e5134
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x2e4fe8
jmp 0x2e5134
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x2e5060
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
cmpl $-0x1, (%rax,%rcx,4)
je 0x2e5060
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,4), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x2e5060
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x38(%rbp), %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movq %rax, -0x38(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x38(%rbp)
je 0x2e512f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
imulq $0x24, -0x38(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imulq $0x24, -0x38(%rbp), %rdx
addq %rdx, %rcx
movl 0x1c(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
movq -0x18(%rbp), %rdx
movq 0x30(%rdx), %rdx
imulq $0x24, -0x38(%rbp), %r8
addq %r8, %rdx
movl 0x1c(%rdx), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movb 0x20(%rcx), %cl
shrb $0x5, %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x30dcb0
cmpl $0x0, %eax
jne 0x2e512d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rax, -0x38(%rbp)
jmp 0x2e512f
jmp 0x2e4faf
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x60(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x2e514c
jmp 0x2e516b
leaq 0xaf147(%rip), %rdi     # 0x39429a
leaq 0xab6d5(%rip), %rsi     # 0x39082f
movl $0x453f, %edx           # imm = 0x453F
leaq 0xaf144(%rip), %rcx     # 0x3942aa
callq 0x2130
cmpq $0x0, -0x48(%rbp)
je 0x2e5193
movq -0x60(%rbp), %rdx
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x2e5195
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2e4f5e
movq -0x28(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)