// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: merge_ug_nodes(ma_ug_t*, asg_t*, kvec_t_u64_warp*)
void merge_ug_nodes(ma_ug_t *ug, asg_t* read_g, kvec_t_u64_warp* array)
{
    if(array->a.n == 0) return;
    uint32_t i, k, v, w;
    ma_utg_t result;
    memset(&result, 0, sizeof(ma_utg_t));
    v = w = (uint32_t)-1;
    for (i = 0; i < array->a.n; i++)
    {
        w = array->a.a[i];
        collect_ma_utg_ts(ug, v, w, &result);
        v = w;
    }

    

    if(result.n == 0) return;
    asg_arc_t *av = NULL;
    uint32_t nv, l;
    result.len = 0;
    for (i = 0; i < result.n - 1; i++)
    {
        
        v = (uint64_t)(result.a[i])>>32;
        w = (uint64_t)(result.a[i + 1])>>32; 
        av = asg_arc_a(read_g, v);
        nv = asg_arc_n(read_g, v);
        l = 0;
        
        for (k = 0; k < nv; k++)
        {
            if(av[k].del) continue;
            if(av[k].v == w) 
            {
                l = asg_arc_len(av[k]);
                break;
            }
        }
        if(k == nv) fprintf(stderr ,"******error, i: %u, k: %u, nv: %u\n", i, k, nv);
        result.a[i] = v; result.a[i] = result.a[i]<<32; result.a[i] = result.a[i] | (uint64_t)(l);
        result.len += l;
    }
    

    if(i < result.n)
    {
        v = (uint64_t)(result.a[i])>>32;
        l = read_g->seq[v>>1].len;
        result.a[i] = v;
        result.a[i] = result.a[i]<<32;
        result.a[i] = result.a[i] | (uint64_t)(l);
        result.len += l;
    }
    //has already set result.a, result.len, result.n, result.m
    result.circ = 0;
    result.start = result.a[0]>>32;
    result.end = (result.a[result.n-1]>>32)^1;

    
    uint32_t beg_uid = array->a.a[0];
    uint32_t end_uid = array->a.a[array->a.n-1];
    uint32_t realLen = array->a.n;
    uint32_t new_uid = array->a.a[0]>>1;
    uint64_t kmp;





    /*******************************just for debug**********************************/
    /**
    if(beg_uid&1)
    {
        if(result.start != ug->u.a[beg_uid>>1].end)
        {
            fprintf(stderr, "ERROR\n");
        }
    }
    else
    {
        if(result.start != ug->u.a[beg_uid>>1].start)
        {
            fprintf(stderr, "ERROR\n");
        }
    }

    if(end_uid&1)
    {
        if(result.end != ug->u.a[end_uid>>1].start)
        {
            fprintf(stderr, "ERROR\n");
        }
    }
    else
    {
        if(result.end != ug->u.a[end_uid>>1].end)
        {
            fprintf(stderr, "ERROR\n");
        }
    }
    **/
    /*******************************just for debug**********************************/

    asg_arc_t *aw = NULL;
    uint32_t nw = 0;
    ///corresponding to direction 1 of new node
    v = beg_uid^1;
    av = asg_arc_a(ug->g, v);
    nv = asg_arc_n(ug->g, v);
    ///fprintf(stderr, "beg_uid_v>>1: %u, beg_uid_v&1: %u, nv: %u\n", v>>1, v&1, nv);
    for (k = 0; k < nv; k++)
    {
        if(av[k].del) continue;
        kmp = new_uid<<1; kmp = kmp^1; kmp = kmp << 32;

        ///if((av[k].v>>1) == (end_uid>>1)) continue;
        w = av[k].v^1; aw = asg_arc_a(ug->g, w); nw = asg_arc_n(ug->g, w);
        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;
            if(aw[i].v == (v^1)) break;
        }
        if(i == nw) fprintf(stderr, "ERROR at %s:%d\n", __FILE__, __LINE__);
        kmp = kmp | (uint64_t)(aw[i].ol);


        ///kmp = kmp | (uint64_t)(((ug->g)->idx[v]>>32) + k);/**kmp = kmp | av[k].v;**/
        ///here kmp is ul
        kv_push(uint64_t, array->a, kmp);
        kmp = av[k].ol; kmp = kmp<<32; kmp = kmp|(uint64_t)(av[k].v);
        ///here kmp is ol + v
        kv_push(uint64_t, array->a, kmp);
        ///fprintf(stderr, "*av[%u].v>>1: %u, v&1: %u, ol: %u\n", k, av[k].v>>1, av[k].v&1, av[k].ol);
    }

    ///corresponding to direction 0 of new node
    v = end_uid;
    av = asg_arc_a(ug->g, v);
    nv = asg_arc_n(ug->g, v);
    ///fprintf(stderr, "end_uid_v>>1: %u, end_uid_v&1: %u, nv: %u\n", v>>1, v&1, nv);
    for (k = 0; k < nv; k++)
    {
        if(av[k].del) continue;
        kmp = new_uid<<1; kmp = kmp << 32;

        w = av[k].v^1; 
        aw = asg_arc_a(ug->g, w); 
        nw = asg_arc_n(ug->g, w);
        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;
            if(aw[i].v == (v^1)) break;
        }
        if(i == nw) fprintf(stderr, "ERROR at %s:%d\n", __FILE__, __LINE__);
        kmp = kmp | (uint64_t)(aw[i].ol);


        ///here kmp is ul
        kv_push(uint64_t, array->a, kmp);
        kmp = av[k].ol; kmp = kmp<<32; kmp = kmp|(uint64_t)(av[k].v);
        ///here kmp is ol + v
        kv_push(uint64_t, array->a, kmp);
        ///fprintf(stderr, "#av[%u].v>>1: %u, v&1: %u, ol: %u\n", k, av[k].v>>1, av[k].v&1, av[k].ol);
    }

    
    ma_utg_t* tmp;
    for (i = 0; i < realLen; i++)
    {
        w = array->a.a[i];
        tmp = &(ug->u.a[w>>1]);
        if(tmp->m != 0)
        {
            tmp->circ = tmp->end = tmp->len = tmp->m = tmp->n = tmp->start = 0;
            free(tmp->a);
            tmp->a = NULL;
        }
        asg_seq_del(ug->g, w>>1);
    }
    
    ug->u.a[beg_uid>>1] = result;
    ug->g->seq[beg_uid>>1].del = 0;
    ug->g->seq[beg_uid>>1].len = result.len;

    uint32_t oLen = 0;
    for (; i < array->a.n; i += 2)
    {
        v = array->a.a[i]>>32;
        w = (uint32_t)array->a.a[i+1];
        /****************************may have bugs********************************/
        ///may have bug here, if there is an edge between beg_uid and end_uid
        ///if(((w>>1) == (beg_uid>>1)) || ((w>>1) == (end_uid>>1))) continue;
        if(((w>>1) == (beg_uid>>1)) || ((w>>1) == (end_uid>>1))) w = v;
        /****************************may have bugs********************************/

        oLen = array->a.a[i+1]>>32;
        asg_append_edges_to_srt(ug->g, v, ug->u.a[v>>1].len, w, oLen, 0, 0, 0);
        oLen = (uint32_t)array->a.a[i];
        asg_append_edges_to_srt(ug->g, w^1, ug->u.a[w>>1].len, v^1, oLen, 0, 0, 0);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, 0x8(%rsp)
movq (%rdx), %rax
testq %rax, %rax
je 0xb18ba
movq %rdx, %rbx
movq %rsi, 0x18(%rsp)
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %r12
andq $0x0, 0x20(%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
xorl %ecx, %ecx
pushq $-0x1
popq %rsi
movl %ecx, %r14d
cmpq %r14, %rax
jbe 0xb127b
movq 0x10(%rbx), %rax
movl (%rax,%r14,8), %ebp
movq 0x8(%rsp), %rdi
movl %ebp, %edx
movq %r12, %rcx
callq 0xb05d2
leal 0x1(%r14), %ecx
movq (%rbx), %rax
movl %ebp, %esi
jmp 0xb1251
movl 0x50(%rsp), %r9d
testl %r9d, %r9d
je 0xb18ba
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movl $0x80000000, %ebp       # imm = 0x80000000
andl 0x40(%rsp), %ebp
decl %r9d
movq 0x58(%rsp), %r12
xorl %r13d, %r13d
movq 0x18(%rsp), %r8
movq %r9, 0x20(%rsp)
cmpq %r9, %r13
je 0xb1379
movq (%r12,%r13,8), %r14
leaq 0x1(%r13), %r11
movl 0xc(%r12,%r13,8), %eax
movq 0x8(%r8), %rsi
movq 0x28(%r8), %rcx
movq %r14, %rdx
shrq $0x20, %rdx
movq (%rcx,%rdx,8), %rcx
movq %rcx, %rdi
shrq $0x20, %rdi
movl %ecx, %edx
imulq $0x18, %rdi, %rdi
addq %rdi, %rsi
addq $0xc, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0xb1338
cmpl $0x0, (%rsi)
js 0xb12ff
cmpl %eax, -0x4(%rsi)
je 0xb1308
incq %rdi
addq $0x18, %rsi
jmp 0xb12f0
movl -0xc(%rsi), %r15d
cmpl %edi, %ecx
je 0xb133b
andq %r10, %r14
movl %r15d, %eax
orq %r14, %rax
movq %rax, (%r12,%r13,8)
addl %ebp, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
andl $0x80000000, %ebp       # imm = 0x80000000
orl %r15d, %ebp
movq %r11, %r13
jmp 0xb12b1
xorl %r15d, %r15d
movq 0x1a1c9e(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1393b9(%rip), %rsi    # 0x1ea705
movl %r13d, %edx
movl %ecx, %r8d
xorl %eax, %eax
movq %r11, 0x10(%rsp)
callq 0x22b0
movq 0x10(%rsp), %r11
movabsq $-0x100000000, %r10     # imm = 0xFFFFFFFF00000000
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %r8
jmp 0xb1310
movq (%r12,%r9,8), %rax
andq %rax, %r10
movq 0x20(%r8), %rcx
movq %rax, %rdx
shrq $0x21, %rdx
movl (%rcx,%rdx,8), %ecx
addl %ecx, %ebp
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orq %r10, %rcx
movq %rcx, (%r12,%r9,8)
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
shrq $0x20, %rax
xorl $0x1, %eax
movl %ebp, 0x34(%rsp)
movl %ebp, 0x40(%rsp)
movl 0x4(%r12), %ecx
movl %ecx, 0x44(%rsp)
movl %eax, 0x48(%rsp)
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
movq (%rax), %r13
movl %r13d, %ecx
movq %rdx, 0x38(%rsp)
movq -0x8(%rax,%rdx,8), %r8
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x28(%rdx), %rsi
xorq $0x1, %rcx
movq (%rsi,%rcx,8), %rcx
movl %ecx, %esi
movq %rsi, 0x28(%rsp)
shrq $0x20, %rcx
imulq $0x18, %rcx, %rsi
addq 0x8(%rdx), %rsi
movl %r8d, %ecx
movq %rcx, 0x70(%rsp)
movq %r13, %rcx
shlq $0x20, %rcx
movq %rcx, 0x18(%rsp)
xorl %ecx, %ecx
movq %r8, 0x10(%rsp)
cmpq 0x28(%rsp), %rcx
je 0xb158b
imulq $0x18, %rcx, %r12
cmpl $0x0, 0xc(%rsi,%r12)
js 0xb1553
movq %rcx, 0x20(%rsp)
movq %rsi, %rbp
addq %rsi, %r12
movl 0x8(%r12), %ecx
xorq $0x1, %rcx
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x8(%rdx), %rsi
movq 0x28(%rdx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdi
leaq (%rsi,%rdi), %r15
movl %ecx, %edx
addq %rdi, %rsi
addq $0xc, %rsi
xorl %r14d, %r14d
cmpq %r14, %rdx
je 0xb155b
cmpl $0x0, (%rsi)
js 0xb148c
cmpl %r13d, -0x4(%rsi)
je 0xb1495
incq %r14
addq $0x18, %rsi
jmp 0xb1478
cmpl %r14d, %ecx
je 0xb155e
movl %r14d, %ecx
imulq $0x18, %rcx, %rcx
movl 0xc(%r15,%rcx), %r14d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xb14d8
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
orq 0x18(%rsp), %r14
btsq $0x20, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
movl 0xc(%r12), %r15d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r15d
movl 0x8(%r12), %r14d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xb1534
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
movq %rbp, %rsi
shlq $0x20, %r15
orq %r15, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
movq 0x10(%rsp), %r8
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0xb1419
movq %rcx, %r14
movq 0x1a1a7b(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x136257(%rip), %rsi    # 0x1e77c6
leaq 0x13681b(%rip), %rdx    # 0x1e7d91
movl $0x7983, %ecx           # imm = 0x7983
xorl %eax, %eax
callq 0x22b0
movq 0x10(%rbx), %rax
jmp 0xb149e
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq 0x28(%rcx), %rdx
movq 0x70(%rsp), %rsi
movq (%rdx,%rsi,8), %rdx
movl %edx, %esi
movq %rsi, 0x28(%rsp)
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
addq 0x8(%rcx), %rdx
movabsq $-0x200000000, %rcx     # imm = 0xFFFFFFFE00000000
andq %rcx, 0x18(%rsp)
xorl %ecx, %ecx
cmpq 0x28(%rsp), %rcx
je 0xb1737
imulq $0x18, %rcx, %r12
cmpl $0x0, 0xc(%rdx,%r12)
js 0xb16ff
movq %rcx, 0x20(%rsp)
movq %rdx, %rbp
addq %rdx, %r12
movl 0x8(%r12), %ecx
xorq $0x1, %rcx
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq 0x8(%rdx), %rsi
movq 0x28(%rdx), %rdx
movq (%rdx,%rcx,8), %rcx
movq %rcx, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdi
leaq (%rsi,%rdi), %r15
movl %ecx, %edx
addq %rdi, %rsi
addq $0xc, %rsi
xorl %r14d, %r14d
cmpq %r14, %rdx
je 0xb1707
cmpl $0x0, (%rsi)
js 0xb163d
movl -0x4(%rsi), %edi
xorl %r8d, %edi
cmpl $0x1, %edi
je 0xb1646
incq %r14
addq $0x18, %rsi
jmp 0xb1624
cmpl %r14d, %ecx
je 0xb170a
movl %r14d, %ecx
imulq $0x18, %rcx, %rcx
movl 0xc(%r15,%rcx), %r14d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xb1689
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
orq 0x18(%rsp), %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
movl 0xc(%r12), %r15d
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %r15d
movl 0x8(%r12), %r14d
movq (%rbx), %rcx
cmpq 0x8(%rbx), %rcx
jne 0xb16e0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
shlq $0x20, %r15
orq %r15, %r14
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movq %r14, (%rax,%rcx,8)
movq 0x10(%rsp), %r8
movq %rbp, %rdx
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0xb15c5
movq %rcx, %r14
movq 0x1a18cf(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1360ab(%rip), %rsi    # 0x1e77c6
leaq 0x13666f(%rip), %rdx    # 0x1e7d91
movl $0x79a2, %ecx           # imm = 0x79A2
xorl %eax, %eax
callq 0x22b0
movq 0x10(%rbx), %rax
jmp 0xb164f
movl 0x38(%rsp), %ebp
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0xb179b
movq 0x10(%rbx), %rax
movl (%rax,%r12,8), %r15d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
shrl %r15d
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%r14,%rax)
je 0xb1780
addq %rax, %r14
andl $0x0, 0x10(%r14)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movq 0x18(%r14), %rdi
callq 0x2220
andq $0x0, 0x18(%r14)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rdi
movl %r15d, %esi
callq 0x6f8da
incq %r12
movq 0x10(%rsp), %r8
jmp 0xb173e
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rax
shrl %r13d
imulq $0x28, %r13, %rcx
movq 0x60(%rsp), %rdx
movq %rdx, 0x20(%rax,%rcx)
movaps 0x40(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
movups %xmm1, 0x10(%rax,%rcx)
movups %xmm0, (%rax,%rcx)
movq 0x18(%rsi), %rax
movq 0x20(%rax), %rax
andl $0x7fffffff, (%rax,%r13,8) # imm = 0x7FFFFFFF
movq 0x18(%rsi), %rax
movq 0x20(%rax), %rax
movl $0x80000000, %ecx       # imm = 0x80000000
andl (%rax,%r13,8), %ecx
addl 0x34(%rsp), %ecx
movl %ecx, (%rax,%r13,8)
shrl %r8d
movq %r8, 0x10(%rsp)
movq 0x38(%rsp), %rax
movl %eax, %r14d
cmpq %r14, (%rbx)
jbe 0xb18ba
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, %r15
shrq $0x20, %r15
leal 0x1(%r14), %edx
movq (%rax,%rdx,8), %r8
movl %r8d, %eax
shrl %eax
cmpl 0x10(%rsp), %eax
movl %r8d, %r12d
cmovel %r15d, %r12d
cmpl %r13d, %eax
cmovel %r15d, %r12d
shrq $0x20, %r8
movq %rbx, %rbp
movq 0x8(%rsp), %rbx
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdi
shrq $0x21, %rcx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %edx
movl %r15d, %esi
movl %r12d, %ecx
xorl %r9d, %r9d
pushq $0x0
pushq $0x0
callq 0xac072
popq %rcx
popq %rdx
movq 0x10(%rbp), %rax
movl (%rax,%r14,8), %r8d
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rdi
movq %rbp, %rbx
movl %r12d, %esi
xorl $0x1, %esi
shrl %r12d
imulq $0x28, %r12, %rcx
movl (%rax,%rcx), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %edx
xorl $0x1, %r15d
movl %r15d, %ecx
xorl %r9d, %r9d
pushq $0x0
pushq $0x0
callq 0xac072
popq %rcx
popq %rdx
leal 0x2(%r14), %eax
jmp 0xb17fe
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq