// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: mb_g_cc_core(mb_match_t*)
uint64_t *mb_g_cc_core(mb_match_t *ma)
{
    uint32_t i, x, y, *flag;
    uint64_t *group;
    mb_edge_t *o = NULL;
    kvec_t(uint32_t) stack; kv_init(stack);

    MALLOC(flag, ma->n_seq);
    for (i = 0; i < ma->n_seq; ++i)
        flag[i] = (uint32_t)-1;

    // connected componets
    for (i = 0; i < ma->n_seq; ++i) {
        if (flag[i] != (uint32_t)-1) continue;
        stack.n = 0;
        kv_push(uint32_t, stack, i);
        while (stack.n > 0) {
            uint32_t k, j, n;
            stack.n--;
            k = stack.a[stack.n];
            flag[k] = i;///group id
            // n = (uint32_t)ma->idx[k];
            // s = ma->idx[k] >> 32;
            o = pt_a(*ma, k); 
            n = pt_n(*ma, k);
            for (j = 0; j < n; ++j) {
                uint32_t t = ma_y(o[j]);
                if (flag[t] != (uint32_t)-1) continue;
                // if (ns == ms) PT_EXPAND(stack, ms);
                // stack[ns++] = t;
                kv_push(uint32_t, stack, t);
            }
        }
    }
    kv_destroy(stack);

    // precalculate the size of each group
    CALLOC(group, ma->n_seq);
    for (i = 0; i < ma->n_seq; ++i)
        group[i] = (uint64_t)flag[i] << 32 | i;
    radix_sort_mc64(group, group + ma->n_seq);
    for (i = 1, x = y = 0; i <= ma->n_seq; ++i) {
        if (i == ma->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;
        }
    }
    free(flag);
    return group;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl 0x38(%rdi), %r14d
leaq (,%r14,4), %r15
movq %r15, %rdi
callq 0x2280
testq %r14, %r14
movq %rax, (%rsp)
je 0x16e84f
movq %rax, %rdi
movl $0xff, %esi
movq %r15, %rdx
callq 0x20e0
movq (%rsp), %rdx
xorl %r15d, %r15d
xorl %r13d, %r13d
xorl %edi, %edi
movq %rbx, 0x8(%rsp)
cmpl $-0x1, (%rdx,%r15,4)
jne 0x16e83e
testq %r13, %r13
jne 0x16e79b
movl $0x8, %esi
callq 0x2370
movq (%rsp), %rdx
movq %rax, %rdi
movl $0x2, %r13d
movl %r15d, (%rdi)
movl $0x1, %ebp
movq %r15, 0x10(%rsp)
movl -0x4(%rdi,%rbp,4), %eax
decq %rbp
movl %r15d, (%rdx,%rax,4)
movq 0x10(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0x16e827
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,4), %r14
shlq $0x3, %r14
addq 0x28(%rbx), %r14
movl %eax, %eax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r15
xorl %r12d, %r12d
movq (%r14,%r12), %rbx
movl %ebx, %eax
cmpl $-0x1, (%rdx,%rax,4)
jne 0x16e81e
cmpq %r13, %rbp
jne 0x16e818
leaq (,%rbp,2), %r13
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %r13
leaq (,%r13,4), %rsi
callq 0x2370
movq (%rsp), %rdx
movq %rax, %rdi
movl %ebx, (%rdi,%rbp,4)
incq %rbp
addq $0x28, %r12
cmpq %r12, %r15
jne 0x16e7df
testq %rbp, %rbp
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r15
jne 0x16e7a8
movl 0x38(%rbx), %r14d
incq %r15
movl %r14d, %eax
cmpq %rax, %r15
jb 0x16e774
jmp 0x16e851
xorl %edi, %edi
callq 0x2220
movl 0x38(%rbx), %r12d
movl $0x8, %esi
movq %r12, %rdi
callq 0x2120
movq (%rsp), %rdx
movq %rax, %r15
testq %r12, %r12
je 0x16e88b
xorl %eax, %eax
movl (%rdx,%rax,4), %ecx
shlq $0x20, %rcx
addq %rax, %rcx
movq %rcx, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x16e875
leaq (%r15,%r12,8), %rsi
movq %r15, %rdi
callq 0x166e19
movl 0x38(%rbx), %eax
testq %rax, %rax
je 0x16e8f9
leal 0x1(%rax), %edx
cmpl $0x3, %edx
movl $0x2, %ecx
cmovael %edx, %ecx
xorl %r8d, %r8d
movl $0x1, %edx
xorl %esi, %esi
movl %r8d, %edi
cmpq %rax, %rdx
je 0x16e8cd
movq (%r15,%rdi,8), %r9
xorq (%r15,%rdx,8), %r9
shrq $0x20, %r9
je 0x16e8f1
cmpq %rdx, %rdi
jae 0x16e8ec
movq %rsi, %r8
shlq $0x20, %r8
movl (%r15,%rdi,8), %r9d
orq %r8, %r9
movq %r9, (%r15,%rdi,8)
incq %rdi
cmpq %rdi, %rdx
jne 0x16e8d9
incl %esi
movl %edx, %r8d
incq %rdx
cmpq %rcx, %rdx
jne 0x16e8b7
movq (%rsp), %rdi
callq 0x2220
movq %r15, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq