// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/inter.cpp
// Function: update_ul_vec_t_ug(ul_idx_t const*, ul_vec_t*, vec_mg_lchain_t*, long)
void update_ul_vec_t_ug(const ul_idx_t *uref, ul_vec_t *rch, vec_mg_lchain_t *uc, int64_t ulid)
{
	int64_t k, ucn = uc->n, a_n, m, l, lk; ma_ug_t *ug = uref->ug; mg_lchain_t *ix, *a; uc_block_t *z;
	for (k = 0, a_n = 0; k < ucn; k += ix->cnt + 1) {
		ix = &(uc->a[k]); assert(ix->v == (uint32_t)-1); ix->hash_pre = (uint32_t)-1; ix->off = -1;
		fill_unaligned_alignments(ug, uc->a + k + 1, ix->cnt, k + 1, ulid); a_n += ix->cnt;
	}

	///up to now, given a <x> in swap
	///x->ts and x->te are the coordinates in unitig
	///x->qs and x->qe are the coordinates in UL
	///x->dist_pre is the idx of this chain at rch 
	// debug_intermediate_chain(uref->ug, uc->a, uc->n, 1, debug_i);
	// dd_ul_vec_t(uref, swap->a, swap->n, rch);
	rch->bb.n = 0; kv_resize(uc_block_t, rch->bb, (uint64_t)a_n);
	for (k = 0; k < ucn; k += ix->cnt + 1) {
		ix = &(uc->a[k]); assert(ix->v == (uint32_t)-1); ix->hash_pre = (uint32_t)-1; ix->off = -1;
		a = uc->a + k + 1; a_n = ix->cnt;
		for (m = 0; m < a_n; m++) {
			kv_pushp(uc_block_t, rch->bb, &z); 
			z->hid = (a[m].v>>1); z->rev = (a[m].v&1); 
			z->pchain = 1; z->base = 0; z->el = 1;
			z->qs = a[m].qs; z->qe = a[m].qe;
			z->te = a[m].re; z->ts = a[m].rs;
			z->pidx = k + 1 + m; z->pdis = z->aidx = (uint32_t)-1;
			// fprintf(stderr, "[M::%s::k->%ld] m->%ld, utg%.6dl(%c), q::[%u, %u), t::[%u, %u)\n", 
			// 				__func__, k, m, (int32_t)z->hid+1, "+-"[z->rev], z->qs, z->qe, z->ts, z->te);
		}
	}

	a_n = rch->bb.n; a = uc->a;
	radix_sort_uc_block_t_qe_srt(rch->bb.a, rch->bb.a + rch->bb.n); 
	for (k = 1, lk = 0, l = m = -1; k <= a_n; k++) {
		a[rch->bb.a[k-1].pidx].off = k-1;
		if(m < (int64_t)rch->bb.a[k-1].pidx) {
			m = rch->bb.a[k-1].pidx; l = k-1;
		}
		if(k == a_n || rch->bb.a[k].qe != rch->bb.a[lk].qe) {
			if(k - lk > 1) {
				radix_sort_uc_block_t_qs_srt(rch->bb.a+lk, rch->bb.a+k);
			}
			lk = k;
		}
	}
	// for (k = 0, l = m = -1; k < a_n; k++) {
	// 	a[rch->bb.a[k].pidx].off = k;
	// 	if(m < (int64_t)rch->bb.a[k].pidx) {
	// 		m = rch->bb.a[k].pidx; l = k;
	// 	}
	// }

	for (k = 0; k < a_n; k++) {
		if(a[rch->bb.a[k].pidx].hash_pre == (uint32_t)-1) {
			rch->bb.a[k].pidx = rch->bb.a[k].pdis = rch->bb.a[k].aidx = (uint32_t)-1;
			continue;
		}
		m = rch->bb.a[k].pidx;
		rch->bb.a[k].pidx = a[a[m].hash_pre].off; 
		rch->bb.a[k].pdis = a[a[m].hash_pre].dist_pre;
		rch->bb.a[rch->bb.a[k].pidx].aidx = k;
	}
	// fprintf(stderr, "+ulid->%ld\n", ulid);
	uint32_t sp = (uint32_t)-1, ep = (uint32_t)-1, ch_n = 0; 
	k = l/**a_n - 1**/;///start from the max chain
	for (l = 0; k >= 0; ) {
		if(sp == (uint32_t)-1 || rch->bb.a[k].qe <= sp) {
            if(sp != (uint32_t)-1) l += ep - sp;
            sp = rch->bb.a[k].qs; ep = rch->bb.a[k].qe;
        } else {
            sp = MIN(sp, rch->bb.a[k].qs);
        }
		if(rch->bb.a[k].pidx == (uint32_t)-1) k = -1;
		else k = rch->bb.a[k].pidx;
		ch_n++;
	}
	
	rch->dd = 0;
	if(sp != (uint32_t)-1) l += ep - sp;
	l = (int64_t)rch->rlen - l;

	// fprintf(stderr, "-ulid:%ld, l:%ld, rch->rlen:%u\n", ulid, l, rch->rlen);
	if(l == 0) {
		rch->dd = 1;
	} else if(l < ((int64_t)rch->rlen)*0.001) {
		rch->dd = 2;
	} else if(ch_n < rch->bb.n) {///multiple chain, might be useful for the scaffolding
		rch->dd = 3;
	}
	// fprintf(stderr, "[M::%s::] rch->dd::%u, rch->bb.n::%u\n", 
	// 						__func__, rch->dd, (uint32_t)rch->bb.n);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbp
movq %rsi, %rbx
movq 0x8(%rdx), %rax
movq %rax, 0x8(%rsp)
movq (%rdi), %r15
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq 0x8(%rsp), %r12
jge 0x167c20
movq (%rbp), %r13
imulq $0x28, %r12, %rax
cmpl $-0x1, 0x8(%r13,%rax)
jne 0x167f5c
addq %rax, %r13
orl $-0x1, 0x24(%r13)
orl $-0x1, (%r13)
leaq 0x28(%r13), %rsi
movl 0x4(%r13), %edx
shlq $0x21, %rdx
sarq $0x21, %rdx
leaq 0x1(%r12), %rcx
movq %r15, %rdi
callq 0x167a9e
movl 0x4(%r13), %eax
shlq $0x21, %rax
sarq $0x21, %rax
addq %rax, %r14
addq %rax, %r12
incq %r12
jmp 0x167bc4
andq $0x0, 0x20(%rbx)
cmpq %r14, 0x28(%rbx)
jae 0x167c77
decq %r14
movq %r14, %rax
shrq %rax
orq %r14, %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
incq %rax
movq %rax, 0x28(%rbx)
movq 0x30(%rbx), %rdi
imulq $0x24, %rax, %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
pushq $0x2
popq %r13
xorl %edi, %edi
movq %rbp, 0x18(%rsp)
cmpq 0x8(%rsp), %rdi
jge 0x167d7b
movq (%rbp), %rax
imulq $0x28, %rdi, %rcx
cmpl $-0x1, 0x8(%rax,%rcx)
jne 0x167f7b
leaq (%rax,%rcx), %rdx
orl $-0x1, 0x24(%rdx)
orl $-0x1, (%rdx)
movq %rdx, 0x20(%rsp)
movl 0x4(%rdx), %edx
addl %edx, %edx
sarl %edx
testl %edx, %edx
movl $0x0, %esi
cmovlel %esi, %edx
imulq $0x28, %rdx, %r14
leaq (%rax,%rcx), %r12
addq $0x34, %r12
movq %rdi, 0x10(%rsp)
leal 0x1(%rdi), %ebp
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x167d56
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0x167d0e
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r13, %rdx
movq %rdx, 0x28(%rbx)
imulq $0x24, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x20(%rbx)
imulq $0x24, %rcx, %rcx
movl -0x4(%r12,%r15), %edx
movl %edx, %esi
shrl %esi
movl %esi, (%rax,%rcx)
shlb $0x5, %dl
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rax,%rcx)
movdqu (%r12,%r15), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x4(%rax,%rcx)
movl %ebp, 0x14(%rax,%rcx)
orq $-0x1, 0x18(%rax,%rcx)
addq $0x28, %r15
incl %ebp
jmp 0x167cd8
movq 0x20(%rsp), %rax
movl 0x4(%rax), %eax
shlq $0x21, %rax
sarq $0x21, %rax
movq 0x10(%rsp), %rdi
addq %rax, %rdi
incq %rdi
movq 0x18(%rsp), %rbp
jmp 0x167c82
movq (%rbp), %r15
movq 0x20(%rbx), %r14
movq 0x30(%rbx), %rdi
imulq $0x24, %r14, %rsi
addq %rdi, %rsi
callq 0x1580d5
movq %r14, %r8
xorl %eax, %eax
testq %r14, %r14
movl $0x0, %r10d
cmovgq %r14, %r10
decq %r8
pushq $-0x1
popq %rbp
pushq $0x24
popq %r13
movq %rbp, %r14
xorl %ecx, %ecx
movq %r15, 0x8(%rsp)
movq %r10, 0x10(%rsp)
cmpq %rax, %r10
je 0x167e39
leaq 0x1(%rax), %r12
movq 0x30(%rbx), %rsi
movl -0x10(%rsi,%r13), %edx
imulq $0x28, %rdx, %rdi
cmpq %rdx, %rbp
cmovleq %rdx, %rbp
movl %eax, (%r15,%rdi)
cmovlq %rax, %r14
cmpq %rax, %r8
je 0x167dfc
movl 0x8(%rsi,%r13), %edx
imulq $0x24, %rcx, %rdi
cmpl 0x8(%rsi,%rdi), %edx
jne 0x167dfc
movq %rcx, %rdx
jmp 0x167e2d
subq %rcx, %rax
incq %rax
movq %r12, %rdx
cmpq $0x2, %rax
jl 0x167e2d
imulq $0x24, %rcx, %rdi
addq %rsi, %rdi
addq %r13, %rsi
movq %r8, %r15
callq 0x158386
movq %r15, %r8
movq 0x10(%rsp), %r10
movq 0x8(%rsp), %r15
movq %r12, %rdx
addq $0x24, %r13
movq %r12, %rax
movq %rdx, %rcx
jmp 0x167dbe
pushq $0x14
popq %rax
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
pushq $-0x1
popq %rsi
cmpq %rcx, %r10
je 0x167e97
movq 0x30(%rbx), %rdi
movl (%rdi,%rax), %r8d
imulq $0x28, %r8, %r8
movl 0x24(%r15,%r8), %r8d
cmpq %rdx, %r8
je 0x167e7d
imulq $0x28, %r8, %r8
movl (%r15,%r8), %r9d
movl %r9d, (%rdi,%rax)
movl 0x20(%r15,%r8), %r8d
imulq $0x24, %r9, %r9
movl %ecx, 0x1c(%rdi,%r9)
jmp 0x167e89
orl $-0x1, 0x8(%rdi,%rax)
orl $-0x1, (%rdi,%rax)
movl %esi, %r8d
movl %r8d, 0x4(%rdi,%rax)
incq %rcx
addq $0x24, %rax
jmp 0x167e46
xorl %ecx, %ecx
pushq $-0x1
popq %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
pushq $-0x1
popq %rsi
movl %edi, %r8d
xorl %eax, %eax
testq %r14, %r14
js 0x167f06
movq 0x30(%rbx), %r9
cmpl $-0x1, %edi
je 0x167ed5
imulq $0x24, %r14, %r11
movl 0x8(%r9,%r11), %r10d
cmpl %edi, %r10d
jbe 0x167ee0
addq %r9, %r11
movl 0x4(%r11), %r10d
cmpl %r10d, %edi
cmovael %r10d, %edi
jmp 0x167ef2
imulq $0x24, %r14, %rdi
movl 0x8(%r9,%rdi), %r10d
jmp 0x167ee6
subl %edi, %r8d
addq %r8, %rcx
imulq $0x24, %r14, %rdi
movl 0x4(%r9,%rdi), %edi
movl %r10d, %r8d
imulq $0x24, %r14, %r10
movl 0x14(%r9,%r10), %r14d
incl %eax
cmpq %rdx, %r14
cmoveq %rsi, %r14
jmp 0x167ea9
movb $0x0, 0x50(%rbx)
subl %edi, %r8d
xorl %edx, %edx
cmpl $-0x1, %edi
cmovnel %r8d, %edx
addq %rdx, %rcx
movl 0x18(%rbx), %esi
movb $0x1, %dl
movq %rsi, %rdi
subq %rcx, %rdi
je 0x167f4a
cvtsi2sd %rdi, %xmm0
cvtsi2sd %rsi, %xmm1
mulsd 0x7acc0(%rip), %xmm1    # 0x1e2bf8
movb $0x2, %dl
ucomisd %xmm0, %xmm1
ja 0x167f4a
movl %eax, %eax
movb $0x3, %dl
cmpq %rax, 0x20(%rbx)
jbe 0x167f4d
movb %dl, 0x50(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8aadc(%rip), %rdi     # 0x1f2a3f
leaq 0x89bb9(%rip), %rsi     # 0x1f1b23
leaq 0x8aef1(%rip), %rcx     # 0x1f2e62
movl $0x1372, %edx           # imm = 0x1372
callq 0x2110
leaq 0x8aabd(%rip), %rdi     # 0x1f2a3f
leaq 0x89b9a(%rip), %rsi     # 0x1f1b23
leaq 0x8aed2(%rip), %rcx     # 0x1f2e62
movl $0x137e, %edx           # imm = 0x137E
callq 0x2110