// File: /workspace/github_cpp_makefile_star2_O3/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), %r14d
movl %r14d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
leaq (,%rbx,4), %r13
movq %r13, %rdi
callq 0x2280
movq %rax, %r15
leaq (,%rbx,8), %rdi
callq 0x2280
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq %r15, %rdi
movl $0xff, %esi
movq %r13, %rdx
callq 0x20e0
testq %rbx, %rbx
je 0x144d9d
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %edi, %edi
cmpl $-0x1, (%r15,%r12,4)
jne 0x144d87
testq %rbx, %rbx
jne 0x144c47
movl $0x8, %esi
callq 0x2370
movq %rax, %rdi
movl $0x2, %ebx
movl %r12d, (%rdi)
movl $0x1, %r14d
movq %r12, 0x20(%rsp)
movl -0x4(%rdi,%r14,4), %eax
movl %r12d, (%r15,%rax,4)
movl -0x4(%rdi,%r14,4), %esi
decq %r14
addl %esi, %esi
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rsi,8), %rdx
testl %edx, %edx
je 0x144cfc
movq %rsi, 0x28(%rsp)
movq %rdx, %rcx
shrq $0x20, %rcx
movl %edx, %edx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %r12
addq $0x8, %r12
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rbp
xorl %r13d, %r13d
movl (%r12,%r13), %eax
shrl %eax
cmpl $-0x1, (%r15,%rax,4)
jne 0x144ce1
cmpq %rbx, %r14
jne 0x144cda
leaq (%r14,%r14), %rbx
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x2370
movq %rax, %rdi
movl (%r12,%r13), %eax
shrl %eax
movl %eax, (%rdi,%r14,4)
incq %r14
addq $0x18, %r13
cmpq %r13, %rbp
jne 0x144ca2
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq 0x28(%rsp), %rsi
movl %esi, %edx
movq 0x8(%rcx,%rdx,8), %rcx
testl %ecx, %ecx
je 0x144d70
movq %rcx, %rdx
shrq $0x20, %rdx
movl %ecx, %ecx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %r13
addq $0x8, %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %r12
xorl %ebp, %ebp
movl (%r13,%rbp), %eax
shrl %eax
cmpl $-0x1, (%r15,%rax,4)
jne 0x144d67
cmpq %rbx, %r14
jne 0x144d60
leaq (%r14,%r14), %rbx
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x2370
movq %rax, %rdi
movl (%r13,%rbp), %eax
shrl %eax
movl %eax, (%rdi,%r14,4)
incq %r14
addq $0x18, %rbp
cmpq %rbp, %r12
jne 0x144d26
testq %r14, %r14
movq 0x20(%rsp), %r12
jne 0x144c55
movq 0x8(%rsp), %rax
movl 0x14(%rax), %r14d
incq %r12
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x144c25
jmp 0x144d9f
xorl %edi, %edi
callq 0x2220
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
je 0x144dd9
xorl %ecx, %ecx
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rdi
movl (%r15,%rcx,4), %edx
shlq $0x20, %rdx
addq %rcx, %rdx
movq %rdx, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x144dc0
jmp 0x144de5
xorl %eax, %eax
movq 0x18(%rsp), %r14
movq 0x10(%rsp), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1379f9
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x144e55
leal 0x1(%rcx), %edx
xorl %esi, %esi
movl $0x1, %r11d
xorl %r9d, %r9d
movl %r9d, %r8d
cmpq %rcx, %r11
je 0x144e29
movq (%rdi,%r8,8), %r10
xorq (%rdi,%r11,8), %r10
shrq $0x20, %r10
je 0x144e4d
cmpq %r11, %r8
jae 0x144e48
movq %rsi, %r9
shlq $0x20, %r9
movl (%rdi,%r8,8), %r10d
orq %r9, %r10
movq %r10, (%rdi,%r8,8)
incq %r8
cmpq %r8, %r11
jne 0x144e35
incl %esi
movl %r11d, %r9d
incq %r11
cmpq %rdx, %r11
jne 0x144e13
movq (%r14), %rsi
testq %rsi, %rsi
je 0x144efe
movq 0x10(%r14), %rcx
xorl %eax, %eax
leaq (%rax,%rax,2), %rdx
shlq $0x4, %rdx
cmpq $0x0, (%rcx,%rdx)
je 0x144ee3
movl $0x1, %esi
movq %rax, %r8
shlq $0x4, %r8
leaq (%r8,%r8,2), %r11
xorl %r8d, %r8d
movq 0x10(%rcx,%r11), %rcx
shlq $0x3, %r8
leaq (%r8,%r8,4), %r9
movq $0x0, 0x20(%rcx,%r9)
andb $-0x80, 0x18(%rcx,%r9)
movq 0x10(%r14), %rcx
movq 0x10(%rcx,%r11), %r8
movl 0x8(%r8,%r9), %r10d
movq (%rdi,%r10,8), %r10
xorq (%rdi,%rax,8), %r10
shrq $0x20, %r10
jne 0x144ed5
addq %r9, %r8
movb 0x18(%r8), %cl
andb $-0x80, %cl
incb %cl
movb %cl, 0x18(%r8)
movq 0x10(%r14), %rcx
movl %esi, %r8d
incl %esi
cmpq %r8, (%rcx,%rdx)
ja 0x144e89
movq (%r14), %rsi
incl %eax
cmpq %rax, %rsi
ja 0x144e67
movq 0x8(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r13d, %r13d
testl %ecx, %ecx
je 0x144f68
movl $0x1, %ebx
leaq 0x30(%rsp), %rbp
movq 0x8(%rsp), %r12
movl %ecx, %ecx
cmpq %rcx, %rbx
je 0x144f2c
movl %r13d, %ecx
movq (%rdi,%rcx,8), %rcx
xorq (%rdi,%rbx,8), %rcx
shrq $0x20, %rcx
je 0x144f4f
movl %ebx, %edx
subl %r13d, %edx
movl %r13d, %esi
movq %r14, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x1448dd
movq 0x10(%rsp), %rdi
movl 0x14(%r12), %eax
movl %ebx, %r13d
leaq 0x1(%rbx), %rdx
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
movq %rdx, %rbx
jb 0x144f14
movq 0x40(%rsp), %r13
movq %r15, %rdi
callq 0x2220
movq %r13, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220