// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movq %rsi, %rbx
movq %rdx, 0x8(%rsp)
movq 0x8(%rdx), %rax
movq %rax, (%rsp)
testq %rax, %rax
jle 0x1b0ab8
movq (%rdi), %r15
xorl %r12d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
movq (%rax), %rax
leaq (%r12,%r12,4), %rcx
cmpl $-0x1, 0x8(%rax,%rcx,8)
jne 0x1b0cb9
leaq (%rax,%rcx,8), %r13
movl %ebp, 0x24(%r13)
movl %ebp, (%r13)
leaq 0x28(%r13), %rsi
movl 0x4(%r13), %edx
shlq $0x21, %rdx
sarq $0x21, %rdx
leaq 0x1(%r12), %rcx
movq %r15, %rdi
callq 0x1b07aa
movl 0x4(%r13), %eax
shlq $0x21, %rax
sarq $0x21, %rax
addq %rax, %r14
addq %rax, %r12
incq %r12
cmpq (%rsp), %r12
jl 0x1b08e4
movq $0x0, 0x20(%rbx)
cmpq %r14, 0x28(%rbx)
jae 0x1b09a2
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
leaq 0x1(%rax), %rcx
movq %rcx, 0x28(%rbx)
movq 0x30(%rbx), %rdi
leaq 0x4(,%rax,4), %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
leaq 0x20(%rbx), %r15
cmpq $0x0, (%rsp)
jle 0x1b0ab3
xorl %r13d, %r13d
movq 0x8(%rsp), %rax
movq (%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpl $-0x1, 0x8(%rax,%rcx,8)
jne 0x1b0cd8
leaq (%rax,%rcx,8), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x24(%rsi)
movl %edx, (%rsi)
movl 0x4(%rsi), %r12d
leal (%r12,%r12), %edx
sarl %edx
testl %edx, %edx
jle 0x1b0aa0
movq %rsi, 0x10(%rsp)
shlq $0x21, %r12
sarq $0x21, %r12
leal 0x1(%r13), %ebp
leaq (%rax,%rcx,8), %r14
addq $0x34, %r14
movq 0x20(%rbx), %rcx
movq 0x30(%rbx), %rax
cmpq 0x28(%rbx), %rcx
jne 0x1b0a45
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x28(%rbx)
shlq $0x2, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
leaq (%rcx,%rcx,8), %rcx
movl -0x4(%r14), %edx
movl %edx, %esi
shrl %esi
movl %esi, (%rax,%rcx,4)
shlb $0x5, %dl
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rax,%rcx,4)
movdqu (%r14), %xmm0
pshufd $0x4e, %xmm0, %xmm0     # xmm0 = xmm0[2,3,0,1]
movdqu %xmm0, 0x4(%rax,%rcx,4)
movl %ebp, 0x14(%rax,%rcx,4)
movq $-0x1, 0x18(%rax,%rcx,4)
incl %ebp
addq $0x28, %r14
decq %r12
jne 0x1b0a0b
movq 0x10(%rsp), %rax
movl 0x4(%rax), %edx
addl %edx, %edx
sarl %edx
movslq %edx, %rax
addq %rax, %r13
incq %r13
cmpq (%rsp), %r13
jl 0x1b09b4
movq (%r15), %rcx
jmp 0x1b0ac6
leaq 0x20(%rbx), %r15
movq $0x0, 0x20(%rbx)
xorl %ecx, %ecx
movq %rcx, (%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x30(%rbx), %rdi
leaq (%rcx,%rcx,8), %rax
leaq (%rdi,%rax,4), %rsi
callq 0x19f452
movq (%rsp), %r9
testq %r9, %r9
jle 0x1b0c6b
leaq -0x1(%r9), %r8
movq $-0x1, %r14
movl $0x24, %ebp
xorl %r12d, %r12d
xorl %eax, %eax
movq $-0x1, %r13
movq 0x8(%rsp), %r10
movq %r8, 0x10(%rsp)
movq %r13, %rdx
movq %r12, %rcx
incq %r12
movq 0x30(%rbx), %rsi
movl -0x10(%rsi,%rbp), %r13d
leaq (,%r13,4), %rdi
addq %r13, %rdi
cmpq %r13, %rdx
cmovgq %rdx, %r13
movl %ecx, (%r10,%rdi,8)
cmovlq %rcx, %r14
cmpq %rcx, %r8
je 0x1b0b5f
movl 0x8(%rsi,%rbp), %edx
leaq (%rax,%rax,8), %rdi
cmpl 0x8(%rsi,%rdi,4), %edx
jne 0x1b0b5f
movq %rax, %rdx
jmp 0x1b0b8f
subq %rax, %rcx
incq %rcx
movq %r12, %rdx
cmpq $0x2, %rcx
jl 0x1b0b8f
leaq (%rax,%rax,8), %rax
leaq (%rsi,%rax,4), %rdi
addq %rbp, %rsi
callq 0x19f70e
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %r10
movq (%rsp), %r9
movq %r12, %rdx
addq $0x24, %rbp
movq %rdx, %rax
cmpq %r12, %r9
jne 0x1b0b1b
movq 0x30(%rbx), %rax
leaq 0x14(%rax), %rcx
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl (%rcx), %edi
leaq (%rdi,%rdi,4), %rdi
movl 0x24(%r10,%rdi,8), %edi
cmpq %rsi, %rdi
je 0x1b0bd5
leaq (%rdi,%rdi,4), %rdi
movl (%r10,%rdi,8), %r8d
movl %r8d, (%rcx)
movl 0x20(%r10,%rdi,8), %edi
leaq (%r8,%r8,8), %r8
movl %edx, 0x1c(%rax,%r8,4)
jmp 0x1b0bdf
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, 0x8(%rcx)
movl %edi, (%rcx)
movl %edi, 0x4(%rcx)
incq %rdx
addq $0x24, %rcx
cmpq %rdx, %r9
jne 0x1b0baa
testq %r14, %r14
js 0x1b0c6b
movq 0x30(%rbx), %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %esi, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %eax, %eax
cmpl $-0x1, %r8d
je 0x1b0c33
leaq (%r14,%r14,8), %r9
cmpl %r8d, 0x8(%rdx,%r9,4)
jbe 0x1b0c2d
leaq (%rdx,%r9,4), %r9
movl 0x4(%r9), %r9d
cmpl %r9d, %r8d
cmovael %r9d, %r8d
jmp 0x1b0c40
subl %r8d, %ecx
addq %rcx, %rax
leaq (%r14,%r14,8), %rcx
movl 0x4(%rdx,%rcx,4), %r8d
movl 0x8(%rdx,%rcx,4), %ecx
leaq (%r14,%r14,8), %r9
movl 0x14(%rdx,%r9,4), %r14d
incl %esi
cmpq %rdi, %r14
jne 0x1b0c0b
movl %esi, %esi
leaq 0x50(%rbx), %rdx
movb $0x0, 0x50(%rbx)
subl %r8d, %ecx
xorl %edi, %edi
cmpl $-0x1, %r8d
cmovnel %ecx, %edi
addq %rdi, %rax
jmp 0x1b0c77
leaq 0x50(%rbx), %rdx
movb $0x0, 0x50(%rbx)
xorl %esi, %esi
xorl %eax, %eax
movl 0x18(%rbx), %edi
movb $0x1, %cl
movq %rdi, %r8
subq %rax, %r8
je 0x1b0ca8
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
cvtsi2sd %rdi, %xmm1
mulsd 0x7ff7f(%rip), %xmm1    # 0x230c18
movb $0x2, %cl
ucomisd %xmm0, %xmm1
ja 0x1b0ca8
movb $0x3, %cl
cmpq %rsi, (%r15)
jbe 0x1b0caa
movb %cl, (%rdx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8feb7(%rip), %rdi     # 0x240b77
leaq 0x8ef94(%rip), %rsi     # 0x23fc5b
leaq 0x902cc(%rip), %rcx     # 0x240f9a
movl $0x1372, %edx           # imm = 0x1372
callq 0x2110
leaq 0x8fe98(%rip), %rdi     # 0x240b77
leaq 0x8ef75(%rip), %rsi     # 0x23fc5b
leaq 0x902ad(%rip), %rcx     # 0x240f9a
movl $0x137e, %edx           # imm = 0x137E
callq 0x2110