// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, %r15
movq %rdi, 0x10(%rsp)
movq 0x8(%rcx), %rdx
movq 0x20(%r8), %rax
cmpq %rax, %rdx
jb 0x1dceee
movq %r9, %rbp
cmpq %rdx, 0x8(%r9)
movq %r8, (%rsp)
movq %rcx, 0x8(%rsp)
jae 0x1dc97f
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %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 0x8(%rsp), %rcx
movq (%rsp), %r8
movq %rax, %r13
movq %rax, 0x10(%rbp)
movq 0x20(%r8), %rax
jmp 0x1dc983
movq 0x10(%rbp), %r13
testq %rax, %rax
je 0x1dc9d2
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 0x1dc9ac
cmpl $-0x1, 0xc(%r9)
je 0x1dc9c3
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 0x1dc994
jmp 0x1dc9e3
xorl %ebp, %ebp
xorl %eax, %eax
jmp 0x1dc9e3
movq %rbp, (%r13,%rax,8)
incq %rbp
incq %rax
movq 0x8(%rcx), %rbx
cmpq %rbx, %rax
jb 0x1dc9d8
movq $0x0, 0x8(%rcx)
cmpq %rbp, 0x10(%r15)
jae 0x1dca56
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 0x8(%rsp), %rcx
movq %rax, (%r15)
movq $0x0, 0x8(%r15)
testq %rbx, %rbx
je 0x1dcb80
xorl %r14d, %r14d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %r12d, %r12d
movq (%r13,%r12,8), %rsi
cmpq $-0x1, %rsi
je 0x1dcb6e
movq (%rcx), %rax
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movups (%rax,%r14), %xmm0
movups 0xc(%rax,%r14), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
incq 0x8(%rcx)
movq (%rcx), %r8
movq (%r13,%r12,8), %rdx
leaq (%rdx,%rdx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rdx, %rax
movl (%r8,%rax), %esi
cmpq %r9, %rsi
je 0x1dcad0
addq %r8, %rax
movl (%r13,%rsi,8), %esi
movl %esi, (%rax)
movq (%r15), %rax
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
jne 0x1dcb1e
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
movl $0x2, %edx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r15)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rsi
addq %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rcx
movq %rax, (%r15)
movq (%rcx), %r8
movq (%r13,%r12,8), %rdx
movq 0x8(%r15), %rsi
decq %rbp
leaq (%rdx,%rdx,8), %rdi
leaq (%rdi,%rdi,2), %rdi
addq %rdx, %rdi
leaq 0x1(%rsi), %rdx
movq %rdx, 0x8(%r15)
leaq (%rsi,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rsi, %rdx
movups (%r8,%rdi), %xmm0
movups 0xc(%r8,%rdi), %xmm1
movups %xmm1, 0xc(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movl (%r13,%r12,8), %edi
movq (%r15), %rdx
movq 0x8(%r15), %rax
leaq (%rax,%rax,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rax, %rsi
movl %edi, -0x1c(%rdx,%rsi)
incq %r12
addq $0x1c, %r14
cmpq %r12, %rbx
jne 0x1dca75
jmp 0x1dcb82
xorl %eax, %eax
testq %rbp, %rbp
jne 0x1dcf0d
movq (%r15), %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rax, %rsi
addq %rdi, %rsi
callq 0x19e2b4
movq 0x8(%r15), %rax
testq %rax, %rax
movq (%rsp), %r10
movq 0x8(%rsp), %r11
je 0x1dceb1
movl $0x1, %ebx
movl $0x1c, %r14d
xorl %ecx, %ecx
cmpq %rax, %rbx
je 0x1dcbe7
movq (%r15), %rdx
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rcx, %rsi
movl 0x8(%rdx,%rsi), %esi
cmpl 0x8(%rdx,%r14), %esi
jne 0x1dcbe7
movq %rcx, %rdx
jmp 0x1dcc1f
movq %rbx, %rsi
subq %rcx, %rsi
movq %rbx, %rdx
cmpq $0x2, %rsi
jb 0x1dcc1f
movq (%r15), %rsi
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
addq %rcx, %rdi
addq %rsi, %rdi
addq %r14, %rsi
callq 0x19e55c
movq 0x8(%rsp), %r11
movq (%rsp), %r10
movq 0x8(%r15), %rax
movq %rbx, %rdx
incq %rbx
addq $0x1c, %r14
movq %rdx, %rcx
cmpq %rax, %rbx
jbe 0x1dcbc4
testq %rax, %rax
je 0x1dceb1
movq (%r11), %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 0x1dcc3f
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %edx, %edx
movq (%r11), %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 0x1dcc96
leaq (%r8,%r8,8), %r9
leaq (%r9,%r9,2), %r9
addq %r8, %r9
movl 0xc(%rdi,%r9), %edi
jmp 0x1dcc9b
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 0x1dcc66
cmpq %rsi, 0x28(%r10)
jae 0x1dcd16
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(%r10)
movq 0x30(%r10), %rdi
leaq 0x4(,%rax,4), %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq (%rsp), %r10
movq %rax, 0x30(%r10)
movq 0x8(%r15), %rsi
movq $0x0, 0x20(%r10)
movl $0x1, %ebp
testq %rsi, %rsi
je 0x1dcebe
xorl %ebx, %ebx
movl $0x2, %r14d
xorps %xmm2, %xmm2
xorl %r12d, %r12d
movq 0x20(%r10), %rcx
movq 0x30(%r10), %rax
cmpq 0x28(%r10), %rcx
jne 0x1dcd76
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r14, %rdx
movq %rdx, 0x28(%r10)
shlq $0x2, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
xorps %xmm2, %xmm2
movq (%rsp), %r10
movq %rax, 0x30(%r10)
movq 0x20(%r10), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r10)
leaq (%rcx,%rcx,8), %rcx
movups %xmm2, 0x10(%rax,%rcx,4)
movups %xmm2, (%rax,%rcx,4)
movl $0x0, 0x20(%rax,%rcx,4)
movq (%r15), %rdx
movl 0xc(%rdx,%rbx), %esi
movl %esi, (%rax,%rcx,4)
movl 0x18(%rdx,%rbx), %edx
shrl $0x1a, %edx
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rax,%rcx,4)
movq (%r15), %rdx
movsd 0x10(%rdx,%rbx), %xmm0
movsd 0x4(%rdx,%rbx), %xmm1
movlhps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movups %xmm1, 0x4(%rax,%rcx,4)
movl (%rdx,%rbx), %edx
movl %edx, 0x14(%rax,%rcx,4)
movq $-0x1, 0x18(%rax,%rcx,4)
incq %r12
addq $0x1c, %rbx
cmpq 0x8(%r15), %r12
jb 0x1dcd3a
movq 0x20(%r10), %rbx
testq %rbx, %rbx
je 0x1dcebe
movq 0x30(%r10), %r14
movq 0x50(%rsp), %rax
movq (%rax), %r15
leaq 0x20(%r14), %r12
xorl %ebp, %ebp
xorl %r13d, %r13d
movl -0x20(%r12), %eax
cmpq %rax, %r15
jbe 0x1dce27
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rax,4)
setns %dl
addq %rdx, %r13
movl -0xc(%r12), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0x1dce96
leaq (%rcx,%rcx,8), %rcx
cmpl $-0x1, 0x1c(%r14,%rcx,4)
jne 0x1dcecf
leaq (%r14,%rcx,4), %rdi
movl %ebp, 0x1c(%rdi)
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rsi
movslq 0x40(%rcx), %rdx
movslq 0x44(%rcx), %rcx
movl (%r12), %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 0x1d4bf8
testq %rax, %rax
js 0x1dce96
movl %eax, -0x8(%r12)
incq %rbp
addq $0x24, %r12
cmpq %rbp, %rbx
jne 0x1dce08
xorl %ebp, %ebp
testq %r13, %r13
sete %bpl
jmp 0x1dcebe
movq $0x0, 0x20(%r10)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66a3b(%rip), %rdi     # 0x243911
leaq 0x62d7e(%rip), %rsi     # 0x23fc5b
leaq 0x669a7(%rip), %rcx     # 0x24388b
movl $0x4762, %edx           # imm = 0x4762
callq 0x2110
leaq 0x66981(%rip), %rdi     # 0x243876
leaq 0x62d5f(%rip), %rsi     # 0x23fc5b
leaq 0x66988(%rip), %rcx     # 0x24388b
movl $0x4725, %edx           # imm = 0x4725
callq 0x2110
leaq 0x669f5(%rip), %rdi     # 0x243909
leaq 0x62d40(%rip), %rsi     # 0x23fc5b
leaq 0x66969(%rip), %rcx     # 0x24388b
movl $0x473c, %edx           # imm = 0x473C
callq 0x2110