// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r14
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
movq (%rdi), %rax
movq 0x18(%rax), %rdi
callq 0xa02fa
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x9d53f
movq (%r15), %rax
movq (%rax), %rbx
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x2280
movq %rax, (%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
movl $0x1, %edi
movl $0x38, %esi
callq 0x2120
movq %rax, %r13
movl $0x1, %edi
movl $0x48, %esi
callq 0x2120
movq %rax, 0x18(%r13)
xorl %ebx, %ebx
cmpq $0x0, (%r14)
movl $0x0, %eax
je 0xba1ce
xorl %edx, %edx
movl $0x1, %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
je 0xba1b1
testl %eax, %eax
je 0xba1c3
movq 0x10(%r14), %rsi
leal -0x1(%rax), %edi
movq (%rsi,%rdi,8), %rdi
cmpq (%rsi,%rdx,8), %rdi
je 0xba1c5
movq 0x10(%r14), %rsi
movq (%rsi,%rdx,8), %rdx
movl %eax, %edi
movq %rdx, (%rsi,%rdi,8)
incl %eax
jmp 0xba1c5
xorl %eax, %eax
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%r14)
ja 0xba197
movl %eax, %ecx
movq %rcx, (%r14)
testl %eax, %eax
je 0xba539
movl $0x1, %r12d
movl $0x1, %eax
xorl %ebx, %ebx
xorl %edi, %edi
movq %r14, 0x30(%rsp)
movl %edi, %esi
cmpq %rax, %rcx
je 0xba20c
movq 0x10(%r14), %rcx
movq (%rcx,%rsi,8), %rdx
xorq (%rcx,%rax,8), %rdx
shrq $0x20, %rdx
je 0xba3ad
cmpl %edi, %r12d
je 0xba232
shlq $0x3, %rsi
addq 0x10(%r14), %rsi
movq (%rsi), %rcx
shrq $0x20, %rcx
movq (%r15), %rax
movq 0x10(%rax), %rax
leaq (%rcx,%rcx,4), %rdx
cmpl $0x2, 0x10(%rax,%rdx,8)
jae 0xba23a
movl %r12d, %edi
jmp 0xba3ad
movq %rdx, 0x38(%rsp)
movq %rax, 0x10(%rsp)
movl %edi, 0x28(%rsp)
movq %rsi, 0x40(%rsp)
movq %rcx, 0x8(%rsp)
cmpl %ecx, %ebx
jae 0xba328
movl %ebx, %eax
addl %ebx, %ebx
leaq (,%rax,8), %rcx
leaq (%rcx,%rcx,4), %r14
movq 0x8(%rsp), %rbp
subq %rax, %rbp
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0xba2a5
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
leaq (%rax,%rax,4), %rax
movq (%r15), %rdx
movq 0x10(%rdx), %rdx
movq 0x20(%rdx,%r14), %rsi
movq %rsi, 0x20(%rcx,%rax,8)
movups (%rdx,%r14), %xmm0
movups 0x10(%rdx,%r14), %xmm1
movups %xmm1, 0x10(%rcx,%rax,8)
movups %xmm0, (%rcx,%rax,8)
movl $0x0, %eax
movl %eax, 0x10(%rdx,%r14)
movl %eax, 0xc(%rdx,%r14)
movl %eax, (%rdx,%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdx,%r14)
movl (%r13), %eax
leal -0x2(%rax,%rax), %ecx
movl %ebx, %edx
movq (%rsp), %rsi
movl %ecx, (%rsi,%rdx,4)
leal -0x1(,%rax,2), %eax
leal 0x1(%rbx), %ecx
movl %eax, (%rsi,%rcx,4)
addl $0x2, %ebx
addq $0x28, %r14
decq %rbp
jne 0xba272
movq 0x10(%rsp), %rax
movq 0x38(%rsp), %rcx
leaq (%rax,%rcx,8), %r14
movl (%r13), %eax
leal 0x1(,%rax,2), %eax
movq 0x8(%rsp), %rcx
leal 0x1(%rcx,%rcx), %edx
movq (%rsp), %rcx
movq %rdx, 0x10(%rsp)
movl %eax, (%rcx,%rdx,4)
xorl %r10d, %r10d
movl %r12d, %eax
subl 0x28(%rsp), %eax
movq %rax, 0x28(%rsp)
jne 0xba3c4
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ebx
leal (%rax,%rax), %ebp
movl 0x10(%r14), %r9d
subl %r10d, %r9d
movq %r13, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %r10d, %r8d
callq 0xb9fb8
movl (%r13), %eax
leal -0x2(,%rax,2), %eax
movl %ebp, %ecx
movq (%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movl %r12d, %edi
movq 0x30(%rsp), %r14
incl %r12d
movq (%r14), %rcx
movq %r12, %rax
cmpq %r12, %rcx
jae 0xba1ef
jmp 0xba539
xorl %ebp, %ebp
xorl %r10d, %r10d
movq 0x40(%rsp), %rax
movl (%rax,%rbp,8), %ebx
movl (%r14), %ecx
testl %ecx, %ecx
js 0xba3e0
testl %ebx, %ebx
je 0xba504
testl %ecx, %ecx
jns 0xba4ce
testl %ebx, %ebx
jne 0xba4ce
movl %r10d, %ebx
movq (%r15), %rax
movq 0x18(%rax), %rdx
movq 0x28(%rdx), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rcx
testl %ecx, %ecx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
je 0xba4ab
movq %rcx, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
shlq $0x3, %rax
addq 0x8(%rdx), %rax
movl %ecx, %ecx
xorl %edx, %edx
leaq (%rdx,%rdx,2), %rsi
movl 0xc(%rax,%rsi,8), %edi
testl %edi, %edi
js 0xba4a3
leaq (%rax,%rsi,8), %rsi
orl $0x80000000, %edi       # imm = 0x80000000
movl %edi, 0xc(%rsi)
movq (%r15), %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 %r11, %r9
je 0xba4a3
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 0xba49a
orb $-0x80, 0x3(%r8,%r9)
addq $0x18, %r9
cmpq %r9, %rdi
jne 0xba486
incq %rdx
cmpq %rcx, %rdx
jne 0xba42c
andb $0x7f, 0x3(%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 0xba4ee
movl %ebx, %r9d
subl %r10d, %r9d
je 0xba50e
movq %r13, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl %r10d, %r8d
callq 0xb9fb8
movl %ebx, %r10d
incq %rbp
cmpq 0x28(%rsp), %rbp
jne 0xba3c9
jmp 0xba367
movl %r10d, %ebx
callq 0x105d2
jmp 0xba4ee
shrl $0x1f, %ecx
movq 0x1cfac8(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x17019a(%rip), %rsi    # 0x22a6bc
movq 0x8(%rsp), %rdx
movl %ebx, %r8d
movl %r10d, %r9d
xorl %eax, %eax
movl %r10d, %ebx
callq 0x22b0
jmp 0xba4ee
movl %ebx, %r14d
movq (%r15), %rax
cmpq %r14, (%rax)
jbe 0xba609
leal (%r14,%r14), %ebx
leal 0x1(%r14), %ebp
xorl %r12d, %r12d
xorps %xmm2, %xmm2
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0xba58c
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rsi
callq 0x2370
xorps %xmm2, %xmm2
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
leaq (%rax,%rax,4), %rax
movq (%r15), %rdx
movq 0x10(%rdx), %rdx
leaq (%r14,%r14,4), %rsi
movq 0x20(%rdx,%rsi,8), %rdi
movq %rdi, 0x20(%rcx,%rax,8)
movups (%rdx,%rsi,8), %xmm0
movups 0x10(%rdx,%rsi,8), %xmm1
movups %xmm1, 0x10(%rcx,%rax,8)
movups %xmm0, (%rcx,%rax,8)
movl %r12d, 0x10(%rdx,%rsi,8)
movl %r12d, 0xc(%rdx,%rsi,8)
movl %r12d, (%rdx,%rsi,8)
movups %xmm2, 0x18(%rdx,%rsi,8)
movl (%r13), %eax
leal -0x2(%rax,%rax), %ecx
movl %ebx, %edx
movq (%rsp), %rsi
movl %ecx, (%rsi,%rdx,4)
leal -0x1(%rax,%rax), %eax
leal 0x1(%rbx), %ecx
movl %eax, (%rsi,%rcx,4)
movl %ebp, %r14d
movq (%r15), %rax
addl $0x2, %ebx
incl %ebp
cmpq %r14, (%rax)
ja 0xba556
movq (%r15), %rax
movq 0x18(%rax), %rax
testl $0x7fffffff, 0x4(%rax)  # imm = 0x7FFFFFFF
je 0xba6f3
xorl %ebx, %ebx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
movq 0x8(%rax), %rax
cmpl $0x0, 0xc(%rax,%rbx)
js 0xba6d3
movq 0x18(%r13), %r14
movl 0x4(%r14), %ecx
andl %ebp, %ecx
movl (%r14), %eax
cmpl %eax, %ecx
jne 0xba66c
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, (%r14)
movq 0x8(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x8(%r14)
movq 0x8(%r14), %rax
movl 0x4(%r14), %ecx
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leal 0x1(%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl $0x80000000, %ecx       # imm = 0x80000000
orl %esi, %ecx
movl %ecx, 0x4(%r14)
leaq (%rdx,%rdx,2), %rcx
movq (%r15), %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
movq (%rsp), %rdi
movl (%rdi,%rsi,4), %esi
xorl $0x1, %esi
movl %esi, 0x8(%rax,%rcx,8)
movl (%rdi,%rdx,4), %edx
movl %edx, 0x4(%rax,%rcx,8)
incq %r12
movq (%r15), %rax
movq 0x18(%rax), %rax
movl 0x4(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
addq $0x18, %rbx
cmpq %rcx, %r12
jb 0xba627
cmpq $0x0, (%r13)
je 0xba723
xorl %ebx, %ebx
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x10(%r13), %rax
movq 0x18(%r13), %rdi
leaq (%rbx,%rbx,4), %rcx
movl (%rax,%rcx,8), %edx
andl %ebp, %edx
movl %ebx, %esi
xorl %ecx, %ecx
callq 0xa0115
incl %ebx
cmpq %rbx, (%r13)
ja 0xba701
movq 0x18(%r13), %rdi
callq 0xa02fa
movq (%r15), %rdi
callq 0xa0004
movq %r13, (%r15)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0xba760
movq (%rsp), %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220