// File: /workspace/github_cpp_makefile_star2_O2/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, 0x44(%rsp)
movq %r9, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm2, %xmm2
movaps %xmm2, 0x130(%rsp)
movaps %xmm2, 0x140(%rsp)
movaps %xmm2, 0x150(%rsp)
movaps %xmm2, 0x160(%rsp)
movaps %xmm2, 0x170(%rsp)
movaps %xmm2, 0x180(%rsp)
andq $0x0, 0x190(%rsp)
movaps %xmm2, 0xc0(%rsp)
movaps %xmm2, 0xd0(%rsp)
movaps %xmm2, 0xe0(%rsp)
movaps %xmm2, 0xf0(%rsp)
movaps %xmm2, 0x100(%rsp)
movaps %xmm2, 0x110(%rsp)
andq $0x0, 0x120(%rsp)
orl $-0x1, 0x50(%rsp)
movaps %xmm2, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movaps %xmm2, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movq 0x18(%rdi), %rax
movl 0x14(%rax), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
pushq $0x2
popq %r11
xorl %esi, %esi
cmpq %rsi, %r10
je 0x975ed
movq 0x20(%rax), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0x975e4
movq 0x10(%rbx), %rdi
cmpl $0x0, -0x4(%rdi,%r11,8)
je 0x975e4
movl (%rdi,%r11,8), %r9d
movl %r9d, -0xc(%rdi,%r11,8)
incq %rsi
addq $0x5, %r11
jmp 0x975c0
movq %rdx, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movq %r8, 0x78(%rsp)
movss %xmm0, 0x48(%rsp)
movss %xmm1, 0x4c(%rsp)
movq %rbx, %rdi
callq 0x8c99a
movq 0x18(%rax), %r8
movl 0x14(%r8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
imulq $0x28, %rcx, %rdi
xorl %ecx, %ecx
cmpq %rcx, %rdi
je 0x97640
movq 0x10(%rax), %rdx
movl 0x10(%rdx,%rcx), %esi
movl %esi, 0x4(%rdx,%rcx)
andl $0x0, 0x8(%rdx,%rcx)
addq $0x28, %rcx
jmp 0x97624
movq %r15, 0x58(%rsp)
movq %rax, 0x18(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x28(%rsp)
movq %r8, 0x8(%rsp)
movl 0x14(%r8), %ebx
movl %ebx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
callq 0x2280
addl %ebx, %ebx
xorl %r11d, %r11d
leaq 0x50(%rsp), %r10
leaq 0x54(%rsp), %r15
leaq 0x24(%rsp), %rbp
leaq 0xa0(%rsp), %r13
leaq 0xc0(%rsp), %r14
xorl %r12d, %r12d
movq %rax, 0x30(%rsp)
movq %rbx, 0x60(%rsp)
cmpl %ebx, %r11d
jne 0x976b6
movl $0x0, %r11d
testl %r12d, %r12d
movl $0x0, %r12d
jne 0x9769b
jmp 0x9774e
subq $0x8, %rsp
movq 0x60(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq 0x80(%rsp), %r8
movss 0x50(%rsp), %xmm0
movss 0x54(%rsp), %xmm1
leaq 0x138(%rsp), %r9
pushq 0x1d8(%rsp)
pushq 0x48(%rsp)
pushq %r10
pushq %r15
pushq %rbp
pushq %r11
pushq %rax
pushq %r13
pushq %r14
movq %r13, %r14
movq %rbp, %r13
movq %r10, %rbp
movq %r15, %rbx
movq %r11, %r15
callq 0xc3f37
movq %r15, %r11
movq %rbx, %r15
movq %rbp, %r10
movq %r13, %rbp
movq %r14, %r13
leaq 0x110(%rsp), %r14
addq $0x50, %rsp
addl %eax, %r12d
cmpl $0x2, %eax
movq 0x30(%rsp), %rax
je 0x976b6
incl %r11d
movq 0x60(%rsp), %rbx
jmp 0x9769b
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
xorl %r15d, %r15d
pushq $0x1
popq %rbp
movq 0x28(%rsp), %r12
movq 0x38(%rsp), %r13
movq 0x18(%r12), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %r15
jae 0x977e3
movq 0x10(%r12), %rbx
imulq $0x28, %r15, %rcx
cmpl $0x0, 0xc(%rbx,%rcx)
je 0x977de
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%r15,8)
js 0x977de
addq %rcx, %rbx
xorl %r14d, %r14d
movl 0x10(%rbx), %eax
cmpq %rax, %r14
jae 0x977de
movq 0x18(%rbx), %rax
movq (%rax,%r14,8), %rsi
shrq $0x21, %rsi
movq 0x58(%rsp), %rax
movq 0x20(%rax), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq %r13, %rdi
movl %r15d, %edx
movl %ebp, %ecx
callq 0x6ab56
incq %r14
jmp 0x977a7
incq %r15
jmp 0x97773
movq 0x8(%rsp), %rdi
movl 0x14(%rdi), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ecx, %ecx
pushq $-0x1
popq %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %r12
cmpq %rax, %rcx
je 0x9788e
movq 0x20(%rdi), %r8
cmpl $0x0, (%r8,%rcx,8)
js 0x97886
movq 0x10(%r12), %r8
imulq $0x28, %rcx, %r9
leaq (%r8,%r9), %rdi
movl 0x10(%r8,%r9), %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
movl %edx, %r11d
cmpq %r9, %r8
je 0x9787d
movq 0x18(%rdi), %r14
movq (%r14,%r9,8), %rbx
shrq $0x21, %rbx
cmpq %rbx, (%r13)
jbe 0x97866
movq 0x8(%r13), %r15
movl (%r15,%rbx,4), %ebx
cmpq %rsi, %rbx
je 0x97866
testl %ebx, %ebx
jns 0x97866
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
testq %r9, %r9
je 0x97871
cmpl %ebx, %r11d
jne 0x97871
movl %r11d, %ebx
incq %r9
movl %ebx, %r11d
jmp 0x9782e
movl %r10d, %r11d
movq %rbx, (%r14,%r11,8)
incl %r10d
jmp 0x97869
movl %r10d, 0x10(%rdi)
movq 0x8(%rsp), %rdi
incq %rcx
jmp 0x977ff
movl 0x14(%rdi), %r12d
addl %r12d, %r12d
pushq $0x6
popq %rbp
xorl %ebx, %ebx
leaq 0x14(%rsp), %r14
cmpq %rbx, %r12
je 0x97a02
movq 0x20(%rdi), %rax
cmpl $0x0, (%rax,%rbx,4)
js 0x979f0
movq 0x18(%rsp), %rcx
movq 0x10(%rcx), %r15
cmpl $0x0, -0x10(%r15,%rbp,4)
je 0x979f0
cmpb $0x1, 0x4(%rax,%rbx,4)
je 0x979f0
movl %ebx, %esi
xorl %edx, %edx
callq 0xc2e7a
movq 0x8(%rsp), %rdi
cmpl $0x1, %eax
jne 0x979f0
movl %ebx, %esi
movq %r14, %rdx
callq 0xc2e7a
movl 0x14(%rsp), %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xc2e7a
movq 0x8(%rsp), %rdi
cmpl $0x1, %eax
jne 0x979f0
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rcx
movl 0x14(%rsp), %eax
movl %eax, %edx
shrl %edx
imulq $0x28, %rdx, %rdx
cmpl $0x0, 0x8(%rcx,%rdx)
jne 0x979f0
xorl $0x1, %eax
movl %eax, 0x24(%rsp)
leaq 0x1(%rbx), %r13
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
movq 0x8(%rsp), %rdi
cmpl $0x1, %eax
jne 0x979f0
movl %r13d, %esi
movq %r14, %rdx
callq 0xc2e7a
movl 0x14(%rsp), %esi
xorl $0x1, %esi
movq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0xc2e7a
movq 0x8(%rsp), %rdi
cmpl $0x1, %eax
jne 0x979f0
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x14(%rsp), %r9d
movl %r9d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
cmpl $0x0, 0x8(%rax,%rcx)
jne 0x979f0
movl %r9d, 0x54(%rsp)
movq (%r15,%rbp,4), %rdx
movl -0x8(%r15,%rbp,4), %ecx
movl 0x24(%rsp), %r8d
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
movss 0x44(%rsp), %xmm0
pushq 0x30(%rsp)
leaq 0x138(%rsp), %rax
pushq %rax
movl 0x1e8(%rsp), %eax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
callq 0xb0225
movq 0x28(%rsp), %rdi
addq $0x20, %rsp
addq $0x2, %rbx
addq $0xa, %rbp
movq 0x38(%rsp), %r13
jmp 0x9789f
movq (%r13), %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %rbx
movq 0x18(%rsp), %r14
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x97a2b
movq 0x8(%r13), %rdx
cmpl $-0x2, (%rdx,%rcx,4)
jg 0x97a27
orl $-0x1, (%rdx,%rcx,4)
incl %ecx
jmp 0x97a12
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x6921d
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x178(%rsp), %rdi
callq 0x2220
movq 0x108(%rsp), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
pushq $0x2
popq %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x97aaf
movq 0x20(%rax), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0x97aa6
movq 0x10(%rbx), %rdi
cmpl $0x0, -0x4(%rdi,%rdx,8)
je 0x97aa6
movl (%rdi,%rdx,8), %r8d
movl %r8d, -0xc(%rdi,%rdx,8)
incq %rsi
addq $0x5, %rdx
jmp 0x97a83
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq