// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: bi_paration(asg_t*, unsigned long*, unsigned int)
void bi_paration(asg_t *bi_g, uint64_t* array, uint32_t bi_graph_Len)
{
    
    kvec_t(uint64_t) a;
    kv_init(a);
    a.a = array;
    a.n = a.m = bi_g->n_seq;
    kdq_t(uint32_t) *buf;
    buf = kdq_init(uint32_t);
    uint32_t i, len, v, w, k, nv, roundID = 0;
    asg_arc_t *av;
    for (i = 0; i < bi_g->n_seq; i++)
    {
        bi_g->seq[i].c = 0;
    }

    re_partition:

    for (i = 0; i < bi_g->n_seq; i++)
    {
        /****************************may have bugs********************************/
        ///how many reads contained in this contig
        len = (a.a[i]>>33);
        /****************************may have bugs********************************/
        ///if the contig is too small, or the contig has already been visited
        ///if(len < bi_graph_Len || bi_g->seq[i].len != UNVISIT) continue;
        if(len < bi_graph_Len || bi_g->seq[i].c == 1) continue;
        if(roundID == 0 && bi_g->seq[i].len == UNVISIT) continue;

        ///set the color of this node
        bi_g->seq[i].len = RED; bi_g->seq[i].c = 1;
        kdq_push(uint32_t, buf, i);
        while (kdq_size(buf) != 0)
        {
            v = *(kdq_pop(uint32_t, buf)); bi_g->seq[v].c = 1;
            if(bi_g->seq[v].len == ISO) continue;
            nv = asg_arc_n(bi_g, v);
            av = asg_arc_a(bi_g, v);


            ///get all out-nodes of v
            for(k = 0; k < nv; k++)
            {
                w = av[k].v;
                /****************************may have bugs********************************/
                ///if(bi_g->seq[w].len == bi_g->seq[v].len)
                len = (a.a[w]>>33);
                ///only check large contig
                if(len >= bi_graph_Len && bi_g->seq[w].len == bi_g->seq[v].len)
                { /****************************may have bugs********************************/
                    break;
                }
            }
            ///means v is conflict 
            if(k != nv)
            {
                bi_g->seq[v].len = ISO; bi_g->seq[v].c = 1;
                continue;
            }



            ///if v is not conflict with others
            for(k = 0; k < nv; k++)
            {
                w = av[k].v;
                ///if the out-node has not been visited
                if(bi_g->seq[w].len == UNVISIT)
                {
                    bi_g->seq[w].len = 1 - bi_g->seq[v].len;
                    bi_g->seq[w].c = 1;
                    /****************************may have bugs********************************/
                    len = (a.a[w]>>33);
                    /****************************may have bugs********************************/
                    if(len < bi_graph_Len) continue;
                    kdq_push(uint32_t, buf, w);
                }
                ///here bi_g->seq[w].len might be ISO or another color
                ///don't need to do anything here
            }
        }
    }

    if(roundID == 0)
    {
        roundID = 1;
        goto re_partition;
    }
    

    //secondary checking
    // uint32_t a_color;
    // kdq_size(buf) = 0;
    // for (i = 0; i < bi_g->n_seq; i++)
    // {
    //     /****************************may have bugs********************************/
    //     len = (a.a[i]>>33);
    //     /****************************may have bugs********************************/
    //     ///just check end contig
    //     if(bi_g->seq[i].len != UNVISIT || (a.a[i] & (uint64_t)(0x100000000)) == 0) continue;
        
    //     v = i;
    //     nv = asg_arc_n(bi_g, v);
    //     av = asg_arc_a(bi_g, v);
    //     a_color = UNVISIT;

    //     for(k = 0; k < nv; k++)
    //     {
    //         w = av[k].v;
    //         ///check all out-nodes that has already been colored
    //         if(bi_g->seq[w].len != UNVISIT)
    //         {
    //             ///if this is the first colored node
    //             if(a_color == UNVISIT)
    //             {
    //                 a_color = bi_g->seq[w].len;
    //             }///if this is not
    //             else if(a_color != bi_g->seq[w].len)
    //             {
    //                 a_color = ISO;
    //                 bi_g->seq[v].len = ISO;
    //                 break;
    //             }
    //         }
    //     }

    //     if(a_color == ISO) continue;
    //     ///no colored out-node
    //     if(a_color == UNVISIT)
    //     {
    //         bi_g->seq[v].len = RED;
    //     }
    //     else
    //     {
    //         bi_g->seq[v].len = 1 - a_color;
    //     }



        

    //     ///check if all reachable nodes are end-contig
    //     kdq_push(uint32_t, buf, i);
    //     while (kdq_size(buf) != 0)
    //     {
    //         v = *(kdq_pop(uint32_t, buf));
    //         ///find a non-end contig
    //         if((a.a[v] & (uint64_t)(0x100000000)) == 0)
    //         {
    //             bi_g->seq[i].len = UNVISIT;
    //             kdq_size(buf) = 0;
    //             break;
    //         }

    //         nv = asg_arc_n(bi_g, v);
    //         av = asg_arc_a(bi_g, v);
    //         for(k = 0; k < nv; k++)
    //         {
    //             w = av[k].v;
    //             ///if the out-node has not been visited
    //             if(bi_g->seq[w].len == UNVISIT)
    //             {
    //                 bi_g->seq[v].len = LABLE;
    //                 kdq_push(uint32_t, buf, w);
    //             }
    //         }
    //     }

    //     for (v = 0; v < bi_g->n_seq; v++)
    //     {
    //         if(bi_g->seq[v].len == LABLE) bi_g->seq[v].len = UNVISIT;
    //     }

    //     if(bi_g->seq[i].len == UNVISIT)
    //     {
    //         continue;
    //     }






    //     ///now all reachable nodes of i are end-contigs
    //     kdq_push(uint32_t, buf, i);
    //     while (kdq_size(buf) != 0)
    //     {
    //         ///each v here is the uncolored node in the first round
    //         v = *(kdq_pop(uint32_t, buf));
    //         if(bi_g->seq[v].len == ISO) continue;
    //         nv = asg_arc_n(bi_g, v);
    //         av = asg_arc_a(bi_g, v);


    //         ///get all out-nodes of v
    //         for(k = 0; k < nv; k++)
    //         {
    //             w = av[k].v;
    //             if(bi_g->seq[w].len == bi_g->seq[v].len)
    //             {
    //                 break;
    //             }
    //         }
    //         ///means v is conflict 
    //         if(k != nv)
    //         {
    //             bi_g->seq[v].len = ISO;
    //             continue;
    //         }



    //         ///if v is not conflict with others
    //         for(k = 0; k < nv; k++)
    //         {
    //             w = av[k].v;
    //             ///if the out-node has not been visited
    //             if(bi_g->seq[w].len == UNVISIT)
    //             {   
    //                 bi_g->seq[w].len = 1 - bi_g->seq[v].len;
    //                 kdq_push(uint32_t, buf, w);
    //             }
    //         }
    //     }
    // }

    kdq_destroy(uint32_t, buf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r14
movl $0x10, %edi
callq 0x2280
movq %r14, %r8
movq %rax, %r9
movl 0x14(%r14), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xeb938
movq 0x20(%r8), %rcx
xorl %edx, %edx
movb $0x0, 0x4(%rcx,%rdx,8)
incq %rdx
movl 0x14(%r8), %eax
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpq %rsi, %rdx
jb 0xeb91f
movb $0x1, %r11b
movabsq $0x800000000000000, %r14 # imm = 0x800000000000000
movl $0x3, %edx
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movl %r13d, 0x2c(%rsp)
movq %rbx, 0x38(%rsp)
movq %r8, 0x8(%rsp)
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xebcf1
xorl %r15d, %r15d
movl %r11d, 0x44(%rsp)
movq (%rbx,%r15,8), %rcx
shrq $0x21, %rcx
cmpl %r13d, %ecx
jb 0xebcdd
movq 0x20(%r8), %rcx
cmpb $0x1, 0x4(%rcx,%r15,8)
je 0xebcdd
movl (%rcx,%r15,8), %esi
movl %esi, %edi
notl %edi
testl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
sete %dil
testb %dil, %r11b
jne 0xebcdd
andl $0x80000000, %esi       # imm = 0x80000000
movl %esi, (%rcx,%r15,8)
movq 0x20(%r8), %rax
movb $0x1, 0x4(%rax,%r15,8)
movq %r14, %rbx
movq %r14, %rax
andq %r10, %rax
andq %rdx, %rax
movq %r15, 0x48(%rsp)
movl %r15d, (%r9,%rax,4)
movl $0x1, %r12d
movq %rbx, %rax
andq %r10, %rax
addq %r12, %rax
decq %rax
decq %r12
andq %rdx, %rax
movl (%r9,%rax,4), %r11d
movq 0x20(%r8), %rax
movb $0x1, 0x4(%rax,%r11,8)
movl (%rax,%r11,8), %ecx
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl $0x2, %esi
je 0xebcbe
movq %rbx, 0x10(%rsp)
movq %r9, 0x20(%rsp)
movq 0x8(%r8), %rdi
movq 0x28(%r8), %rsi
movq %r11, 0x18(%rsp)
movq (%rsi,%r11,8), %rsi
movq %rsi, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
testl %esi, %esi
movq 0x38(%rsp), %r15
je 0xeba7e
movl %esi, %r10d
leaq (%rdi,%r8,8), %r11
addq $0x8, %r11
xorl %r9d, %r9d
movl (%r11), %ebx
movq (%r15,%rbx,8), %r14
shrq $0x21, %r14
cmpl %r13d, %r14d
jb 0xeba70
movl (%rax,%rbx,8), %ebx
xorl %ecx, %ebx
testl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
je 0xeba81
incq %r9
addq $0x18, %r11
cmpq %r9, %r10
jne 0xeba53
jmp 0xeba8a
xorl %r9d, %r9d
cmpl %esi, %r9d
jne 0xebc84
testl %esi, %esi
je 0xebca5
leaq (%rdi,%r8,8), %r11
movl %esi, %r14d
xorl %ebp, %ebp
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r9
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %r11, 0x30(%rsp)
movq %r14, 0x50(%rsp)
leaq (,%rbp,2), %rax
addq %rbp, %rax
movl 0x8(%r11,%rax,8), %r15d
movq 0x20(%r8), %rax
movl (%rax,%r15,8), %ecx
movl %ecx, %esi
notl %esi
testl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
jne 0xebc76
movl $0x1, %esi
subl (%rax,%rdi,8), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, (%rax,%r15,8)
movq 0x20(%r8), %rax
movb $0x1, 0x4(%rax,%r15,8)
movq 0x38(%rsp), %rax
movq (%rax,%r15,8), %rax
shrq $0x21, %rax
cmpl %r13d, %eax
jb 0xebc76
movq %rbx, %rax
shrq $0x3a, %rax
movl $0x1, %esi
movl %eax, %ecx
shlq %cl, %rsi
cmpq %rsi, %r12
jne 0xebc63
leal 0x1(%rax), %r13d
movl $0x1, %esi
movl %r13d, %ecx
shlq %cl, %rsi
cmpq %r12, %rsi
jae 0xebb90
movq $-0x1, %r13
incq %r13
movq %r12, %rsi
movl %r13d, %ecx
shrq %cl, %rsi
cmpq $0x3f, %r13
ja 0xebb71
testq %rsi, %rsi
jne 0xebb5a
cmpq %r13, %rax
jne 0xebb80
movl 0x2c(%rsp), %r13d
jmp 0xebc63
movl $0x1, %esi
movl %r13d, %ecx
shlq %cl, %rsi
cmpq %rax, %r13
jbe 0xebbb6
movq %rsi, %r14
movl $0x4, %esi
movl %r13d, %ecx
shlq %cl, %rsi
movq %r9, %rdi
callq 0x2370
movq %rax, %r9
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq %r14, %rsi
movq %rbx, %r14
andq %r10, %r14
leaq (%r14,%r12), %rdx
cmpq %r12, %rdx
jbe 0xebc0b
movq %r12, %rdx
subq %r14, %rdx
movq %rsi, %rax
subq %rdx, %rax
leaq (%r9,%rax,4), %rdi
movq %rsi, 0x10(%rsp)
leaq (%r9,%r14,4), %rsi
shlq $0x2, %rdx
movq %r9, 0x20(%rsp)
callq 0x2360
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq 0x20(%rsp), %r9
subq %r12, %rbx
addq 0x10(%rsp), %rbx
andq %r10, %rbx
movq %rbx, %r14
jmp 0xebc30
subq %rsi, %rdx
jbe 0xebc30
leaq (%r9,%rsi,4), %rsi
shlq $0x2, %rdx
movq %r9, %rdi
movq %r9, %rbx
callq 0x2360
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq %rbx, %r9
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %r11
movq %r13, %rbx
shlq $0x3a, %rbx
orq %r14, %rbx
movq $-0x1, %rdx
movl %r13d, %ecx
shlq %cl, %rdx
notq %rdx
movl 0x2c(%rsp), %r13d
movq 0x50(%rsp), %r14
movq %rbx, %rax
andq %r10, %rax
addq %r12, %rax
incq %r12
andq %rdx, %rax
movl %r15d, (%r9,%rax,4)
incq %rbp
cmpq %r14, %rbp
jne 0xebac3
jmp 0xebcbe
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x2, %ecx
movq 0x18(%rsp), %rsi
movl %ecx, (%rax,%rsi,8)
movq 0x8(%rsp), %r8
movq 0x20(%r8), %rax
movb $0x1, 0x4(%rax,%rsi,8)
jmp 0xebcaa
movq 0x8(%rsp), %r8
movq 0x20(%rsp), %r9
movabsq $0x3ffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFF
movq 0x10(%rsp), %rbx
testq %r12, %r12
jne 0xeb9e1
movq %rbx, %r14
movl 0x14(%r8), %eax
movq 0x38(%rsp), %rbx
movl 0x44(%rsp), %r11d
movq 0x48(%rsp), %r15
incq %r15
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r15
jb 0xeb976
testb $0x1, %r11b
movl $0x0, %r11d
jne 0xeb963
movq %r9, %rdi
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220