// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: resolve_tangles(ma_ug_t*, asg_t*, ma_hit_t_alloc*, long long, long long, float, float, R_to_U*, unsigned char*, unsigned int, float)
void resolve_tangles(ma_ug_t *src, asg_t *read_g, ma_hit_t_alloc* reverse_sources, long long minLongUntig, 
long long maxShortUntig, float l_untig_rate, float max_node_threshold, R_to_U* ruIndex, uint8_t* is_r_het, 
uint32_t trio_flag, float drop_ratio)
{
    buf_t b_0, b_1;
    memset(&b_0, 0, sizeof(buf_t));
    memset(&b_1, 0, sizeof(buf_t));
    uint32_t i, v, w, sv, n_vtx, beg, end, next_uID = (uint32_t)-1;
    kvec_t_u32_warp u_vecs;
    kv_init(u_vecs.a);

    kvec_t_u64_warp e_vecs;
    kv_init(e_vecs.a);

    ///note: we must reset start for each unitig
    n_vtx = src->g->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(src->g->seq[v].del) continue;
        if(src->u.a[v].m==0) continue;
        EvaluateLen(src->u, v) = src->u.a[v].n;
    }

    ma_ug_t *ug = NULL;
    ug = copy_untig_graph(src);

    asg_t* nsg = ug->g;
    n_vtx = nsg->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        ///nsg->seq[v].c = PRIMARY_LABLE;
        EvaluateLen(ug->u, v) = ug->u.a[v].n;
        IsMerge(ug->u, v) = 0;
    }

    uint8_t* visit = NULL;
    visit = (uint8_t*)malloc(sizeof(uint8_t) * nsg->n_seq);
    uint32_t n_reduce, flag, dbg_round = 0;
    n_vtx = nsg->n_seq * 2;
    while (1)
    {
        n_reduce = 0;
        for (v = 0; v < n_vtx; ++v) 
        {
            //as for return value: 0: do nothing, 1: unroll, 2: convex
            //we just need 1
            sv = v;
            flag = 0;
            while (1)
            {
                flag = walk_through(read_g, ug, reverse_sources, minLongUntig, 
                    maxShortUntig, l_untig_rate, max_node_threshold, &b_0, &b_1, 
                    &u_vecs, visit, sv, &beg, &end, &next_uID, ruIndex, is_r_het);
                n_reduce += flag;
                if(flag != UNROLL_M)
                {
                    break;
                }
            }
        }
        if(n_reduce == 0) break;
        dbg_round++;
    }
    asg_cleanup(nsg);
	asg_symm(nsg);

    uint32_t uId, rId, is_Unitig, m, pre;
    ma_utg_t* nsu = NULL;
    for (v = 0; v < src->g->n_seq; v++)
    {
        uId = v;
        nsu = &(src->u.a[v]);
        if(nsu->m == 0) continue;
        if(src->g->seq[v].del) continue;
        for (i = 0; i < nsu->n; i++)
        {
            rId = nsu->a[i]>>33;
            ///ori = nsu->a[k]>>32&1;
            set_R_to_U(ruIndex, rId, uId, 1, &(read_g->seq[rId].c));
        }
    }

    n_vtx = nsg->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(nsg->seq[v].del) continue;
        nsu = &(ug->u.a[v]);
        m = 0;
        pre = (uint32_t)(-1);
        for (i = 0; i < nsu->n; i++)
        {
            rId = nsu->a[i]>>33;
            get_R_to_U(ruIndex, rId, &uId, &is_Unitig);
            if(is_Unitig != 1 || uId == ((uint32_t)(-1))) continue;
            if(i > 0 && pre == uId) continue;
            pre = uId;
            nsu->a[m] = uId;
            m++;
        }
        nsu->n = m;
    }
    n_vtx = nsg->n_seq;
    for (i = 0; i < n_vtx; ++i) 
    {
        v = i;
        nsu = &(ug->u.a[v]);
        if(nsg->seq[v].del) continue;
        if(IsMerge(ug->u, v) == 0) continue;
        if(nsg->seq[v].c == ALTER_LABLE) continue;

        v = v<<1;
        if(get_real_length(nsg, v, NULL) != 1) continue;
        get_real_length(nsg, v, &w);
        if(get_real_length(nsg, w^1, NULL) != 1) continue;
        if(IsMerge(ug->u, w>>1) != 0) continue;
        beg = w^1;


        v = v^1;
        if(get_real_length(nsg, v, NULL) != 1) continue;
        get_real_length(nsg, v, &w);
        if(get_real_length(nsg, w^1, NULL) != 1) continue;
        if(IsMerge(ug->u, w>>1) != 0) continue;
        end = w;
        ///we have three types of merged nodes
        ///1) CONVEX_M: one direction has two out-nodes, another direction has one out-node
        ///2) UNROLL_E: one direction has one out-node, another direction doesn't has out-node
        ///3) UNROLL_M: both directions have one out-node
        ///here we just need UNROLL_M
        ///beg and end must be unchanged in src
        unroll_tangle(src, ug, nsu->a, nsu->n, beg, end, &e_vecs, trio_flag, &b_0, visit, drop_ratio);
    }

    for (v = 0; v < ruIndex->len; v++)
    {
        get_R_to_U(ruIndex, v, &uId, &is_Unitig);
        if(is_Unitig == 1) ruIndex->index[v] = (uint32_t)-1;
    }
    kv_destroy(u_vecs.a);
    kv_destroy(e_vecs.a);
    
    ma_ug_destroy(ug);
    free(visit);
    free(b_0.b.a);
    free(b_1.b.a);


    ///note: we must reset start for each unitig
    n_vtx = src->g->n_seq;
    for (v = 0; v < n_vtx; ++v) 
    {
        if(src->g->seq[v].del) continue;
        if(src->u.a[v].m==0) continue;
        EvaluateLen(src->u, v) = src->u.a[v].n;
    }
    ///print_untig_by_read(src, "m64076_200203_181219/82511682/ccs", 2429597, NULL, NULL, "end-1");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movss %xmm2, 0x34(%rsp)
movq %r9, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm2, %xmm2
movaps %xmm2, 0xc0(%rsp)
movaps %xmm2, 0xd0(%rsp)
movaps %xmm2, 0xe0(%rsp)
movaps %xmm2, 0xf0(%rsp)
movaps %xmm2, 0x100(%rsp)
movaps %xmm2, 0x110(%rsp)
xorl %eax, %eax
movq %rax, 0x120(%rsp)
movaps %xmm2, 0x130(%rsp)
movaps %xmm2, 0x140(%rsp)
movaps %xmm2, 0x150(%rsp)
movaps %xmm2, 0x160(%rsp)
movaps %xmm2, 0x170(%rsp)
movaps %xmm2, 0x180(%rsp)
movq %rax, 0x190(%rsp)
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
movq %rax, 0xb0(%rsp)
movaps %xmm2, 0xa0(%rsp)
movq %rax, 0x90(%rsp)
movaps %xmm2, 0x80(%rsp)
movq 0x18(%rdi), %rax
movl 0x14(%rax), %esi
andq $0x7fffffff, %rsi       # imm = 0x7FFFFFFF
je 0xd3cea
movq 0x20(%rax), %rax
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %r9
xorl %r10d, %r10d
cmpl $0x0, (%rax)
js 0xd3cdd
movq 0x10(%rbx), %rsi
cmpl $0x0, 0xc(%rsi,%r10)
je 0xd3cdd
movl 0x10(%rsi,%r10), %edi
movl %edi, 0x4(%rsi,%r10)
addq $0x8, %rax
addq $0x28, %r10
cmpq %r10, %r9
jne 0xd3cc2
movq %rdx, 0x60(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x70(%rsp)
movss %xmm0, 0x38(%rsp)
movss %xmm1, 0x3c(%rsp)
movq %rbx, %rdi
callq 0xc8eab
movq 0x18(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x14(%rcx), %r12d
movq %r12, %rcx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
movq %rbx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movq %r15, 0x50(%rsp)
je 0xd3d75
movq 0x10(%rax), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movl 0x10(%rax,%rdx), %esi
movl %esi, 0x4(%rax,%rdx)
movl $0x0, 0x8(%rax,%rdx)
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xd3d48
movq 0x8(%rsp), %rax
movl 0x14(%rax), %r12d
movl %r12d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
jmp 0xd3d77
xorl %edi, %edi
callq 0x2280
addl %r12d, %r12d
movq %rax, 0x28(%rsp)
je 0xd3e78
movq 0x1d0(%rsp), %r10
xorl %ecx, %ecx
leaq 0x44(%rsp), %r11
leaq 0x48(%rsp), %r14
leaq 0x4c(%rsp), %r15
leaq 0xa0(%rsp), %r13
leaq 0x130(%rsp), %rbx
xorl %ebp, %ebp
movq %r12, 0x58(%rsp)
movq %rbp, 0x78(%rsp)
movl %ecx, 0x40(%rsp)
subq $0x8, %rsp
movq 0x58(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r8
movss 0x40(%rsp), %xmm0
movss 0x44(%rsp), %xmm1
leaq 0xc8(%rsp), %r9
pushq %r10
pushq 0x28(%rsp)
pushq %r11
pushq %r14
pushq %r15
pushq %rbp
pushq %rax
pushq %r13
pushq %rbx
movq %r15, %r12
movq %r10, %r15
movq %rbx, %rbp
movq %r13, %rbx
movq %r14, %r13
movq %r11, %r14
callq 0x106525
movl 0x90(%rsp), %ecx
movq %r14, %r11
movq %r13, %r14
movq %rbx, %r13
movq %rbp, %rbx
movq 0xc8(%rsp), %rbp
movq %r15, %r10
movq %r12, %r15
addq $0x50, %rsp
addl %eax, %ecx
cmpl $0x2, %eax
movq 0x28(%rsp), %rax
je 0xd3dbf
incl %ebp
movq 0x58(%rsp), %r12
cmpl %r12d, %ebp
jne 0xd3dba
testl %ecx, %ecx
je 0xd3e78
movl $0x0, %ecx
movl $0x0, %ebp
testl %r12d, %r12d
jne 0xd3dba
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
movl $0x80000000, %ecx       # imm = 0x80000000
orl 0x14(%rbx), %ecx
movl %ecx, 0x14(%rbx)
movq 0x20(%rsp), %r12
movq 0x18(%r12), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0xd3f3c
xorl %ebx, %ebx
movq 0x18(%rsp), %r13
movq 0x50(%rsp), %rbp
movq 0x10(%r12), %rcx
leaq (%rbx,%rbx,4), %rdx
cmpl $0x0, 0xc(%rcx,%rdx,8)
je 0xd3f23
movq 0x20(%rax), %rsi
cmpl $0x0, (%rsi,%rbx,8)
js 0xd3f23
leaq (%rcx,%rdx,8), %r14
cmpl $0x0, 0x10(%r14)
je 0xd3f23
xorl %r15d, %r15d
movq 0x18(%r14), %rax
movq (%rax,%r15,8), %rsi
shrq $0x21, %rsi
movq 0x20(%rbp), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq %r13, %rdi
movl %ebx, %edx
movl $0x1, %ecx
callq 0xa27e9
incq %r15
movl 0x10(%r14), %eax
cmpq %rax, %r15
jb 0xd3eeb
movq 0x18(%r12), %rax
incq %rbx
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0xd3ec3
movq 0x8(%rsp), %rbx
movl 0x14(%rbx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0xd4251
movq 0x20(%rbx), %rax
movl %ecx, %ecx
xorl %edx, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r12
cmpl $0x0, (%rax,%rdx,8)
js 0xd3fe6
movq 0x10(%r12), %rdi
leaq (%rdx,%rdx,4), %r8
leaq (%rdi,%r8,8), %rdi
movl 0x10(%rdi), %r8d
testq %r8, %r8
je 0xd3fda
movq 0x18(%rdi), %r9
xorl %r11d, %r11d
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq 0x18(%rsp), %rbx
movq (%r9,%r11,8), %r14
shrq $0x21, %r14
cmpq %r14, (%rbx)
jbe 0xd3fbe
movq 0x8(%rbx), %r15
movl (%r15,%r14,4), %r14d
cmpq %rsi, %r14
je 0xd3fbe
testl %r14d, %r14d
jns 0xd3fbe
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
testq %r11, %r11
je 0xd3fce
cmpl %r14d, %ebp
jne 0xd3fce
movl %ebp, %r14d
incq %r11
movl %r14d, %ebp
cmpq %r11, %r8
jne 0xd3f8e
jmp 0xd3fdd
movl %r10d, %r15d
movq %r14, (%r9,%r15,8)
incl %r10d
jmp 0xd3fc1
xorl %r10d, %r10d
movl %r10d, 0x10(%rdi)
movq 0x8(%rsp), %rbx
incq %rdx
cmpq %rcx, %rdx
jne 0xd3f5a
movl 0x14(%rbx), %r15d
andq $0x7fffffff, %r15       # imm = 0x7FFFFFFF
je 0xd4251
xorl %r12d, %r12d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x20(%rbx), %rdx
cmpl $0x0, (%rdx,%r12,8)
js 0xd4245
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rcx
leaq (%r12,%r12,4), %rax
leaq (%rcx,%rax,8), %rax
cmpl $0x0, 0x8(%rax)
je 0xd4245
cmpb $0x1, 0x4(%rdx,%r12,8)
je 0xd4245
movq 0x28(%rbx), %rdx
movq %r12, %rsi
shlq $0x4, %rsi
movq (%rdx,%rsi), %r8
movq %r8, %r9
andq %r13, %r9
je 0xd4245
leaq (%r12,%r12), %rdi
movq 0x8(%rbx), %rsi
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (%rsi,%r8,8), %r8
addq $0xc, %r8
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl (%r8,%r10), %r14d
notl %r14d
shrl $0x1f, %r14d
addl %r14d, %r11d
addq $0x18, %r10
cmpq %r10, %r9
jne 0xd4083
cmpl $0x1, %r11d
jne 0xd4245
xorl %r10d, %r10d
cmpl $0x0, (%r8,%r10)
js 0xd40b3
movl -0x4(%r8,%r10), %ebp
addq $0x18, %r10
cmpq %r10, %r9
jne 0xd40a7
movl %ebp, %r8d
xorl $0x1, %r8d
movq (%rdx,%r8,8), %r9
movq %r9, %r10
andq %r13, %r10
je 0xd4245
shrq $0x20, %r9
leaq (%r9,%r9,2), %r9
leaq (%rsi,%r9,8), %r9
addq $0xc, %r9
shlq $0x3, %r10
leaq (%r10,%r10,2), %r10
xorl %r11d, %r11d
xorl %r14d, %r14d
movl (%r9,%r11), %ebx
notl %ebx
shrl $0x1f, %ebx
addl %ebx, %r14d
addq $0x18, %r11
cmpq %r11, %r10
jne 0xd40f1
cmpl $0x1, %r14d
jne 0xd4240
movl %ebp, %r9d
shrl %r9d
leaq (%r9,%r9,4), %r9
cmpl $0x0, 0x8(%rcx,%r9,8)
movq 0x8(%rsp), %rbx
jne 0xd4245
movl %r8d, 0x4c(%rsp)
movq 0x8(%rdx,%rdi,8), %rdi
movq %rdi, %r9
andq %r13, %r9
je 0xd4245
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%rsi,%rdi,8), %rdi
addq $0xc, %rdi
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl (%rdi,%r10), %ebx
notl %ebx
shrl $0x1f, %ebx
addl %ebx, %r11d
addq $0x18, %r10
cmpq %r10, %r9
jne 0xd415f
cmpl $0x1, %r11d
jne 0xd4240
xorl %r10d, %r10d
movq 0x8(%rsp), %rbx
cmpl $0x0, (%rdi,%r10)
js 0xd4192
movl -0x4(%rdi,%r10), %ebp
addq $0x18, %r10
cmpq %r10, %r9
jne 0xd4186
movl %ebp, %edi
xorl $0x1, %edi
movq (%rdx,%rdi,8), %rdx
movq %rdx, %rdi
andq %r13, %rdi
je 0xd4245
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rsi
xorl %edi, %edi
xorl %r9d, %r9d
movl (%rdx,%rdi), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0xd41cd
cmpl $0x1, %r9d
jne 0xd4245
movl %ebp, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
cmpl $0x0, 0x8(%rcx,%rdx,8)
jne 0xd4245
movl %ebp, 0x48(%rsp)
movq 0x18(%rax), %rdx
movl 0x10(%rax), %ecx
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl %ebp, %r9d
movss 0x34(%rsp), %xmm0
pushq 0x28(%rsp)
leaq 0xc8(%rsp), %rax
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0xefcba
addq $0x20, %rsp
jmp 0xd4245
movq 0x8(%rsp), %rbx
incq %r12
cmpq %r15, %r12
jne 0xd400c
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
testq %rax, %rax
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r14
je 0xd4285
movq 0x8(%rcx), %rcx
xorl %edx, %edx
cmpl $-0x2, (%rcx,%rdx,4)
jg 0xd427b
movl $0xffffffff, (%rcx,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xd426e
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0xa0761
movq 0x28(%rsp), %rdi
callq 0x2220
movq 0x108(%rsp), %rdi
callq 0x2220
movq 0x178(%rsp), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
movl 0x14(%rax), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0xd430e
movq 0x20(%rax), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
cmpl $0x0, (%rax)
js 0xd4301
movq 0x10(%rbx), %rsi
cmpl $0x0, 0xc(%rsi,%rdx)
je 0xd4301
movl 0x10(%rsi,%rdx), %edi
movl %edi, 0x4(%rsi,%rdx)
addq $0x8, %rax
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xd42e9
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq