// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/inter.cpp
// Function: update_consensus_chain(ug_opt_t const*, asg_t const*, kv_ul_ov_t*, kv_ul_ov_t*, ul_vec_t*, asg64_v*, R_to_U*)
uint32_t update_consensus_chain(const ug_opt_t *uopt, const asg_t *rg, kv_ul_ov_t *idx, kv_ul_ov_t *dump, ul_vec_t *rch, asg64_v *b, R_to_U *ri)
{
	uint64_t *idm, k, l, kn, dn, cc = 0;
	assert(dump->n >= rch->bb.n);
	kv_resize(uint64_t, *b, dump->n); idm = b->a;

	for (k = kn = 0; k < rch->bb.n; k++) {
		idm[k] = (uint64_t)-1;
		if((dump->a[k].qn == ((uint32_t)-1)) && (dump->a[k].tn == ((uint32_t)-1))) continue;
		dump->a[k].qn = rch->bb.a[dump->a[k].qn].pidx; idm[k] = kn; kn++;
	}
	for (; k < dump->n; k++) {
		idm[k] = kn; kn++;
	}
	// fprintf(stderr, "[M::%s::kn->%lu] dump->n::%u\n", __func__, kn, (uint32_t)dump->n);
	
	dn = dump->n; dump->n = 0; 
	kv_resize(ul_ov_t, *idx, kn); idx->n = 0;
	for (k = 0; k < dn; k++) {
		if(idm[k] == ((uint64_t)-1)) continue;
		dump->a[idm[k]] = dump->a[k]; kn--; dump->n++;
		if(dump->a[idm[k]].qn != ((uint32_t)-1)) {
			dump->a[idm[k]].qn = (uint32_t)idm[dump->a[idm[k]].qn];
		}
		kv_push(ul_ov_t, *idx, dump->a[idm[k]]); idx->a[idx->n-1].qn = idm[k];
	}
	assert(kn == 0); 

	radix_sort_ul_ov_srt_qe(idx->a, idx->a + idx->n);
	for (k = 1, l = 0; k <= idx->n; k++) {
        if (k == idx->n || idx->a[l].qe != idx->a[k].qe) {
            if(k - l > 1) radix_sort_ul_ov_srt_qs(idx->a+l, idx->a+k);
            l = k;
        }
    }
	for (k = 0; k < idx->n; k++) dump->a[idx->a[k].qn].tn = k;

	for (k = 0; k < idx->n; k++) {
		idx->a[k].qn = ((dump->a[idx->a[k].qn].qn!=((uint32_t)-1))?
									(dump->a[dump->a[idx->a[k].qn].qn].tn):((uint32_t)-1));
		idx->a[k].el = 1;
	}

	uc_block_t *z, *p; int64_t tt;
	kv_resize(uc_block_t, rch->bb, idx->n); rch->bb.n = 0;
	for (k = 0; k < idx->n; k++) {
		// fprintf(stderr, "+k::%lu[M::%s::id->%u] q::[%u, %u), t::[%u, %u), is_cr::%u\n", 
		// k, __func__, idx->a[k].tn, idx->a[k].qs, idx->a[k].qe, idx->a[k].ts, idx->a[k].te, 
		// !!(is_contain_r((*ri), idx->a[k].tn)));
		kv_pushp(uc_block_t, rch->bb, &z); memset(z, 0, sizeof((*z)));
		z->hid = idx->a[k].tn; z->rev = idx->a[k].rev; 
    	z->pchain = 1; z->base = 0; z->el = 1;
		z->qs = idx->a[k].qs; z->qe = idx->a[k].qe;
		z->te = idx->a[k].te; z->ts = idx->a[k].ts;
        z->pidx = idx->a[k].qn; z->pdis = z->aidx = (uint32_t)-1;
		
	}
	for (k = cc = 0; k < rch->bb.n; k++) {
		if(is_contain_r((*ri), rch->bb.a[k].hid)) cc++;
		// fprintf(stderr, "-k::%lu[M::%s::id->%u] q::[%u, %u), t::[%u, %u), is_cr::%u\n", 
		// k, __func__, rch->bb.a[k].hid, rch->bb.a[k].qs, rch->bb.a[k].qe, rch->bb.a[k].ts, rch->bb.a[k].te, 
		// !!(is_contain_r((*ri), rch->bb.a[k].hid)));
		if(rch->bb.a[k].pidx == (uint32_t)-1) continue;
		z = &(rch->bb.a[k]); p = &(rch->bb.a[z->pidx]);
		assert(p->aidx == (uint32_t)-1); p->aidx = k;
		tt = g_adjacent_dis_mul(NULL, uopt->sources, uopt->max_hang, uopt->min_ovlp, ((z->hid<<1)|((uint32_t)z->rev))^1, ((p->hid<<1)|((uint32_t)p->rev))^1);
		if(tt >= 0) z->pdis = tt; ///assert(tt >= 0);
	}

	///debug_ssb
	// for (k = 0; k < rch->bb.n; k++) {
	// 	if(rch->bb.a[k].pidx != (uint32_t)-1) {
	// 		if((rch->bb.a[k].pidx >= 0) && (rch->bb.a[k].pidx < rch->bb.n) && 
	// 					(rch->bb.a[rch->bb.a[k].pidx].aidx == k)) {
	// 			;
	// 		} else {
	// 			fprintf(stderr, "[M::%s::k->%lu] +rch->bb.n::%u\n", __func__, k, (uint32_t)rch->bb.n);
	// 			for (l = 0; l < rch->bb.n; l++) {
	// 				fprintf(stderr, "[M::%.*s::k->%lu] q::[%u, %u), t::[%u, %u), is_cr::%u, pidx::%u, aidx::%u\n", 
	// 				(int)Get_NAME_LENGTH(R_INF, rch->bb.a[l].hid), Get_NAME(R_INF, rch->bb.a[l].hid), l, 
	// 				rch->bb.a[l].qs, rch->bb.a[l].qe, rch->bb.a[l].ts, rch->bb.a[l].te, 
	// 				!!(is_contain_r((*ri), rch->bb.a[l].hid)), 
	// 				rch->bb.a[l].pidx, rch->bb.a[l].aidx);
	// 			}
	// 			exit(1);
	// 		}
	// 	}

	// 	if(rch->bb.a[k].aidx != (uint32_t)-1) {
	// 		if((rch->bb.a[k].aidx >= 0) && (rch->bb.a[k].aidx < rch->bb.n) && 
	// 					(rch->bb.a[rch->bb.a[k].aidx].pidx == k)) {
	// 			;
	// 		} else {
	// 			fprintf(stderr, "[M::%s::k->%lu] -rch->bb.n::%u\n", __func__, k, (uint32_t)rch->bb.n);
	// 			for (l = 0; l < rch->bb.n; l++) {
	// 				fprintf(stderr, "[M::%.*s::k->%lu] q::[%u, %u), t::[%u, %u), is_cr::%u, pidx::%u, aidx::%u\n", 
	// 				(int)Get_NAME_LENGTH(R_INF, rch->bb.a[l].hid), Get_NAME(R_INF, rch->bb.a[l].hid), l, 
	// 				rch->bb.a[l].qs, rch->bb.a[l].qe, rch->bb.a[l].ts, rch->bb.a[l].te, 
	// 				!!(is_contain_r((*ri), rch->bb.a[l].hid)), 
	// 				rch->bb.a[l].pidx, rch->bb.a[l].aidx);
	// 			}
	// 			exit(1);
	// 		}
	// 	}
	// }

	return ((cc==0)?1:0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, 0x10(%rsp)
movq 0x8(%rcx), %rax
cmpq 0x20(%r8), %rax
jb 0x1d2e58
movq %r9, %r13
movq %rdx, %r15
cmpq %rax, 0x8(%r9)
movq %r8, 0x8(%rsp)
movq %rcx, (%rsp)
jae 0x1d28e6
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r13)
movq 0x10(%r13), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %rcx
movq 0x8(%rsp), %r8
movq %rax, 0x10(%r13)
movq 0x10(%r13), %r13
cmpq $0x0, 0x20(%r8)
je 0x1d293b
movq (%rcx), %r9
xorl %eax, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq $-0x1, (%r13,%rax,8)
movl (%r9), %esi
cmpq %rdx, %rsi
jne 0x1d2915
cmpl $-0x1, 0xc(%r9)
je 0x1d292c
movq 0x30(%r8), %rdi
leaq (%rsi,%rsi,8), %rsi
movl 0x14(%rdi,%rsi,4), %esi
movl %esi, (%r9)
movq %rbp, (%r13,%rax,8)
incq %rbp
incq %rax
addq $0x1c, %r9
cmpq 0x20(%r8), %rax
jb 0x1d28fd
jmp 0x1d294c
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0x1d294c
movq %rbp, (%r13,%rax,8)
incq %rbp
incq %rax
movq 0x8(%rcx), %r14
cmpq %r14, %rax
jb 0x1d2941
movq $0x0, 0x8(%rcx)
cmpq %rbp, 0x10(%r15)
jae 0x1d29be
leaq -0x1(%rbp), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x10(%r15)
movq (%r15), %rdi
leaq (%rax,%rax,8), %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax), %rsi
incq %rsi
callq 0x2370
movq (%rsp), %rcx
movq %rax, (%r15)
movq $0x0, 0x8(%r15)
testq %r14, %r14
je 0x1d2ae1
xorl %r12d, %r12d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq (%r13,%rbx,8), %rax
cmpq $-0x1, %rax
je 0x1d2ad1
movq (%rcx), %rsi
leaq (%rax,%rax,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rax, %rdx
movups (%rsi,%r12), %xmm0
movups 0xc(%rsi,%r12), %xmm1
movups %xmm1, 0xc(%rsi,%rdx)
movups %xmm0, (%rsi,%rdx)
incq 0x8(%rcx)
movq (%rcx), %rax
movq (%r13,%rbx,8), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
movl (%rax,%rsi), %edx
cmpq %r9, %rdx
je 0x1d2a34
addq %rsi, %rax
movl (%r13,%rdx,8), %edx
movl %edx, (%rax)
movq 0x10(%r15), %rax
cmpq %rax, 0x8(%r15)
jne 0x1d2a72
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x10(%r15)
movq (%r15), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rsi
addq %rcx, %rsi
callq 0x2370
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq (%rsp), %rcx
movq %rax, (%r15)
decq %rbp
movq (%rcx), %rax
movq (%r13,%rbx,8), %rsi
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movq (%r15), %r8
movq 0x8(%r15), %rsi
leaq 0x1(%rsi), %rdi
movq %rdi, 0x8(%r15)
leaq (%rsi,%rsi,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rsi, %rdi
movups (%rax,%rdx), %xmm0
movups 0xc(%rax,%rdx), %xmm1
movups %xmm1, 0xc(%r8,%rdi)
movups %xmm0, (%r8,%rdi)
movl (%r13,%rbx,8), %eax
movq (%r15), %rdi
movq 0x8(%r15), %rdx
leaq (%rdx,%rdx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rdx, %rsi
movl %eax, -0x1c(%rdi,%rsi)
incq %rbx
addq $0x1c, %r12
cmpq %rbx, %r14
jne 0x1d29da
testq %rbp, %rbp
jne 0x1d2e77
movq (%r15), %rdi
movq 0x8(%r15), %rsi
leaq (%rsi,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdi, %rsi
addq %rax, %rsi
callq 0x195511
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1d2b85
movl $0x1, %ebx
movl $0x1c, %r14d
xorl %ecx, %ecx
movq (%rsp), %r10
cmpq %rax, %rbx
je 0x1d2b41
movq (%r15), %rax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl 0x8(%rax,%rdx), %edx
cmpl 0x8(%rax,%r14), %edx
jne 0x1d2b41
movq %rcx, %rdx
jmp 0x1d2b70
movq %rbx, %rax
subq %rcx, %rax
movq %rbx, %rdx
cmpq $0x2, %rax
jb 0x1d2b70
movq (%r15), %rsi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
addq %rcx, %rdi
addq %rsi, %rdi
addq %r14, %rsi
callq 0x1957bd
movq (%rsp), %r10
movq %rbx, %rdx
incq %rbx
movq 0x8(%r15), %rax
addq $0x1c, %r14
movq %rdx, %rcx
cmpq %rax, %rbx
jbe 0x1d2b1e
jmp 0x1d2b89
movq (%rsp), %r10
testq %rax, %rax
je 0x1d2bb4
movq (%r10), %rcx
movq (%r15), %rdx
xorl %esi, %esi
movl (%rdx), %edi
leaq (%rdi,%rdi,8), %r8
leaq (%r8,%r8,2), %r8
addq %rdi, %r8
movl %esi, 0xc(%rcx,%r8)
incq %rsi
addq $0x1c, %rdx
cmpq %rsi, %rax
jne 0x1d2b96
movq 0x8(%r15), %rsi
testq %rsi, %rsi
je 0x1d2c13
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %edx, %edx
movq (%r10), %rdi
movq (%r15), %rsi
movl (%rsi,%rax), %r8d
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movl (%rdi,%r9), %r8d
cmpq %rcx, %r8
je 0x1d2bf6
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movl 0xc(%rdi,%r9), %edi
jmp 0x1d2bfb
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%rsi,%rax)
orb $0x40, 0x1b(%rsi,%rax)
incq %rdx
movq 0x8(%r15), %rsi
addq $0x1c, %rax
cmpq %rsi, %rdx
jb 0x1d2bc6
movq 0x8(%rsp), %rdi
cmpq %rsi, 0x28(%rdi)
jae 0x1d2c78
decq %rsi
movq %rsi, %rax
shrq %rax
orq %rsi, %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, 0x28(%rdi)
movq 0x30(%rdi), %rdi
leaq 0x4(,%rax,4), %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq 0x8(%rsp), %rdi
movq %rax, 0x30(%rdi)
movq $0x0, 0x20(%rdi)
cmpq $0x0, 0x8(%r15)
je 0x1d2d53
xorl %ebx, %ebx
movl $0x2, %r14d
xorps %xmm0, %xmm0
xorl %r12d, %r12d
movq 0x28(%rdi), %rax
cmpq %rax, 0x20(%rdi)
jne 0x1d2ccf
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r14, %rcx
movq %rcx, 0x28(%rdi)
movq 0x30(%rdi), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
xorps %xmm0, %xmm0
movq 0x8(%rsp), %rdi
movq %rax, 0x30(%rdi)
movq 0x20(%rdi), %rax
movq 0x30(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%rdi)
leaq (%rax,%rax,8), %rax
movups %xmm0, 0x10(%rcx,%rax,4)
movups %xmm0, (%rcx,%rax,4)
movl $0x0, 0x20(%rcx,%rax,4)
movq (%r15), %rdx
movl 0xc(%rdx,%rbx), %esi
movl %esi, (%rcx,%rax,4)
movl 0x18(%rdx,%rbx), %edx
shrl $0x1a, %edx
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rcx,%rax,4)
movq (%r15), %rdx
movl 0x4(%rdx,%rbx), %esi
movl %esi, 0x4(%rcx,%rax,4)
movl 0x8(%rdx,%rbx), %esi
movl %esi, 0x8(%rcx,%rax,4)
movl 0x14(%rdx,%rbx), %esi
movl %esi, 0x10(%rcx,%rax,4)
movl 0x10(%rdx,%rbx), %esi
movl %esi, 0xc(%rcx,%rax,4)
movl (%rdx,%rbx), %edx
movl %edx, 0x14(%rcx,%rax,4)
movq $-0x1, 0x18(%rcx,%rax,4)
incq %r12
addq $0x1c, %rbx
cmpq 0x8(%r15), %r12
jb 0x1d2c99
cmpq $0x0, 0x20(%rdi)
je 0x1d2e25
movq 0x50(%rsp), %rbx
movl $0x20, %r14d
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x30(%rdi), %rbp
movl -0x20(%rbp,%r14), %eax
cmpq %rax, (%rbx)
jbe 0x1d2d93
movq 0x8(%rbx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rax,4)
setns %dl
addq %rdx, %r13
movl -0xc(%rbp,%r14), %ecx
cmpq %r12, %rcx
je 0x1d2e0a
leaq (%rcx,%rcx,8), %rcx
cmpl $-0x1, 0x1c(%rbp,%rcx,4)
jne 0x1d2e39
leaq (,%rcx,4), %rdi
addq %rbp, %rdi
movl %r15d, 0x1c(%rdi)
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rsi
movslq 0x40(%rcx), %rdx
movslq 0x44(%rcx), %rcx
movl (%rbp,%r14), %r8d
shrl $0x5, %r8d
andl $0x1, %r8d
leal (%r8,%rax,2), %r8d
xorl $0x1, %r8d
movl (%rdi), %eax
movl 0x20(%rdi), %edi
shrl $0x5, %edi
andl $0x1, %edi
leal (%rdi,%rax,2), %r9d
xorl $0x1, %r9d
xorl %edi, %edi
callq 0x1cad23
testq %rax, %rax
movq 0x8(%rsp), %rdi
js 0x1d2e0a
movl %eax, -0x8(%rbp,%r14)
incq %r15
addq $0x24, %r14
cmpq 0x20(%rdi), %r15
jb 0x1d2d75
xorl %eax, %eax
testq %r13, %r13
sete %al
jmp 0x1d2e2a
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x64801(%rip), %rdi     # 0x237641
leaq 0x60b44(%rip), %rsi     # 0x23398b
leaq 0x6476d(%rip), %rcx     # 0x2375bb
movl $0x4762, %edx           # imm = 0x4762
callq 0x2110
leaq 0x64747(%rip), %rdi     # 0x2375a6
leaq 0x60b25(%rip), %rsi     # 0x23398b
leaq 0x6474e(%rip), %rcx     # 0x2375bb
movl $0x4725, %edx           # imm = 0x4725
callq 0x2110
leaq 0x647bb(%rip), %rdi     # 0x237639
leaq 0x60b06(%rip), %rsi     # 0x23398b
leaq 0x6472f(%rip), %rcx     # 0x2375bb
movl $0x473c, %edx           # imm = 0x473C
callq 0x2110