// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: assign_per_unitig_G_partition(G_partition*, unsigned long, hc_links*, bubble_type*, unsigned int)
void assign_per_unitig_G_partition(G_partition* g_p, uint64_t hap_n, hc_links* link, bubble_type* bub,
uint32_t bubble_first)
{
    double index_time = yak_realtime();
    reset_G_partition(g_p, hap_n);

    partition_warp* res = NULL;
    hc_edge *a = NULL;
    uint32_t i, a_n, v, u, uv = (uint32_t)-1, k, k_n, k_nv, k_nu, beg, sink, *b_a = NULL, b_a_n;
    
    if(bubble_first)
    {
        int c;
        kvec_t_u32_warp stack; kv_init(stack.a);
        uint8_t *color = NULL; CALLOC(color, hap_n);
        uint32_t n_bub = bub->f_bub + bub->b_bub;
        for (i = 0; i < n_bub; i++)
        {
            get_bubbles(bub, i, &beg, &sink, &b_a, &b_a_n, NULL);
            if(b_a_n == 2 && i < bub->f_bub)
            {
                continue;
            }
            ///full bubble do not overlap with any others
            ///broken bubbles might be, but should do nothing
            c = graph_bipartiteness(b_a, b_a_n, color, link, &stack);

            if(c == 0)
            {
                fprintf(stderr, "too good: s-utg%.6ul && e-utg%.6ul && %s\n",(beg>>1)+1, (sink>>1)+1, b_a_n != 4? "abnormal" : "normal");
            }
            if(c == -1)
            {
                fprintf(stderr, "too bad: s-utg%.6ul && e-utg%.6ul\n",(beg>>1)+1, (sink>>1)+1);
            }
            if(c == 1)
            {
                fprintf(stderr, "\nprefect=%u: s-utg%.6ul && e-utg%.6ul\n", b_a_n, (beg>>1)+1, (sink>>1)+1);
                
                
                for (k = 0; k < b_a_n; k++)
                {
                    if((color[b_a[k]>>1] & 2) == 0) fprintf(stderr, "ERROR\n");
                    if((color[b_a[k]>>1] & 4) == 0) fprintf(stderr, "0: utg%.6ul\n", (b_a[k]>>1)+1);
                }

                for (k = 0; k < b_a_n; k++)
                {
                    if((color[b_a[k]>>1] & 2) == 0) fprintf(stderr, "ERROR\n");
                    if((color[b_a[k]>>1] & 4) != 0) fprintf(stderr, "1: utg%.6ul\n", (b_a[k]>>1)+1);
                }

                for (k = 0; k < b_a_n; k++) color[b_a[k]>>1] = 0;
            }
        }
        free(color);
        kv_destroy(stack.a);
    }

    for (i = 0; i < hap_n; i++)
    {
        v = i;
        a = link->a.a[v].f.a;
        a_n = link->a.a[v].f.n;
        for (k = k_n = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            if(a[k].dis != RC_0) break;
            u = a[k].uID;
            k_n++;
        }
        if(k_n != 1)
        {   
            u = (uint32_t)-1;
            goto push_uv;
        } 
        
        a = link->a.a[u].f.a;
        a_n = link->a.a[u].f.n;
        for (k = k_n = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            if(a[k].dis != RC_0) break;
            uv = a[k].uID;
            k_n++;
        }
        if(k_n != 1 || uv != v)
        {
            u = (uint32_t)-1;
            goto push_uv;
        } 

        push_uv:
        k_nv = 0;k_nu = 0;

        // not such easy. need to deal with here very carefully
        // if(g_p->index[v] != (uint32_t)-1) continue; 
        // if(u != (uint32_t)-1 && g_p->index[u] != (uint32_t)-1) u = (uint32_t)-1;

        a = link->a.a[v].e.a;
        a_n = link->a.a[v].e.n;
        for (k = 0; k < a_n; k++)
        {
            if(a[k].del) continue;
            k_nv++;
        }
                
        if(u != (uint32_t)-1)
        {
            a = link->a.a[u].e.a;
            a_n = link->a.a[u].e.n;
            for (k = 0; k < a_n; k++)
            {
                if(a[k].del) continue;
                k_nu++;
            }
        }
        if(k_nv == 0) continue;
        if(k_nv > 0 && k_nu > 0 && v > u) continue;

        kv_pushp(partition_warp, *g_p, &res);
        kv_init(res->a);
        res->full_bub = 0;
        res->h[0] = 1; res->h[1] = 0;
        kv_push(uint32_t, res->a, v);
        if(u != (uint32_t)-1)
        {
            res->h[1] = 1;
            kv_push(uint32_t, res->a, u);
        }

        for (k = 0; k < res->h[0]; k++)
        {
            g_p->index[res->a.a[k]] = g_p->n-1;
            g_p->index[res->a.a[k]] = g_p->index[res->a.a[k]] << 1;
        } 

        for (; k < res->a.n; k++)
        {
            g_p->index[res->a.a[k]] = g_p->n-1;
            g_p->index[res->a.a[k]] = (g_p->index[res->a.a[k]] << 1) + 1;
        } 
    }
    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %ebx
movq %rcx, (%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, %r14
callq 0x1303c7
movsd %xmm0, 0x28(%rsp)
movq $0x0, (%r14)
testq %r15, %r15
je 0x1508a8
movq 0x18(%r14), %rdi
leaq (,%r15,4), %rdx
movl $0xff, %esi
callq 0x20e0
testl %ebx, %ebx
je 0x150909
movq %r14, 0x20(%rsp)
movl $0x1, %esi
movq %r15, 0x18(%rsp)
movq %r15, %rdi
callq 0x2120
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq (%rsp), %rsi
movq 0x88(%rsi), %rax
addq 0x80(%rsi), %rax
testl %eax, %eax
jne 0x15092e
movq %r13, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movsd 0x28(%rsp), %xmm0
callq 0x1d56e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
xorl %r12d, %r12d
movq %rdi, 0x8(%rsp)
movq 0x28(%rsi), %rax
movq 0x40(%rsi), %rdx
movl (%rdx,%r12,4), %ecx
movl 0x4(%rdx,%r12,4), %r15d
subl %ecx, %r15d
addl $-0x2, %r15d
movl (%rax,%rcx,4), %ebp
leal 0x1(%rcx), %edx
movl (%rax,%rdx,4), %ebx
cmpl $0x2, %r15d
jne 0x150975
cmpq %r12, 0x80(%rsi)
jbe 0x150975
incq %r12
cmpq %rdi, %r12
jne 0x150938
jmp 0x1508ed
leaq (%rax,%rcx,4), %r14
addq $0x8, %r14
movq %r14, %rdi
movl %r15d, %esi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
leaq 0x30(%rsp), %r8
callq 0x1504d6
movq 0x139644(%rip), %rcx    # 0x289fe0
movq (%rcx), %rdi
shrl %ebp
incl %ebp
shrl %ebx
incl %ebx
cmpl $-0x1, %eax
je 0x150a0f
cmpl $0x1, %eax
jne 0x150a23
leaq 0xdf5f3(%rip), %rsi     # 0x22ffab
movl %r15d, %edx
movl %ebp, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x22b0
testl %r15d, %r15d
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
je 0x150968
movl %r15d, %ebx
xorl %r15d, %r15d
movl (%r14,%r15,4), %eax
shrl %eax
testb $0x2, (%r13,%rax)
je 0x150a01
movl (%r14,%r15,4), %edi
shrl %edi
testb $0x4, (%r13,%rdi)
je 0x150a08
incq %r15
cmpq %r15, %rbx
jne 0x1509db
jmp 0x150a59
callq 0x1d4fa
jmp 0x1509e9
callq 0x1d516
jmp 0x1509f7
leaq 0xdf572(%rip), %rsi     # 0x22ff88
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x22b0
jmp 0x150a4b
cmpl $0x4, %r15d
leaq 0xdf551(%rip), %r8      # 0x22ff7f
leaq 0xdf54c(%rip), %rax     # 0x22ff81
cmoveq %rax, %r8
leaq 0xdf515(%rip), %rsi     # 0x22ff55
movl %ebp, %edx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
jmp 0x150968
xorl %r15d, %r15d
movl (%r14,%r15,4), %eax
shrl %eax
testb $0x2, (%r13,%rax)
je 0x150a82
movl (%r14,%r15,4), %edi
shrl %edi
testb $0x4, (%r13,%rdi)
jne 0x150a89
incq %r15
cmpq %r15, %rbx
jne 0x150a5c
jmp 0x150a90
callq 0x1d534
jmp 0x150a6a
callq 0x1d550
jmp 0x150a78
xorl %eax, %eax
movq (%rsp), %rsi
movq 0x8(%rsp), %rdi
movl (%r14,%rax,4), %ecx
shrl %ecx
movb $0x0, (%r13,%rcx)
incq %rax
cmpq %rax, %rbx
jne 0x150a9b
jmp 0x150968