// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: purge_merge(asg_t*, ma_ug_t*, hap_overlaps_list*, buf_t*, R_to_U*, ma_hit_t_alloc*, ma_sub_t*, asg_t*, unsigned long*, kvec_asg_arc_t_offset*, kvec_t_i32_warp*, kvec_t_i32_warp*, int, int, kvec_asg_arc_t_warp*, unsigned char*, hap_cov_t*)
void purge_merge(asg_t *purge_g, ma_ug_t *ug, hap_overlaps_list* all_ovlp, buf_t* b_0,
R_to_U* ruIndex, ma_hit_t_alloc* reverse_sources, ma_sub_t *coverage_cut, asg_t *read_g, 
uint64_t* position_index, kvec_asg_arc_t_offset* u_buffer, kvec_t_i32_warp* tailIndex, 
kvec_t_i32_warp* prevIndex, int max_hang, int min_ovlp, kvec_asg_arc_t_warp* edge, uint8_t* visit,
hap_cov_t *cov)
{
    uint32_t i, nv, k, v, w, x_beg_index, x_end_index, y_beg_index, y_end_index, cut_beg, cut_end, begIndex, endIndex, keepUid;
    hap_overlaps *x = NULL/**, *y = NULL**/;
    ma_utg_t *xReads = NULL, *yReads = NULL;
    asg_arc_t t_forward, t_backward;
    asg_arc_t *av = NULL;
    kvec_t(uint64_t) buffer;
    uint64_t totalLen;
    int index = 0;
    i = 0;
    while (i < b_0->b.n)
    {
        cut_beg = 0; cut_end = (uint32_t)-1;
        kv_init(buffer);
        /********************for the first node********************/
        v = b_0->b.a[i];
        keepUid = v>>1;
        xReads = &(ug->u.a[v>>1]);
        if(v&1)
        {
            for (k = 0; k < xReads->n; k++)
            {
                kv_push(uint64_t, buffer, (xReads->a[xReads->n - k - 1])^(uint64_t)(0x100000000));
            }
        }
        else
        {
            for (k = 0; k < xReads->n; k++)
            {
                kv_push(uint64_t, buffer, xReads->a[k]);
            }
        }
        cut_beg = 0; cut_end = xReads->n - 1;
        i++;
        /********************for the first node********************/

        
        for (; i < b_0->b.n; i++)
        {
            ///x = y = NULL;
            x = NULL;

            v = b_0->b.a[i-1];
            w = b_0->b.a[i];
            

            index = get_specific_hap_overlap(&(all_ovlp->x[v>>1]), v>>1, w>>1);
            x = &(all_ovlp->x[v>>1].a.a[index]);


        
            xReads = &(ug->u.a[v>>1]);
            yReads = &(ug->u.a[w>>1]);

            begIndex = x->x_beg_id;
            if(cut_beg > begIndex) begIndex = cut_beg;

            endIndex = x->x_end_id-1;
            if(cut_end < endIndex) endIndex = cut_end;

            get_node_boundary_advance(ruIndex, reverse_sources, coverage_cut, read_g, position_index, max_hang, 
            min_ovlp, xReads, yReads, v>>1, w>>1, begIndex, endIndex, x->y_beg_id, x->y_end_id-1, v&1, 
            x->rev, u_buffer, tailIndex, prevIndex, &t_forward, &t_backward);

            if(t_forward.del || t_backward.del) break;
            
            kv_push(asg_arc_t, edge->a, t_forward);
            kv_push(asg_arc_t, edge->a, t_backward);

            x_beg_index = 0; x_end_index = xReads->n - 1;
            y_beg_index = 0; y_end_index = yReads->n - 1;
            
            if((v&1) == 0)
            {
                x_end_index = (uint32_t)position_index[t_forward.ul>>33];
                buffer.n = buffer.n - (cut_end - x_end_index);
            }
            else
            {
                x_beg_index = (uint32_t)position_index[t_forward.ul>>33];
                buffer.n = buffer.n - (x_beg_index - cut_beg);
            }

            if((w&1) == 1)
            {
                y_end_index = (uint32_t)position_index[t_forward.v>>1];
            }
            else
            {
                y_beg_index = (uint32_t)position_index[t_forward.v>>1];
            }

            cut_beg = y_beg_index;
            cut_end = y_end_index;

            if((w&1) == 1)
            {
                for (k = y_end_index; k >= y_beg_index; k--)
                {
                    kv_push(uint64_t, buffer, (yReads->a[k])^(uint64_t)(0x100000000));
                    if(k==0) break;
                }
            }
            else
            {
                for (k = y_beg_index; k <= y_end_index; k++)
                {
                    kv_push(uint64_t, buffer, yReads->a[k]);
                }
            }

            purge_g->seq[w>>1].c = ALTER_LABLE;
            if(cov) collect_trans_purge_joint_cov(cov, ug, x);

            // if(buffer.n > 1)
            // {
            //     for (k = 0; k < buffer.n - 1; k++)
            //     {
            //         if((buffer.a[k]>>32) == 854769 && (buffer.a[k+1]>>32) == 64486)
            //         {
            //             fprintf(stderr, "+++++++v: %u, w: %u, xReads->n: %u, yReads->n: %u\n", 
            //                         v, w, xReads->n, yReads->n);
            //             fprintf(stderr, "x->rev: %u, x->x_beg_id: %u, x->x_end_id: %u, x->y_beg_id: %u, x->y_end_id: %u\n", 
            //                         x->rev, x->x_beg_id, x->x_end_id, x->y_beg_id, x->y_end_id);
            //             fprintf(stderr, "t_forward.ul>>32: %u, t_forward.v: %u, y_beg_index: %u, y_end_index: %u\n", 
            //                         t_forward.ul>>32, t_forward.v, y_beg_index, y_end_index);
            //             fprintf(stderr, "type: %u, x->x_beg_pos: %u, x->x_end_pos: %u, xReads->len: %u\n", 
            //                         x->type, x->x_beg_pos, x->x_end_pos, xReads->len);
            //             fprintf(stderr, "x->y_beg_pos: %u, x->y_end_pos: %u, yReads->len: %u\n", 
            //                         x->y_beg_pos, x->y_end_pos, yReads->len);
            //         }
            //     }
            // }
        }

        // fprintf(stderr, "+keepUid: %u, i: %u, b_0->b.n: %u, buffer.n: %u\n", 
        //                             keepUid, i, (uint32_t)b_0->b.n, (uint32_t)buffer.n);
        fill_unitig(buffer.a, buffer.n, read_g, edge, 0, &totalLen);
        ///fprintf(stderr, "-keepUid: %u\n", keepUid);

        xReads = &(ug->u.a[keepUid]);
        free(xReads->a);
        xReads->a = buffer.a;
        xReads->n = buffer.n;
        xReads->m = buffer.m;
        xReads->len = totalLen;
        xReads->circ = 0;
        if(xReads->start != (xReads->a[0]>>32))
        {
            xReads->start = xReads->a[0]>>32;
            v = (keepUid<<1)+1;
            av = asg_arc_a(ug->g, v);
            nv = asg_arc_n(ug->g, v);
            for (k = 0; k < nv; k++)
            {
                if(av[k].del) continue;
                asg_arc_del(ug->g, av[k].ul>>32, av[k].v, 1);
                asg_arc_del(ug->g, av[k].v^1, av[k].ul>>32^1, 1);
            }
        }
        
        if(xReads->end != ((xReads->a[xReads->n-1]>>32)^1))
        {
            xReads->end = ((xReads->a[xReads->n-1]>>32)^1);
            v = (keepUid<<1);
            av = asg_arc_a(ug->g, v);
            nv = asg_arc_n(ug->g, v);
            for (k = 0; k < nv; k++)
            {
                if(av[k].del) continue;
                asg_arc_del(ug->g, av[k].ul>>32, av[k].v, 1);
                asg_arc_del(ug->g, av[k].v^1, av[k].ul>>32^1, 1);
            }
        }
    }

    for (i = 0; i < b_0->b.n; i++)
    {
        v = b_0->b.a[i];
        visit[v>>1] = 1;
        if(purge_g->seq[v>1].c != ALTER_LABLE) continue;
        asg_seq_drop(purge_g, v>1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movq %rcx, %r12
movq %rdx, 0x68(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x30(%rsp)
movl %ebp, %ecx
movq 0x38(%r12), %rax
cmpq %rcx, %rax
jbe 0xd061c
movq 0x48(%r12), %rax
movl (%rax,%rcx,4), %ecx
movl %ecx, %eax
shrl %eax
imulq $0x28, %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
addq %rbx, %r14
movq %rcx, 0x58(%rsp)
testb $0x1, %cl
movq %rbx, 0x60(%rsp)
jne 0xd0045
xorl %edi, %edi
xorl %r12d, %r12d
xorl %r13d, %r13d
movl 0x10(%r14), %edx
cmpq %rdx, %r13
jae 0xd00ab
cmpq %r12, %r13
jne 0xd0034
movq %r14, %r15
leaq (%r12,%r12), %r14
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r14, %r12
movq %r15, %r14
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rax
movq %rax, (%rdi,%r13,8)
incq %r13
jmp 0xcfffb
movq %rbp, %rbx
movl 0x10(%r14), %edx
pushq $-0x1
popq %rax
movl %eax, %ebp
xorl %edi, %edi
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpl %r13d, %edx
jbe 0xd00b2
cmpq %r12, %r13
jne 0xd0088
leaq (%r12,%r12), %r15
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movq %rax, %rdi
movl 0x10(%r14), %edx
movq %r15, %r12
movq 0x18(%r14), %rax
leal (%rdx,%rbp), %ecx
movq (%rax,%rcx,8), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
xorq %rcx, %rax
movq %rax, (%rdi,%r13,8)
incq %r13
decl %ebp
jmp 0xd0059
movq %rdi, 0x18(%rsp)
jmp 0xd00ba
movq %rdi, 0x18(%rsp)
movq %rbx, %rbp
decl %edx
movq %rdx, 0x38(%rsp)
movq $0x0, 0x20(%rsp)
movl %ebp, %eax
leal 0x1(%rax), %esi
movq 0x30(%rsp), %rcx
movq %rsi, 0x48(%rsp)
cmpq %rsi, 0x38(%rcx)
jbe 0xd0477
movq 0x30(%rsp), %rcx
movq 0x48(%rcx), %rcx
movl %eax, %eax
movl (%rcx,%rax,4), %edx
movq %rdx, 0x10(%rsp)
movq 0x48(%rsp), %rax
movl (%rcx,%rax,4), %ecx
movq 0x68(%rsp), %rax
movq (%rax), %rbp
movl %edx, %ebx
shrl %ebx
imulq $0x18, %rbx, %r15
movq %rbx, 0x90(%rsp)
leaq (%r15,%rbp), %rdi
movl %ecx, 0x54(%rsp)
movl %ecx, %r14d
shrl %r14d
movl %ebx, %esi
movl %r14d, %edx
callq 0xcd675
movq 0x10(%rbp,%r15), %rdx
movslq %eax, %rsi
shlq $0x6, %rsi
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rcx
imulq $0x28, %rbx, %rax
addq %rcx, %rax
movq %rax, 0x98(%rsp)
imulq $0x28, %r14, %rbp
addq %rcx, %rbp
movl 0x14(%rdx,%rsi), %r10d
movl 0x18(%rdx,%rsi), %r11d
movq 0x20(%rsp), %rax
cmpl %r10d, %eax
cmoval %eax, %r10d
decl %r11d
movq 0x38(%rsp), %rax
cmpl %r11d, %eax
cmovbl %eax, %r11d
movl 0x1c(%rdx,%rsi), %eax
movq %rax, 0x40(%rsp)
movl 0x20(%rdx,%rsi), %r15d
decl %r15d
movq 0x10(%rsp), %rax
andl $0x1, %eax
movq %rdx, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movzbl (%rdx,%rsi), %ecx
movq %rcx, 0x10(%rsp)
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x110(%rsp), %rdx
movq 0x118(%rsp), %rcx
movq 0x120(%rsp), %r8
movl 0x140(%rsp), %r9d
leaq 0xc0(%rsp), %rbx
pushq %rbx
leaq 0xb0(%rsp), %rbx
pushq %rbx
pushq 0x148(%rsp)
pushq 0x148(%rsp)
pushq 0x148(%rsp)
pushq 0x38(%rsp)
movq %rax, 0x40(%rsp)
pushq %rax
pushq %r15
pushq 0x80(%rsp)
pushq %r11
pushq %r10
movq %r14, 0x98(%rsp)
pushq %r14
pushq 0xf0(%rsp)
pushq %rbp
pushq 0x108(%rsp)
movl 0x1c0(%rsp), %eax
pushq %rax
callq 0xcf346
addq $0x80, %rsp
cmpl $0x0, 0xb4(%rsp)
js 0xd0477
cmpl $0x0, 0xcc(%rsp)
js 0xd0477
movq 0x150(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0xd029b
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq 0x38(%rsp), %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
imulq $0x18, %rcx, %rcx
movq 0xb8(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0xa8(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0xd02f6
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq 0x20(%rsp), %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
imulq $0x18, %rcx, %rcx
movq 0xd0(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0xc0(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq 0xa8(%rsp), %rax
shrq $0x21, %rax
movq 0x120(%rsp), %rcx
movl (%rcx,%rax,8), %eax
subl %eax, %r14d
subl %esi, %eax
cmpl $0x0, 0x10(%rsp)
cmovel %r14d, %eax
subq %rax, %r13
movl 0xb0(%rsp), %eax
shrl %eax
movq (%rcx,%rax,8), %rax
testb $0x1, 0x54(%rsp)
jne 0xd03b5
movl 0x10(%rbp), %r14d
decl %r14d
movq %rax, 0x20(%rsp)
movl %eax, %r15d
movq 0x18(%rsp), %rdi
cmpl %r14d, %r15d
ja 0xd041b
cmpq %r12, %r13
jne 0xd039e
leaq (%r12,%r12), %rbx
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rbx, %r12
movq 0x18(%rbp), %rax
movl %r15d, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, (%rdi,%r13,8)
incq %r13
incl %r15d
jmp 0xd036f
movq %rax, %rbx
movl %eax, %r14d
movq 0x18(%rsp), %rdi
cmpq %r12, %r13
jne 0xd03e6
leaq (%r12,%r12), %r15
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r15, %r12
movq 0x18(%rbp), %rax
movq (%rax,%r14,8), %rax
movabsq $0x100000000, %rcx      # imm = 0x100000000
xorq %rcx, %rax
movq %rax, (%rdi,%r13,8)
incq %r13
addq $-0x1, %r14
jb 0xd03c0
movq %rdi, 0x18(%rsp)
movl %ebx, %r14d
movq $0x0, 0x20(%rsp)
jmp 0xd0420
movq %rdi, 0x18(%rsp)
movq %r14, 0x38(%rsp)
movq 0x28(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x40(%rsp), %rcx
movb $0x1, 0x4(%rax,%rcx,8)
movq 0x48(%rsp), %rbx
movl %ebx, %eax
cmpq $0x0, 0x160(%rsp)
je 0xd00cc
movq 0x88(%rsp), %rdx
addq 0x80(%rsp), %rdx
movq 0x160(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0xcfe1a
movl %ebx, %eax
jmp 0xd00cc
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movl %r13d, %esi
movq 0x118(%rsp), %rdx
movq 0x150(%rsp), %rcx
xorl %r8d, %r8d
leaq 0xa0(%rsp), %r9
callq 0xcfa82
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movq 0x60(%rsp), %r15
leaq (%rbx,%r15), %r14
movq 0x18(%rbx,%r15), %rdi
callq 0x2220
movq %rbp, %rdx
movq %rbp, 0x18(%rbx,%r15)
movl %r13d, 0x10(%rbx,%r15)
movl %r12d, 0xc(%rbx,%r15)
movl 0xa0(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl %eax, (%rbx,%r15)
movl 0x4(%rbx,%r15), %ecx
movq (%rbp), %rax
shrq $0x20, %rax
cmpq %rcx, %rax
movq 0x48(%rsp), %rbp
je 0xd0584
movl %eax, 0x4(%r14)
movq 0x58(%rsp), %rax
leaq (,%rax,8), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rdx
orq $0x8, %rax
movq (%rdx,%rax), %rax
movl %eax, %edx
shrq $0x20, %rax
imulq $0x18, %rax, %rbx
addq 0x8(%rcx), %rbx
imulq $0x18, %rdx, %r15
xorl %r12d, %r12d
cmpq %r12, %r15
je 0xd057c
cmpl $0x0, 0xc(%rbx,%r12)
js 0xd0576
movq 0x8(%rsp), %r13
movq 0x18(%r13), %rdi
movl 0x4(%rbx,%r12), %esi
movl 0x8(%rbx,%r12), %edx
callq 0xcf2a7
movq 0x18(%r13), %rdi
movl 0x4(%rbx,%r12), %edx
movl 0x8(%rbx,%r12), %esi
xorl $0x1, %esi
xorl $0x1, %edx
callq 0xcf2a7
addq $0x18, %r12
jmp 0xd0538
movq 0x18(%r14), %rdx
movl 0x10(%r14), %r13d
movl 0x8(%r14), %ecx
decl %r13d
movl 0x4(%rdx,%r13,8), %eax
xorq $0x1, %rax
cmpq %rcx, %rax
movq 0x30(%rsp), %r12
je 0xcffb8
movl %eax, 0x8(%r14)
movq 0x58(%rsp), %rdx
andl $-0x2, %edx
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x28(%rax), %rcx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rbx
addq 0x8(%rax), %rbx
imulq $0x18, %rdx, %r14
xorl %r15d, %r15d
cmpq %r15, %r14
je 0xcffb8
cmpl $0x0, 0xc(%rbx,%r15)
js 0xd0616
movq 0x8(%rsp), %r13
movq 0x18(%r13), %rdi
movl 0x4(%rbx,%r15), %esi
movl 0x8(%rbx,%r15), %edx
callq 0xcf2a7
movq 0x18(%r13), %rdi
movl 0x4(%rbx,%r15), %edx
movl 0x8(%rbx,%r15), %esi
xorl $0x1, %esi
xorl $0x1, %edx
callq 0xcf2a7
addq $0x18, %r15
jmp 0xd05d4
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0xd0713
movq 0x48(%r12), %rax
movq %rcx, 0x10(%rsp)
movl (%rax,%rcx,4), %eax
movl %eax, %ecx
shrl %ecx
movq 0x158(%rsp), %rdx
movb $0x1, (%rdx,%rcx)
movq 0x28(%rsp), %rcx
movq 0x20(%rcx), %rcx
xorl %edx, %edx
cmpl $0x2, %eax
setae %dl
cmpb $0x1, 0x4(%rcx,%rdx,8)
jne 0xd06fd
xorl %r15d, %r15d
cmpl $0x2, %eax
setae %r15b
addl %r15d, %r15d
xorl %r12d, %r12d
cmpq $0x2, %r12
je 0xd06fd
movq %r12, %rbx
orq %r15, %rbx
movq 0x28(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movq (%rcx,%rbx,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
xorl $0x1, %ebx
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r13
addq $0xc, %r13
imulq $0x18, %rdx, %rbp
xorl %r14d, %r14d
cmpq %r14, %rbp
je 0xd06f5
movl (%r13,%r14), %eax
testl %eax, %eax
js 0xd06ef
movq 0x28(%rsp), %rcx
movq 0x20(%rcx), %rcx
movl -0x4(%r13,%r14), %esi
movl %esi, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xd06ef
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%r13,%r14)
xorl $0x1, %esi
movq 0x28(%rsp), %rdi
movl %ebx, %edx
callq 0xcf2a7
addq $0x18, %r14
jmp 0xd06af
incq %r12
jmp 0xd0672
movq 0x10(%rsp), %rcx
incl %ecx
movq 0x30(%rsp), %r12
movq 0x38(%r12), %rax
jmp 0xd061e
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq