// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: break_all_contigs(ma_ug_t**, asg_t*, kvec_asg_arc_t_warp*, kvec_t_u64_warp*)
void break_all_contigs(ma_ug_t **ug, asg_t *read_g, kvec_asg_arc_t_warp* edge, kvec_t_u64_warp* break_points)
{
    asg_cleanup((*ug)->g);
    uint32_t k, l, m, occ, uID, idx;
    uint64_t *a = NULL, w;
    ma_utg_t *u = NULL;
    radix_sort_arch64(break_points->a.a, break_points->a.a + break_points->a.n);
    uint32_t *utg_idx = NULL; MALLOC(utg_idx, (*ug)->u.n<<1);
    memset(utg_idx, -1, sizeof(uint32_t)*((*ug)->u.n<<1));
    asg_arc_t *av = NULL;
    uint32_t p_u_idx, nv, v, s_i, p_i;
    ma_ug_t *n_ug = NULL;
    ma_utg_t *p = NULL, *z = NULL;
    n_ug = (ma_ug_t*)calloc(1, sizeof(ma_ug_t));
    n_ug->g = asg_init();

    for (k = m = 0; k < break_points->a.n; k++)
    {
        if(k == 0 || (m > 0 && break_points->a.a[m-1] != break_points->a.a[k]))
        {
            break_points->a.a[m] = break_points->a.a[k];
            m++;
        }
    }
    ///fprintf(stderr, "break_points->a.n: %u, m: %u\n", (uint32_t)break_points->a.n, m);
    break_points->a.n = m;

    for (k = 1, l = 0, p_i = 0; k <= break_points->a.n; ++k) 
    {
        if (k == break_points->a.n || (break_points->a.a[k]>>32) != (break_points->a.a[l]>>32)) 
        {
            occ = k - l;
            a = break_points->a.a + l;
            l = k;
            if(occ == 0) continue;
            uID = a[0]>>32;
            u = &((*ug)->u.a[uID]);
            if(u->n < 2) continue;

            for (s_i = p_i; s_i < uID; s_i++)
            {
                kv_pushp(ma_utg_t, n_ug->u, &p);
                z = &((*ug)->u.a[s_i]);
                (*p) = (*z);
                z->len = z->circ = /**z->start = z->end =**/ z->m = z->n = 0;
                z->a = NULL; z->s = NULL;

                utg_idx[s_i<<1] = ((uint32_t)(n_ug->u.n-1))<<1;
                utg_idx[(s_i<<1)+1] = (((uint32_t)(n_ug->u.n-1))<<1)+1;
            }
            p_i = uID + 1;


            utg_idx[(uID<<1)+1] = (((uint32_t)(n_ug->u.n))<<1)+1;
            for (m = p_u_idx = 0; m < occ; m++)
            {
                idx = (uint32_t)(a[m]);
                if(!u->circ && idx == 0)
                {
                    fprintf(stderr, "ERROR 1\n");
                    continue;
                } 
                

                if(u->circ && idx == 0) 
                {
                    av = asg_arc_a((*ug)->g, (uID<<1)+1);
                    nv = asg_arc_n((*ug)->g, (uID<<1)+1);
                    for (v = 0; v < nv; v++)
                    {
                        if(av[v].del) continue;
                        av[v].del = 1;
                        asg_arc_del((*ug)->g, (av[v].v)^1, (av[v].ul>>32)^1, 1);
                    }
                    u->circ = 0;
                    u->start = u->a[0]>>32;
                    u->end = (u->a[u->n-1]>>32)^1;
                    continue; 
                }

                if(idx - p_u_idx <= 0)
                {
                    fprintf(stderr, "ERROR2: uID: %u, u->circ: %u, idx: %u, p_u_idx: %u\n", 
                                                                        uID, u->circ, idx, p_u_idx);
                    continue;
                } 
                
                
                push_sub_unitig(n_ug, u, read_g, edge, p_u_idx, idx - p_u_idx);
                p_u_idx = idx;
            }
            push_sub_unitig(n_ug, u, read_g, edge, p_u_idx, u->n - p_u_idx);
            utg_idx[(uID<<1)] = ((uint32_t)(n_ug->u.n-1))<<1;

        }
    }

    for (s_i = p_i; s_i < (*ug)->u.n; s_i++)
    {
        kv_pushp(ma_utg_t, n_ug->u, &p);
        z = &((*ug)->u.a[s_i]);
        (*p) = (*z);
        z->len = z->circ = /**z->start = z->end =**/ z->m = z->n = 0;
        z->a = NULL; z->s = NULL;

        utg_idx[s_i<<1] = ((uint32_t)(n_ug->u.n-1))<<1;
        utg_idx[(s_i<<1)+1] = (((uint32_t)(n_ug->u.n-1))<<1)+1;
    }

    // for (k = 0; k < (*ug)->u.n; k++)
    // {
    //     if(utg_idx[(k<<1)] == (uint32_t)-1) fprintf(stderr, "ERROR 3\n");
    //     if(utg_idx[(k<<1)+1] == (uint32_t)-1) fprintf(stderr, "ERROR 4\n");
    // }
    
    asg_arc_t *q = NULL;
    for (k = 0; k < (*ug)->g->n_arc; k++)
    {
        if((*ug)->g->arc[k].del) continue;
        q = asg_arc_pushp(n_ug->g);
        (*q) = (*ug)->g->arc[k];

        q->v = utg_idx[q->v^1]^1;

        w = q->ul>>32; w = utg_idx[w]; w <<= 32;
        q->ul <<= 32; q->ul >>= 32; q->ul |= w;
    }
    
    for (k = 0; k < n_ug->u.n; k++)
    {
        asg_seq_set(n_ug->g, k, n_ug->u.a[k].len, 0);
    }
    
    asg_cleanup(n_ug->g);
    
    // fprintf(stderr, "n_ug->u.n: %u, n_ug->g->n_seq: %u, (*ug)->u.n: %u\n", (uint32_t)n_ug->u.n, (uint32_t)n_ug->g->n_seq,
    // (uint32_t)(*ug)->u.n);

    // for (k = 0; k < ((*ug)->u.n<<1); k++)
    // {
    //     uint32_t ug_rid, n_ug_rid;
    //     if(k&1) ug_rid = ((*ug)->u.a[k>>1]).start;
    //     else ug_rid = ((*ug)->u.a[k>>1]).end;

    //     if(utg_idx[k]&1) n_ug_rid = n_ug->u.a[utg_idx[k]>>1].start;
    //     else n_ug_rid = n_ug->u.a[utg_idx[k]>>1].end;
        
    //     if(ug_rid != n_ug_rid)
    //     {
    //         fprintf(stderr, "ERROR, uid: %u, dir: %u, circle: %u, ug_rid: %u, n_ug_rid: %u\n", 
    //                     k>>1, k&1, (*ug)->u.a[k>>1].circ, ug_rid, n_ug_rid);
    //     }
    // }



    ma_ug_destroy((*ug));
    (*ug) = n_ug;
    renew_utg(ug, read_g, edge); ///for circle
    // fprintf(stderr, "***********(1)edge->a.n: %u***********\n", (uint32_t)edge->a.n);
    // debug_utg_graph(*ug, read_g, edge, 0, 0);
    // fprintf(stderr, "***********(1)edge->a.n: %u***********\n", (uint32_t)edge->a.n);
    free(utg_idx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x18(%rax), %rdi
callq 0xa0a7d
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x9dc69
movq %rbx, (%rsp)
movq (%rbx), %rax
movq %rax, %r13
movq (%rax), %rbx
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x2280
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %rbx
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rbx, %r9
movq %rax, 0x18(%rbx)
movq (%r14), %rdx
testq %rdx, %rdx
movq %r15, 0x30(%rsp)
movq %rbx, 0x8(%rsp)
je 0xbb519
movq %r15, %r12
xorl %esi, %esi
movl $0x1, %eax
xorl %ecx, %ecx
movq (%rsp), %r11
cmpl $0x1, %eax
jne 0xbb120
movq 0x10(%r14), %rdi
movq (%rdi,%rsi,8), %rsi
jmp 0xbb136
testl %ecx, %ecx
je 0xbb143
movq 0x10(%r14), %rdi
leal -0x1(%rcx), %r8d
movq (%rdi,%rsi,8), %rsi
cmpq %rsi, (%rdi,%r8,8)
je 0xbb145
movl %ecx, %edx
movq %rsi, (%rdi,%rdx,8)
incl %ecx
movq (%r14), %rdx
jmp 0xbb145
xorl %ecx, %ecx
movl %eax, %esi
incl %eax
cmpq %rsi, %rdx
ja 0xbb111
movl %ecx, %eax
movq %rax, (%r14)
testl %ecx, %ecx
je 0xbb523
movl $0x1, %r10d
movl $0x1, %edx
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq %r14, 0x50(%rsp)
movq 0x10(%r14), %rsi
movl %ecx, %edi
cmpq %rdx, %rax
je 0xbb195
movq (%rsi,%rdi,8), %r8
xorq (%rsi,%rdx,8), %r8
shrq $0x20, %r8
je 0xbb500
movl %r10d, %edx
subl %ecx, %edx
movq %rdx, 0x70(%rsp)
je 0xbb4fd
leaq (%rsi,%rdi,8), %rcx
movq %rcx, 0x68(%rsp)
movq (%rcx), %r8
shrq $0x20, %r8
movq (%r11), %rcx
movq 0x10(%rcx), %rcx
leaq (%r8,%r8,4), %rdx
cmpl $0x2, 0x10(%rcx,%rdx,8)
jb 0xbb4fd
movq 0x18(%rsp), %rdi
cmpl %r8d, %edi
movq %r8, 0x38(%rsp)
movq %r10, 0x58(%rsp)
jae 0xbb316
movq %rdx, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movl %edi, %edx
movq 0x8(%r9), %rcx
movq 0x10(%r9), %rax
leal (,%rbp,2), %r15d
leaq (,%rbp,8), %rsi
leaq (%rsi,%rsi,4), %r14
addl %edi, %edi
leaq (,%rdx,8), %rsi
leaq (%rsi,%rsi,4), %rbx
movq %r8, %r12
subq %rdx, %r12
xorl %r13d, %r13d
movq %rbp, 0x20(%rsp)
movq 0x10(%rsp), %r8
movq %rdi, %r10
movq %rdi, 0x18(%rsp)
movq %r15, 0x60(%rsp)
cmpq %rcx, %rbp
jne 0xbb285
leaq (%rcx,%rcx), %r15
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %r15
movq %r15, 0x8(%r9)
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r11
movq %rax, 0x10(%r9)
movq %r15, %rcx
movq 0x60(%rsp), %r15
movq 0x18(%rsp), %r10
movq 0x10(%rsp), %r8
incq %rbp
movq %rbp, (%r9)
movq (%r11), %rdx
movq 0x10(%rdx), %rdx
movq 0x20(%rdx,%rbx), %rsi
movq %rsi, 0x20(%rax,%r14)
movups (%rdx,%rbx), %xmm0
movups 0x10(%rdx,%rbx), %xmm1
movups %xmm1, 0x10(%rax,%r14)
movups %xmm0, (%rax,%r14)
xorl %esi, %esi
movl %esi, 0x10(%rdx,%rbx)
movl %esi, 0xc(%rdx,%rbx)
movl %esi, (%rdx,%rbx)
leal (%r15,%r13), %esi
leal (%r10,%r13), %edi
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdx,%rbx)
movl %esi, (%r8,%rdi,4)
leal (%r15,%r13), %edx
incl %edx
leal (%r10,%r13), %esi
incl %esi
movl %edx, (%r8,%rsi,4)
addl $0x2, %r13d
addq $0x28, %r14
addq $0x28, %rbx
decq %r12
jne 0xbb239
movq 0x20(%rsp), %rbp
leal (%r13,%rbp,2), %ebp
movq 0x30(%rsp), %r12
movq 0x38(%rsp), %r8
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %rdx
jmp 0xbb318
addl %ebp, %ebp
leaq (%rcx,%rdx,8), %r14
leal 0x1(%r8), %eax
movq %rax, 0x18(%rsp)
orl $0x1, %ebp
leal (%r8,%r8), %eax
movl %eax, 0x20(%rsp)
leal 0x1(,%r8,2), %ecx
movq 0x10(%rsp), %rax
movq %rcx, %r15
movl %ebp, (%rax,%rcx,4)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x68(%rsp), %rax
movl (%rax,%r13,8), %ebx
movl (%r14), %ecx
testl %ecx, %ecx
js 0xbb360
testl %ebx, %ebx
je 0xbb482
testl %ecx, %ecx
jns 0xbb44c
testl %ebx, %ebx
jne 0xbb44c
movq (%r11), %rax
movq 0x18(%rax), %rsi
movq 0x28(%rsi), %rax
movq (%rax,%r15,8), %rdx
testl %edx, %edx
je 0xbb425
movq %rdx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
shlq $0x3, %rax
addq 0x8(%rsi), %rax
movl %edx, %ecx
xorl %edx, %edx
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
leaq (%rdx,%rdx,2), %rsi
movl 0xc(%rax,%rsi,8), %edi
testl %edi, %edi
js 0xbb41a
leaq (%rax,%rsi,8), %rsi
orl $0x80000000, %edi       # imm = 0x80000000
movl %edi, 0xc(%rsi)
movq (%r11), %rdi
movq 0x18(%rdi), %rdi
movl 0x8(%rsi), %r8d
xorq $0x1, %r8
movq 0x28(%rdi), %r9
movq (%r9,%r8,8), %r8
movq %r8, %r9
andq %rbx, %r9
je 0xbb41a
movl 0x4(%rsi), %esi
movq 0x8(%rdi), %r10
shrq $0x20, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %rdi
leaq (%r8,%r8,2), %r8
leaq (%r10,%r8,8), %r8
addq $0xc, %r8
xorl %r9d, %r9d
movl -0x4(%r8,%r9), %r10d
xorl %esi, %r10d
cmpl $0x1, %r10d
jne 0xbb411
orb $-0x80, 0x3(%r8,%r9)
addq $0x18, %r9
cmpq %r9, %rdi
jne 0xbb3fd
incq %rdx
cmpq %rcx, %rdx
jne 0xbb3a3
movl (%r14), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, (%r14)
movq 0x18(%r14), %rax
movl 0x4(%rax), %ecx
movl %ecx, 0x4(%r14)
movl 0x10(%r14), %ecx
decl %ecx
movl 0x4(%rax,%rcx,8), %eax
xorl $0x1, %eax
movl %eax, 0x8(%r14)
jmp 0xbb472
movl %ebx, %r9d
subl %ebp, %r9d
je 0xbb489
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0xbaf12
movl %ebx, %ebp
movq (%rsp), %r11
incq %r13
cmpq 0x70(%rsp), %r13
jne 0xbb348
jmp 0xbb4b1
callq 0x10385
jmp 0xbb46e
shrl $0x1f, %ecx
movq 0x1d9b4d(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x17b40f(%rip), %rsi    # 0x2368ac
movq 0x38(%rsp), %rdx
movl %ebp, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x22b0
jmp 0xbb46e
movl 0x10(%r14), %r9d
subl %ebp, %r9d
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq %r12, %rcx
movl %ebp, %r8d
callq 0xbaf12
movq %rbx, %r9
movq (%rsp), %r11
movq (%rbx), %rbp
leal -0x2(,%rbp,2), %eax
movl 0x20(%rsp), %ecx
movq 0x10(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movq 0x50(%rsp), %r14
movq (%r14), %rax
movq 0x58(%rsp), %r10
movl %r10d, %ecx
incl %r10d
movq %r10, %rdx
cmpq %r10, %rax
jae 0xbb178
movq (%r11), %rsi
movq 0x18(%rsp), %rax
jmp 0xbb52a
xorl %ebp, %ebp
xorl %eax, %eax
movq (%rsp), %r11
jmp 0xbb527
xorl %ebp, %ebp
xorl %eax, %eax
movq %r13, %rsi
movl %eax, %r14d
cmpq %r14, (%rsi)
movq 0x10(%rsp), %r15
jbe 0xbb62b
movq 0x8(%r9), %rcx
movq 0x10(%r9), %rax
leal (,%rbp,2), %ebx
leaq (,%rbp,8), %rdx
leaq (%rdx,%rdx,4), %r12
movq %rbp, %r8
leal (%r14,%r14), %r13d
leal 0x1(%r14), %ebp
xorl %r10d, %r10d
xorps %xmm2, %xmm2
movq %r8, 0x20(%rsp)
cmpq %rcx, %r8
jne 0xbb5b7
leaq (%rcx,%rcx), %r15
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %r15
movq %r15, 0x8(%r9)
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,4), %rsi
movq %rax, %rdi
callq 0x2370
xorps %xmm2, %xmm2
xorl %r10d, %r10d
movq 0x8(%rsp), %r9
movq (%rsp), %r11
movq %rax, 0x10(%r9)
movq (%r11), %rsi
movq %r15, %rcx
movq 0x10(%rsp), %r15
movq 0x20(%rsp), %r8
incq %r8
movq %r8, (%r9)
movq 0x10(%rsi), %rdx
leaq (%r14,%r14,4), %rsi
movq 0x20(%rdx,%rsi,8), %rdi
movq %rdi, 0x20(%rax,%r12)
movups (%rdx,%rsi,8), %xmm0
movups 0x10(%rdx,%rsi,8), %xmm1
movups %xmm1, 0x10(%rax,%r12)
movups %xmm0, (%rax,%r12)
movl %r10d, 0x10(%rdx,%rsi,8)
movl %r10d, 0xc(%rdx,%rsi,8)
movl %r10d, (%rdx,%rsi,8)
movl %r13d, %edi
movups %xmm2, 0x18(%rdx,%rsi,8)
movl %ebx, (%r15,%rdi,4)
leal 0x1(%rbx), %edx
leal 0x1(%r13), %esi
movl %edx, (%r15,%rsi,4)
movl %ebp, %r14d
movq (%r11), %rsi
addl $0x2, %ebx
addq $0x28, %r12
addl $0x2, %r13d
incl %ebp
cmpq %r14, (%rsi)
ja 0xbb567
jmp 0xbb62e
movq %rbp, %r8
movq %r8, %rbp
movq 0x18(%rsi), %rax
testl $0x7fffffff, 0x4(%rax)  # imm = 0x7FFFFFFF
je 0xbb724
xorl %ebx, %ebx
movl $0x10, %r12d
xorl %r13d, %r13d
movq 0x8(%rax), %rax
cmpl $0x0, 0xc(%rax,%rbx)
js 0xbb707
movq 0x18(%r9), %r14
movl 0x4(%r14), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl (%r14), %ecx
jne 0xbb6a9
leal (%rcx,%rcx), %eax
testl %ecx, %ecx
cmovel %r12d, %eax
movl %eax, (%r14)
movq 0x8(%r14), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,2), %rsi
callq 0x2370
movq 0x8(%rsp), %r9
movq (%rsp), %r11
movq %rax, 0x8(%r14)
movl 0x4(%r14), %edx
movl %edx, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0xbb6ad
movq 0x8(%r14), %rax
leal 0x1(%rdx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %edx       # imm = 0x80000000
orl %esi, %edx
movl %edx, 0x4(%r14)
movl %ecx, %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%r11), %rdx
movq 0x18(%rdx), %rdx
movq 0x8(%rdx), %rdx
movups (%rdx,%rbx), %xmm0
movups %xmm0, (%rax,%rcx,8)
movq 0x10(%rdx,%rbx), %rdx
movq %rdx, 0x10(%rax,%rcx,8)
movl 0x4(%rax,%rcx,8), %edx
movl 0x8(%rax,%rcx,8), %esi
xorq $0x1, %rsi
movl (%r15,%rsi,4), %esi
xorl $0x1, %esi
movl %esi, 0x8(%rax,%rcx,8)
movl (%r15,%rdx,4), %edx
movl %edx, 0x4(%rax,%rcx,8)
movq (%r11), %rsi
incq %r13
movq 0x18(%rsi), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq $0x18, %rbx
cmpq %rcx, %r13
jb 0xbb64d
movq 0x18(%r9), %rbx
testq %rbp, %rbp
je 0xbb760
movq %rbp, %r13
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r12
xorl %r14d, %r14d
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r14,%r14,4), %rax
movl (%r12,%rax,8), %edx
andl %ebp, %edx
movq %rbx, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0xa0887
incl %r14d
cmpq %r14, %r13
ja 0xbb741
movq %rbx, %rdi
callq 0xa0a7d
movq (%rsp), %rbx
movq (%rbx), %rdi
callq 0xa0761
movq 0x8(%rsp), %rax
movq %rax, (%rbx)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0xbb7a4
movq %r15, %rdi
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220