// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq 0x8(%rdx), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
jle 0x1a76f1
movq (%rdi), %r15
xorl %r12d, %r12d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq (%r12,%r12,4), %rcx
cmpl $-0x1, 0x8(%rax,%rcx,8)
jne 0x1a7a74
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 0x1a7552
movl 0x4(%r13), %eax
shlq $0x21, %rax
sarq $0x21, %rax
addq %rax, %r14
addq %rax, %r12
incq %r12
cmpq 0x8(%rsp), %r12
jl 0x1a7693
jmp 0x1a76f4
xorl %r14d, %r14d
movq $0x0, 0x20(%rbx)
cmpq %r14, 0x28(%rbx)
jae 0x1a7757
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)
cmpq $0x0, 0x8(%rsp)
jle 0x1a786b
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq (%rax), %rax
leaq (%r15,%r15,4), %rcx
cmpl $-0x1, 0x8(%rax,%rcx,8)
jne 0x1a7a93
leaq (%rax,%rcx,8), %rbp
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %edx, 0x24(%rbp)
movl %edx, (%rbp)
movl 0x4(%rbp), %r14d
leal (%r14,%r14), %edx
sarl %edx
testl %edx, %edx
jle 0x1a784f
shlq $0x21, %r14
sarq $0x21, %r14
leal 0x1(%r15), %r12d
leaq (%rax,%rcx,8), %r13
addq $0x40, %r13
movq 0x28(%rbx), %rax
cmpq %rax, 0x20(%rbx)
jne 0x1a77e5
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x28(%rbx)
movq 0x30(%rbx), %rdi
shlq $0x2, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x30(%rbx)
movq 0x20(%rbx), %rax
movq 0x30(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x20(%rbx)
leaq (%rax,%rax,8), %rax
movl -0x10(%r13), %edx
movl %edx, %esi
shrl %esi
movl %esi, (%rcx,%rax,4)
shlb $0x5, %dl
andb $0x20, %dl
orb $-0x7f, %dl
movb %dl, 0x20(%rcx,%rax,4)
movl -0x4(%r13), %edx
movl %edx, 0x4(%rcx,%rax,4)
movl (%r13), %edx
movl %edx, 0x8(%rcx,%rax,4)
movl -0x8(%r13), %edx
movl %edx, 0x10(%rcx,%rax,4)
movl -0xc(%r13), %edx
movl %edx, 0xc(%rcx,%rax,4)
movl %r12d, 0x14(%rcx,%rax,4)
movq $-0x1, 0x18(%rcx,%rax,4)
incl %r12d
addq $0x28, %r13
decq %r14
jne 0x1a77b2
movl 0x4(%rbp), %eax
shlq $0x21, %rax
sarq $0x21, %rax
addq %rax, %r15
incq %r15
cmpq 0x8(%rsp), %r15
jl 0x1a7766
movq 0x20(%rbx), %r14
movq 0x30(%rbx), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %r12
leaq (%r14,%r14,8), %rax
leaq (%rdi,%rax,4), %rsi
callq 0x1966c5
movq %r14, %r9
testq %r14, %r14
jle 0x1a7937
leaq -0x1(%r9), %r8
movq $-0x1, %r14
movl $0x24, %ebp
xorl %r13d, %r13d
xorl %eax, %eax
movq $-0x1, %r15
movq %r12, 0x10(%rsp)
movq %r9, 0x8(%rsp)
movq %r15, %rdx
movq %r13, %rcx
incq %r13
movq 0x30(%rbx), %rsi
movl -0x10(%rsi,%rbp), %r15d
leaq (%r15,%r15,4), %rdi
cmpq %r15, %rdx
cmovgq %rdx, %r15
movl %ecx, (%r12,%rdi,8)
cmovlq %rcx, %r14
cmpq %rcx, %r8
je 0x1a78f7
movl 0x8(%rsi,%rbp), %edx
leaq (%rax,%rax,8), %rdi
cmpl 0x8(%rsi,%rdi,4), %edx
jne 0x1a78f7
movq %rax, %rdx
jmp 0x1a7929
subq %rax, %rcx
incq %rcx
movq %r13, %rdx
cmpq $0x2, %rcx
jl 0x1a7929
leaq (%rax,%rax,8), %rax
leaq (%rsi,%rax,4), %rdi
addq %rbp, %rsi
movq %r8, %r12
callq 0x196983
movq %r12, %r8
movq 0x8(%rsp), %r9
movq 0x10(%rsp), %r12
movq %r13, %rdx
addq $0x24, %rbp
movq %rdx, %rax
cmpq %r13, %r9
jne 0x1a78ba
jmp 0x1a793e
movq $-0x1, %r14
testq %r9, %r9
jle 0x1a7999
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(%r12,%rdi,8), %edi
cmpq %rsi, %rdi
je 0x1a797f
leaq (%rdi,%rdi,4), %rdi
movl (%r12,%rdi,8), %r8d
movl %r8d, (%rcx)
movl 0x20(%r12,%rdi,8), %edi
movl %edi, 0x4(%rcx)
leaq (%r8,%r8,8), %rdi
movl %edx, 0x1c(%rax,%rdi,4)
jmp 0x1a798d
movl $0xffffffff, 0x8(%rcx)  # imm = 0xFFFFFFFF
movq $-0x1, (%rcx)
incq %rdx
addq $0x24, %rcx
cmpq %rdx, %r9
jne 0x1a7952
xorl %eax, %eax
testq %r14, %r14
js 0x1a7a16
movq 0x30(%rbx), %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq $-0x1, %r10
xorl %r9d, %r9d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
cmpl $-0x1, %esi
je 0x1a79ea
movq 0x30(%rbx), %r11
leaq (%r14,%r14,8), %r15
cmpl %esi, 0x8(%r11,%r15,4)
jbe 0x1a79e5
leaq (%r11,%r15,4), %r11
movl 0x4(%r11), %r11d
cmpl %r11d, %esi
cmovael %r11d, %esi
jmp 0x1a79fc
subl %esi, %ecx
addq %rcx, %rdx
movq 0x30(%rbx), %rcx
leaq (%r14,%r14,8), %r11
movl 0x4(%rcx,%r11,4), %esi
movl 0x8(%rcx,%r11,4), %ecx
leaq (%r14,%r14,8), %r11
movl 0x14(%rdi,%r11,4), %r14d
incl %r9d
cmpq %r8, %r14
cmoveq %r10, %r14
jne 0x1a79c0
movl %r9d, %edi
jmp 0x1a7a24
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %edi, %edi
movb $0x0, 0x50(%rbx)
subl %esi, %ecx
cmpl $-0x1, %esi
cmovel %eax, %ecx
addq %rdx, %rcx
movl 0x18(%rbx), %edx
movb $0x1, %al
movq %rdx, %rsi
subq %rcx, %rsi
je 0x1a7a62
cvtsi2sd %rsi, %xmm0
cvtsi2sd %rdx, %xmm1
mulsd 0x7d186(%rip), %xmm1    # 0x224bd8
movb $0x2, %al
ucomisd %xmm0, %xmm1
ja 0x1a7a62
movb $0x3, %al
cmpq %rdi, 0x20(%rbx)
jbe 0x1a7a65
movb %al, 0x50(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8ce2c(%rip), %rdi     # 0x2348a7
leaq 0x8bf09(%rip), %rsi     # 0x23398b
leaq 0x8d241(%rip), %rcx     # 0x234cca
movl $0x1372, %edx           # imm = 0x1372
callq 0x2110
leaq 0x8ce0d(%rip), %rdi     # 0x2348a7
leaq 0x8beea(%rip), %rsi     # 0x23398b
leaq 0x8d222(%rip), %rcx     # 0x234cca
movl $0x137e, %edx           # imm = 0x137E
callq 0x2110