// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, 0x28(%rsp)
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x18(%rax), %rdi
callq 0x694eb
movq (%r15), %rax
movq 0x10(%r15), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x66c25
movq %rbx, 0x8(%rsp)
movq (%rbx), %rax
movq (%rax), %rbx
shlq $0x3, %rbx
movq %rbx, %rdi
callq 0x2280
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x2120
movq %rax, %rbx
callq 0x68f5d
movq %rbx, (%rsp)
movq %rax, 0x18(%rbx)
xorl %ecx, %ecx
xorl %edx, %edx
movl %edx, %edx
movl %ecx, %eax
cmpq %rdx, (%r15)
jbe 0x7fdf7
testq %rdx, %rdx
je 0x7fde1
testl %ecx, %ecx
je 0x7fdf1
movq 0x10(%r15), %rsi
leal -0x1(%rcx), %r8d
movq (%rsi,%rdx,8), %rdi
cmpq %rdi, (%rsi,%r8,8)
jne 0x7fde9
jmp 0x7fdf3
movq 0x10(%r15), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, (%rsi,%rax,8)
incl %ecx
jmp 0x7fdf3
xorl %ecx, %ecx
incl %edx
jmp 0x7fdbb
movq %rax, (%r15)
pushq $0x1
popq %r10
xorl %r9d, %r9d
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %r14, 0x30(%rsp)
movq %r15, 0x58(%rsp)
movl %r10d, %edi
cmpq %rdi, %rax
jb 0x8013f
movq 0x10(%r15), %rdx
movl %ecx, %esi
je 0x7fe36
movq (%rdx,%rsi,8), %r8
xorq (%rdx,%rdi,8), %r8
shrq $0x20, %r8
je 0x80137
movl %r10d, %edi
subl %ecx, %edi
je 0x80137
leaq (%rdx,%rsi,8), %rcx
movq %rcx, 0x50(%rsp)
movq (%rcx), %rsi
shrq $0x20, %rsi
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rdx
movq %rsi, 0x10(%rsp)
imulq $0x28, %rsi, %rcx
cmpl $0x2, 0x10(%rdx,%rcx)
jb 0x80134
movl %edi, 0x40(%rsp)
movl %r10d, 0x44(%rsp)
addq %rcx, %rdx
movq %rdx, 0x48(%rsp)
movl %r13d, %r12d
movq %r9, %r15
leal (%r9,%r9), %edi
imulq $0x28, %r9, %rbp
leaq (%r9,%r9), %rax
movq %rax, 0x38(%rsp)
imulq $0x28, %r12, %rbx
addl %r13d, %r13d
xorl %r14d, %r14d
movq %r9, 0x60(%rsp)
movq %rdi, 0x20(%rsp)
cmpq 0x10(%rsp), %r12
movq (%rsp), %rcx
jae 0x7ff70
movq 0x10(%rcx), %rax
cmpq 0x8(%rcx), %r15
jne 0x7fef9
movq 0x38(%rsp), %rcx
addq %r14, %rcx
testq %r15, %r15
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq (%rsp), %rdx
movq %rcx, 0x8(%rdx)
imulq $0x28, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x20(%rsp), %rdi
movq (%rsp), %rcx
movq %rax, 0x10(%rcx)
incq %r15
movq %r15, (%rcx)
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movq 0x20(%rcx,%rbx), %rdx
movq %rdx, 0x20(%rax,%rbp)
movups (%rcx,%rbx), %xmm0
movups 0x10(%rcx,%rbx), %xmm1
movups %xmm1, 0x10(%rax,%rbp)
movups %xmm0, (%rax,%rbp)
andl $0x0, 0x10(%rcx,%rbx)
andl $0x0, 0xc(%rcx,%rbx)
andl $0x0, (%rcx,%rbx)
leal (%rdi,%r14), %eax
leal (%r14,%r13), %edx
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rcx,%rbx)
movq 0x18(%rsp), %rsi
movl %eax, (%rsi,%rdx,4)
leal (%rdi,%r14), %eax
incl %eax
leal (%r14,%r13), %ecx
incl %ecx
movl %eax, (%rsi,%rcx,4)
incq %r12
addq $0x2, %r14
addq $0x28, %rbp
addq $0x28, %rbx
jmp 0x7fead
movq 0x60(%rsp), %rax
leal (%r14,%rax,2), %eax
incl %eax
movq 0x10(%rsp), %rcx
leal (%rcx,%rcx), %edx
movl %edx, 0x38(%rsp)
leal 0x1(%rcx,%rcx), %edx
movq 0x18(%rsp), %rcx
movq %rdx, %r13
movl %eax, (%rcx,%rdx,4)
movl 0x40(%rsp), %eax
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %r12d, %r12d
movq 0x48(%rsp), %r14
movq 0x50(%rsp), %r15
cmpq 0x20(%rsp), %rbp
jae 0x800e4
movl (%r15,%rbp,8), %ebx
movl (%r14), %ecx
testl %ecx, %ecx
js 0x7ffcc
testl %ebx, %ebx
je 0x8009e
testl %ecx, %ecx
jns 0x80045
testl %ebx, %ebx
jne 0x80045
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rax
movq (%rax,%r13,8), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rdx, %rbx
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %r14
addq $0xc, %r14
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x8006b
movl (%r14,%r15), %eax
testl %eax, %eax
js 0x8003f
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%r14,%r15)
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
movl -0x8(%r14,%r15), %edx
movl -0x4(%r14,%r15), %esi
xorl $0x1, %esi
xorl $0x1, %edx
pushq $0x1
popq %rcx
callq 0x6db50
addq $0x18, %r15
jmp 0x80005
movl %ebx, %r9d
subl %r12d, %r9d
je 0x800bc
movq (%rsp), %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movl %r12d, %r8d
callq 0x7fc00
movl %ebx, %r12d
jmp 0x80096
movq 0x48(%rsp), %r14
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)
movq 0x50(%rsp), %r15
incq %rbp
jmp 0x7ffae
movq 0x1d2f3b(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x1686f4(%rip), %rdi    # 0x1e87a3
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x2410
jmp 0x80096
shrl $0x1f, %ecx
movq 0x1d2f1a(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1686dc(%rip), %rsi    # 0x1e87ac
movq 0x10(%rsp), %rdx
movl %r12d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x22b0
jmp 0x80096
movq 0x10(%rsp), %rax
leal 0x1(%rax), %r13d
movl 0x10(%r14), %r9d
subl %r12d, %r9d
movq (%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movl %r12d, %r8d
callq 0x7fc00
movq (%rbx), %r9
leal -0x2(,%r9,2), %eax
movl 0x38(%rsp), %ecx
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movq 0x58(%rsp), %r15
movq (%r15), %rax
movl 0x44(%rsp), %r10d
movl %r10d, %ecx
incl %r10d
jmp 0x7fe10
leal (%r9,%r9), %r8d
imulq $0x28, %r9, %r14
movq %r9, %rdi
leaq (%r9,%r9), %rax
movq %rax, 0x10(%rsp)
leal (,%r13,2), %r9d
xorl %r12d, %r12d
xorps %xmm2, %xmm2
movq %r8, 0x20(%rsp)
movq (%rsp), %rbp
movl %r13d, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rcx
cmpq %r15, (%rcx)
jbe 0x80239
movq 0x10(%rbp), %rax
movq %rdi, %rbx
cmpq 0x8(%rbp), %rdi
jne 0x801c7
movq 0x10(%rsp), %rcx
addq %r12, %rcx
testq %rbx, %rbx
pushq $0x2
popq %rdx
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbp)
imulq $0x28, %rcx, %rsi
movq %rax, %rdi
movq %r9, %r13
callq 0x2370
xorps %xmm2, %xmm2
movq %r13, %r9
movq 0x20(%rsp), %r8
movq %rax, 0x10(%rbp)
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
incq %rbx
movq %rbx, %rdi
movq %rbx, (%rbp)
movq 0x10(%rcx), %rcx
imulq $0x28, %r15, %rdx
movq 0x20(%rcx,%rdx), %rsi
movq %rsi, 0x20(%rax,%r14)
movups (%rcx,%rdx), %xmm0
movups 0x10(%rcx,%rdx), %xmm1
movups %xmm1, 0x10(%rax,%r14)
movups %xmm0, (%rax,%r14)
andl $0x0, 0x10(%rcx,%rdx)
andl $0x0, 0xc(%rcx,%rdx)
andl $0x0, (%rcx,%rdx)
leal (%r8,%r12), %eax
leal (%r9,%r12), %esi
movups %xmm2, 0x18(%rcx,%rdx)
movq 0x18(%rsp), %rdx
movl %eax, (%rdx,%rsi,4)
leal 0x1(%r8,%r12), %eax
leal (%r9,%r12), %ecx
incl %ecx
movl %eax, (%rdx,%rcx,4)
leal 0x1(%r15), %r13d
addq $0x2, %r12
addq $0x28, %r14
jmp 0x8016a
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x30(%rsp), %r15
movq %rdi, %r12
movq 0x18(%rcx), %rax
movl 0x4(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %r14
jae 0x802ba
movq 0x8(%rax), %rax
cmpl $0x0, 0xc(%rax,%rbx)
js 0x802b1
movq (%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x6c45d
movq 0x8(%rsp), %rsi
movq (%rsi), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movups (%rcx,%rbx), %xmm0
movups %xmm0, (%rax)
movq 0x10(%rcx,%rbx), %rcx
movq %rcx, 0x10(%rax)
movl 0x4(%rax), %ecx
movl 0x8(%rax), %edx
xorq $0x1, %rdx
movq 0x18(%rsp), %rdi
movl (%rdi,%rdx,4), %edx
xorl $0x1, %edx
movl %edx, 0x8(%rax)
movl (%rdi,%rcx,4), %ecx
movl %ecx, 0x4(%rax)
movq (%rsi), %rcx
incq %r14
addq $0x18, %rbx
jmp 0x80246
xorl %eax, %eax
movl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq (%rsp), %r14
movl %eax, %ebx
movq 0x18(%r14), %rdi
cmpq %rbx, %r12
jbe 0x802eb
movq 0x10(%r14), %rax
imulq $0x28, %rbx, %rcx
movl (%rax,%rcx), %edx
andl %ebp, %edx
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x69316
leal 0x1(%rbx), %eax
jmp 0x802c5
callq 0x694eb
movq 0x8(%rsp), %rbx
movq (%rbx), %rdi
callq 0x6921d
movq %r14, (%rbx)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
callq 0x80328
movq 0x18(%rsp), %rdi
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2220