// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: update_ug_by_tigs(asg_t*, hc_links*)
void update_ug_by_tigs(asg_t *sg, hc_links *link)
{
    uint32_t i, m, v, nv, x, y, qn, tn;
    asg_arc_t *av = NULL;
	kvec_t(uint32_t) stack; kv_init(stack);
    uint32_t *flag = NULL; MALLOC(flag, sg->n_seq);
    uint64_t *group = NULL; MALLOC(group, sg->n_seq);
    kvec_t_u32_warp buf; kv_init(buf.a);


    memset(flag, -1, sg->n_seq*sizeof(uint32_t));
	// connected componets
	for (i = 0; i < sg->n_seq; ++i) {
		if (flag[i] != (uint32_t)-1) continue;
		stack.n = 0;
		kv_push(uint32_t, stack, i);
		while (stack.n > 0) {
			stack.n--;
			flag[stack.a[stack.n]] = i;///group id

            v = (stack.a[stack.n])<<1;
            av = asg_arc_a(sg, v);
            nv = asg_arc_n(sg, v);
			for (m = 0; m < nv; ++m) {
				if (flag[av[m].v>>1] != (uint32_t)-1) continue;
				kv_push(uint32_t, stack, av[m].v>>1);
			}

            v++;
            av = asg_arc_a(sg, v);
            nv = asg_arc_n(sg, v);
			for (m = 0; m < nv; ++m) {
				if (flag[av[m].v>>1] != (uint32_t)-1) continue;
				kv_push(uint32_t, stack, av[m].v>>1);
			}
		}
	}
	kv_destroy(stack);



	// precalculate the size of each group
	for (i = 0; i < sg->n_seq; ++i)
		group[i] = (uint64_t)flag[i] << 32 | i;
    radix_sort_hc64(group, group + sg->n_seq);
	for (i = 1, x = y = 0; i <= sg->n_seq; ++i) {
		if (i == sg->n_seq || (group[i]>>32) != (group[x]>>32)) {
			uint32_t j;
			for (j = x; j < i; ++j)
				group[j] = (uint64_t)y << 32 | (uint32_t)group[j];///(group id)|first element in this group
			++y, x = i;
		}
	}


    for (i = 0; i < link->a.n; i++)
    {
        qn = i; 
        for (m = 0; m < link->a.a[i].e.n; m++)
        {
            link->a.a[i].e.a[m].occ = 0;
            link->a.a[i].e.a[m].is_cc = 0;
            tn = link->a.a[i].e.a[m].uID;
            if((group[qn]>>32) == (group[tn]>>32))
            {
                link->a.a[i].e.a[m].is_cc = 1;
            }
        }
    }


    for (i = 1, x = 0; i <= sg->n_seq; ++i) 
    {
		if (i == sg->n_seq || (group[i]>>32) != (group[x]>>32)) 
        {
            append_tig_link(group, x, i - x, link, sg, &buf);
			x = i;
		}
	}

    free(flag); kv_destroy(buf.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
movl 0x14(%rdi), %ebx
movl %ebx, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
leaq (,%r12,4), %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %r15
shlq $0x3, %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movq %r15, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
xorl %r14d, %r14d
xorl %edi, %edi
xorl %eax, %eax
movq %r12, 0x20(%rsp)
movl %ebx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r14
jae 0xfd72a
cmpl $-0x1, (%r15,%r14,4)
jne 0xfd722
testq %rax, %rax
jne 0xfd5f0
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, %rdi
pushq $0x2
popq %rax
movq %r14, 0x10(%rsp)
movl %r14d, (%rdi)
pushq $0x1
popq %rbp
testq %rbp, %rbp
je 0xfd710
movl -0x4(%rdi,%rbp,4), %ecx
movq 0x10(%rsp), %rdx
movl %edx, (%r15,%rcx,4)
movl -0x4(%rdi,%rbp,4), %esi
decq %rbp
addl %esi, %esi
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movq %rsi, 0x28(%rsp)
movq (%rdx,%rsi,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x18, %rsi, %rbx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r12
addq $0x8, %r12
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0xfd693
movl (%r12,%r13), %ecx
shrl %ecx
cmpl $-0x1, (%r15,%rcx,4)
jne 0xfd68d
cmpq %rax, %rbp
jne 0xfd687
leaq (%rax,%rax), %r14
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
callq 0x2370
movq %rax, %rdi
movl (%r12,%r13), %ecx
shrl %ecx
movq %r14, %rax
movl %ecx, (%rdi,%rbp,4)
incq %rbp
addq $0x18, %r13
jmp 0xfd649
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %rcx
movq 0x28(%rdx), %rdx
movl 0x28(%rsp), %esi
movq 0x8(%rdx,%rsi,8), %rdx
movl %edx, %esi
shrq $0x20, %rdx
imulq $0x18, %rsi, %rbx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r14
addq $0x8, %r14
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0xfd5fb
movl (%r14,%r12), %ecx
shrl %ecx
cmpl $-0x1, (%r15,%rcx,4)
jne 0xfd70a
cmpq %rax, %rbp
jne 0xfd704
leaq (%rax,%rax), %r13
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r13
leaq (,%r13,4), %rsi
callq 0x2370
movq %rax, %rdi
movl (%r14,%r12), %ecx
shrl %ecx
movq %r13, %rax
movl %ecx, (%rdi,%rbp,4)
incq %rbp
addq $0x18, %r12
jmp 0xfd6c2
movq 0x8(%rsp), %rcx
movl 0x14(%rcx), %ebx
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %r14
incq %r14
jmp 0xfd5c1
callq 0x2220
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
movq 0x18(%rsp), %r14
cmpq %rcx, %rax
je 0xfd75c
movl (%r15,%rcx,4), %edx
shlq $0x20, %rdx
addq %rcx, %rdx
movq %rdx, (%r12,%rcx,8)
incq %rcx
jmp 0xfd743
leaq (%r12,%rax,8), %rsi
movq %r12, %rdi
callq 0xf116c
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
leal 0x1(%rax), %ecx
xorl %edi, %edi
pushq $0x1
popq %rdx
xorl %esi, %esi
cmpq %rcx, %rdx
je 0xfd7bf
movl %edi, %r8d
cmpq %rax, %rdx
je 0xfd79a
movq (%r12,%r8,8), %r9
xorq (%r12,%rdx,8), %r9
shrq $0x20, %r9
je 0xfd7ba
movq %rsi, %rdi
shlq $0x20, %rdi
cmpq %rdx, %r8
jae 0xfd7b6
movl (%r12,%r8,8), %r9d
orq %rdi, %r9
movq %r9, (%r12,%r8,8)
incq %r8
jmp 0xfd7a1
incl %esi
movl %edx, %edi
incq %rdx
jmp 0xfd77f
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, (%r14)
jbe 0xfd826
xorl %ecx, %ecx
imulq $0x30, %rax, %rdx
movl %ecx, %ecx
movq 0x10(%r14), %rsi
cmpq %rcx, (%rsi,%rdx)
jbe 0xfd822
addq %rdx, %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rcx, %rdi
andq $0x0, 0x20(%rsi,%rdi)
andb $-0x80, 0x18(%rsi,%rdi)
movq 0x10(%r14), %rsi
movq 0x10(%rsi,%rdx), %rsi
movl 0x8(%rsi,%rdi), %r8d
movq (%r12,%r8,8), %r8
xorq (%r12,%rax,8), %r8
shrq $0x20, %r8
jne 0xfd81e
addq %rdi, %rsi
movb 0x18(%rsi), %dil
andb $-0x80, %dil
incb %dil
movb %dil, 0x18(%rsi)
incl %ecx
jmp 0xfd7ce
incl %eax
jmp 0xfd7c1
xorl %esi, %esi
pushq $0x1
popq %rbx
leaq 0x30(%rsp), %r13
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %rbx
ja 0xfd872
je 0xfd854
movl %esi, %eax
movq (%r12,%rax,8), %rax
xorq (%r12,%rbx,8), %rax
shrq $0x20, %rax
je 0xfd86d
movl %ebx, %edx
subl %esi, %edx
movq %r12, %rdi
movq %r14, %rcx
movq 0x8(%rsp), %r8
movq %r13, %r9
callq 0xfd2ad
movl %ebx, %esi
incq %rbx
jmp 0xfd830
movq %r15, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220