// File: /workspace/github_cpp_makefile_star2_O2/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 0x18f4aa
movq %r9, %rbp
movq %rcx, %rbx
movq %rdx, %r15
cmpq %rax, 0x8(%r9)
movq %r8, (%rsp)
jae 0x18f013
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(%rbp)
movq 0x10(%rbp), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq (%rsp), %r8
movq %rax, %r13
movq %rax, 0x10(%rbp)
jmp 0x18f017
movq 0x10(%rbp), %r13
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
xorl %eax, %eax
cmpq 0x20(%r8), %rax
jae 0x18f06c
orq $-0x1, (%r13,%rax,8)
movq (%rbx), %rsi
movl (%rsi,%rcx), %edi
cmpq %rdx, %rdi
jne 0x18f040
cmpl $-0x1, 0xc(%rsi,%rcx)
je 0x18f058
movq 0x30(%r8), %r9
imulq $0x24, %rdi, %rdi
movl 0x14(%r9,%rdi), %edi
movl %edi, (%rsi,%rcx)
movq %rbp, (%r13,%rax,8)
incq %rbp
incq %rax
addq $0x1c, %rcx
jmp 0x18f022
movq %rbp, (%r13,%rax,8)
incq %rbp
incq %rax
movq 0x8(%rbx), %r14
cmpq %r14, %rax
jb 0x18f061
movq %rbx, 0x8(%rsp)
andq $0x0, 0x8(%rbx)
cmpq %rbp, 0x10(%r15)
jae 0x18f0d0
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
incq %rcx
movq %rcx, 0x10(%r15)
movq (%r15), %rdi
imulq $0x1c, %rcx, %rsi
callq 0x2370
movq %rax, (%r15)
andq $0x0, 0x8(%r15)
xorl %r12d, %r12d
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %eax, %eax
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x18f1c6
movq (%r13,%rbx,8), %rcx
cmpq $-0x1, %rcx
je 0x18f1ba
movq 0x8(%rsp), %rdx
movq (%rdx), %rax
imulq $0x1c, %rcx, %rcx
movups (%rax,%r12), %xmm0
movups 0xc(%rax,%r12), %xmm1
movups %xmm1, 0xc(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
incq 0x8(%rdx)
movq (%rdx), %rcx
movq (%r13,%rbx,8), %rdx
imulq $0x1c, %rdx, %rax
movl (%rcx,%rax), %esi
cmpq %r8, %rsi
je 0x18f13c
addq %rcx, %rax
movl (%r13,%rsi,8), %esi
movl %esi, (%rax)
movq (%r15), %rax
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
jne 0x18f181
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x10(%r15)
imulq $0x1c, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %rax, (%r15)
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq (%r13,%rbx,8), %rdx
movq 0x8(%r15), %rsi
decq %rbp
imulq $0x1c, %rdx, %rdx
leaq 0x1(%rsi), %rdi
movq %rdi, 0x8(%r15)
imulq $0x1c, %rsi, %rsi
movups (%rcx,%rdx), %xmm0
movups 0xc(%rcx,%rdx), %xmm1
movups %xmm1, 0xc(%rax,%rsi)
movups %xmm0, (%rax,%rsi)
movl (%r13,%rbx,8), %ecx
movq (%r15), %rdx
movq 0x8(%r15), %rax
imulq $0x1c, %rax, %rsi
movl %ecx, -0x1c(%rdx,%rsi)
incq %rbx
addq $0x1c, %r12
jmp 0x18f0e2
testq %rbp, %rbp
jne 0x18f4c9
movq (%r15), %rdi
imulq $0x1c, %rax, %rsi
addq %rdi, %rsi
callq 0x156f5c
pushq $0x1
popq %rbx
pushq $0x1c
popq %r14
xorl %ecx, %ecx
movq (%rsp), %r11
movq 0x10(%rsp), %rbp
pushq $0x2
popq %r12
movq 0x8(%r15), %rax
cmpq %rax, %rbx
ja 0x18f24a
je 0x18f216
movq (%r15), %rax
imulq $0x1c, %rcx, %rdx
movl 0x8(%rax,%rdx), %edx
cmpl 0x8(%rax,%r14), %edx
jne 0x18f216
movq %rcx, %rax
jmp 0x18f23e
movq %rbx, %rdx
subq %rcx, %rdx
movq %rbx, %rax
cmpq $0x2, %rdx
jb 0x18f23e
movq (%r15), %rsi
imulq $0x1c, %rcx, %rdi
addq %rsi, %rdi
addq %r14, %rsi
callq 0x1571fa
movq (%rsp), %r11
movq %rbx, %rax
incq %rbx
addq $0x1c, %r14
movq %rax, %rcx
jmp 0x18f1f4
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x8(%rsp), %rbx
cmpq %rdx, %rax
je 0x18f272
movq (%rbx), %rsi
movq (%r15), %rdi
movl (%rdi,%rcx), %edi
imulq $0x1c, %rdi, %rdi
movl %edx, 0xc(%rsi,%rdi)
incq %rdx
addq $0x1c, %rcx
jmp 0x18f253
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
pushq $-0x1
popq %rsi
xorl %edi, %edi
cmpq %rax, %rdi
jae 0x18f2bc
movq (%rbx), %r8
movq (%r15), %rax
movl (%rax,%rcx), %r9d
imulq $0x1c, %r9, %r9
movl (%r8,%r9), %r9d
movl %esi, %r10d
cmpq %rdx, %r9
je 0x18f2a6
imulq $0x1c, %r9, %r9
movl 0xc(%r8,%r9), %r10d
movl %r10d, (%rax,%rcx)
orb $0x40, 0x1b(%rax,%rcx)
incq %rdi
movq 0x8(%r15), %rax
addq $0x1c, %rcx
jmp 0x18f27e
cmpq %rax, 0x28(%r11)
jae 0x18f312
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
incq %rcx
movq %rcx, 0x28(%r11)
movq 0x30(%r11), %rdi
imulq $0x24, %rcx, %rsi
callq 0x2370
movq (%rsp), %r11
movq %rax, 0x30(%r11)
andq $0x0, 0x20(%r11)
xorl %ebx, %ebx
xorps %xmm2, %xmm2
xorl %r14d, %r14d
cmpq 0x8(%r15), %r14
jae 0x18f3c6
movq 0x20(%r11), %rcx
movq 0x30(%r11), %rax
cmpq 0x28(%r11), %rcx
jne 0x18f361
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r12, %rdx
movq %rdx, 0x28(%r11)
imulq $0x24, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
xorps %xmm2, %xmm2
movq (%rsp), %r11
movq %rax, 0x30(%r11)
movq 0x20(%r11), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%r11)
imulq $0x24, %rcx, %rcx
andl $0x0, 0x20(%rax,%rcx)
movups %xmm2, 0x10(%rax,%rcx)
movups %xmm2, (%rax,%rcx)
movq (%r15), %rdx
movl 0xc(%rdx,%rbx), %esi
movl %esi, (%rax,%rcx)
movl 0x18(%rdx,%rbx), %edx
shrl $0x1a, %edx
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rax,%rcx)
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)
movl (%rdx,%rbx), %edx
movl %edx, 0x14(%rax,%rcx)
orq $-0x1, 0x18(%rax,%rcx)
incq %r14
addq $0x1c, %rbx
jmp 0x18f31f
pushq $0x20
popq %rbx
xorl %r14d, %r14d
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
xorl %r12d, %r12d
cmpq 0x20(%r11), %r12
movq 0x50(%rsp), %rcx
jae 0x18f474
movq 0x30(%r11), %r13
movl -0x20(%r13,%rbx), %eax
cmpq %rax, (%rcx)
jbe 0x18f402
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rax,4)
setns %dl
addq %rdx, %r14
movl -0xc(%r13,%rbx), %ecx
cmpq %r15, %rcx
je 0x18f468
imulq $0x24, %rcx, %rdi
cmpl $-0x1, 0x1c(%r13,%rdi)
jne 0x18f48b
addq %r13, %rdi
movl %r12d, 0x1c(%rdi)
movq 0x8(%rbp), %rsi
movslq 0x40(%rbp), %rdx
movslq 0x44(%rbp), %rcx
movl (%r13,%rbx), %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 0x18842c
testq %rax, %rax
movq (%rsp), %r11
js 0x18f468
movl %eax, -0x8(%r13,%rbx)
incq %r12
addq $0x24, %rbx
jmp 0x18f3d5
xorl %eax, %eax
testq %r14, %r14
sete %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x66347(%rip), %rdi     # 0x1f57d9
leaq 0x6268a(%rip), %rsi     # 0x1f1b23
leaq 0x662b3(%rip), %rcx     # 0x1f5753
movl $0x4762, %edx           # imm = 0x4762
callq 0x2110
leaq 0x6628d(%rip), %rdi     # 0x1f573e
leaq 0x6266b(%rip), %rsi     # 0x1f1b23
leaq 0x66294(%rip), %rcx     # 0x1f5753
movl $0x4725, %edx           # imm = 0x4725
callq 0x2110
leaq 0x66301(%rip), %rdi     # 0x1f57d1
leaq 0x6264c(%rip), %rsi     # 0x1f1b23
leaq 0x66275(%rip), %rcx     # 0x1f5753
movl $0x473c, %edx           # imm = 0x473C
callq 0x2110