// File: /workspace/github_cpp_makefile_star2_O1/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)
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), %r10d
andq $0x7fffffff, %r10       # imm = 0x7FFFFFFF
je 0xd2890
movq 0x20(%rax), %rax
shlq $0x3, %r10
leaq (%r10,%r10,4), %rbx
xorl %r14d, %r14d
cmpl $0x0, (%rax)
js 0xd2883
movq 0x10(%rdi), %r10
cmpl $0x0, 0xc(%r10,%r14)
je 0xd2883
movl 0x10(%r10,%r14), %r11d
movl %r11d, 0x4(%r10,%r14)
addq $0x8, %rax
addq $0x28, %r14
cmpq %r14, %rbx
jne 0xd2868
movq %rdx, 0x60(%rsp)
movq %rcx, 0x68(%rsp)
movq %r8, 0x70(%rsp)
movss %xmm0, 0x38(%rsp)
movss %xmm1, 0x3c(%rsp)
movq %rsi, 0x58(%rsp)
movq %r9, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
callq 0xc815c
movq 0x18(%rax), %r8
movl 0x14(%r8), %ecx
andq $0x7fffffff, %rcx       # imm = 0x7FFFFFFF
je 0xd28f7
movq 0x10(%rax), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
movl 0x10(%rdi,%rdx), %esi
movl %esi, 0x4(%rdi,%rdx)
movl $0x0, 0x8(%rdi,%rdx)
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xd28de
movq %rax, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movl 0x14(%r8), %ebx
movl %ebx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
callq 0x2280
addl %ebx, %ebx
movq %rbx, 0x50(%rsp)
movq %rax, 0x28(%rsp)
je 0xd2a0a
movq 0x1d0(%rsp), %r10
xorl %ecx, %ecx
leaq 0x44(%rsp), %r11
leaq 0x48(%rsp), %r14
leaq 0x4c(%rsp), %r15
leaq 0xa0(%rsp), %r12
leaq 0x130(%rsp), %rbp
xorl %r13d, %r13d
movq %r13, 0x78(%rsp)
movl %ecx, 0x40(%rsp)
subq $0x8, %rsp
movq 0x60(%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 %r13
pushq %rax
pushq %r12
pushq %rbp
movq %r15, %r13
movq %r10, %r15
movq %r12, %rbx
movq %r14, %r12
movq %r11, %r14
callq 0x102235
movl 0x90(%rsp), %ecx
movq %r14, %r11
movq %r12, %r14
movq %rbx, %r12
movq %r15, %r10
movq %r13, %r15
movq 0xc8(%rsp), %r13
addq $0x50, %rsp
addl %eax, %ecx
cmpl $0x2, %eax
movq 0x28(%rsp), %rax
je 0xd2955
incl %r13d
cmpl 0x50(%rsp), %r13d
jne 0xd2950
testl %ecx, %ecx
je 0xd2a0a
movl $0x0, %ecx
movl $0x0, %r13d
cmpl $0x0, 0x50(%rsp)
jne 0xd2950
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq 0x20(%rsp), %r12
movq 0x18(%r12), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
movq 0x18(%rsp), %r13
movq 0x58(%rsp), %rbp
je 0xd2abb
xorl %ebx, %ebx
movq 0x10(%r12), %rcx
leaq (%rbx,%rbx,4), %rdx
cmpl $0x0, 0xc(%rcx,%rdx,8)
je 0xd2aa5
movq 0x20(%rax), %rax
cmpl $0x0, (%rax,%rbx,8)
js 0xd2aa5
leaq (%rcx,%rdx,8), %r14
cmpl $0x0, 0x10(%r14)
je 0xd2aa5
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 0xa1cd4
incq %r15
movl 0x10(%r14), %eax
cmpq %rax, %r15
jb 0xd2a72
incq %rbx
movq 0x18(%r12), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0xd2a4a
movq 0x8(%rsp), %r15
movl 0x14(%r15), %eax
andq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
movq 0x10(%rsp), %r12
je 0xd2b9a
movq 0x20(%r15), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xd2b8e
movq 0x10(%r12), %rsi
leaq (%rdx,%rdx,4), %rdi
leaq (%rsi,%rdi,8), %rsi
movl 0x10(%rsi), %edi
testq %rdi, %rdi
je 0xd2b82
movq 0x18(%rsi), %r8
xorl %r10d, %r10d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %r9d, %r9d
movq 0x18(%rsp), %r15
movq (%r8,%r10,8), %rbx
shrq $0x21, %rbx
movb $0x1, %bpl
cmpq %rbx, (%r15)
jbe 0xd2b48
movq 0x8(%r15), %r14
movl (%r14,%rbx,4), %r14d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %r14d
je 0xd2b4d
movl %r14d, %ebx
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
testl %r14d, %r14d
setns %bpl
jmp 0xd2b4d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ebx
sete %r14b
orb %bpl, %r14b
jne 0xd2b63
testq %r10, %r10
je 0xd2b68
cmpl %ebx, %r11d
jne 0xd2b68
movl %r11d, %ebx
jmp 0xd2b75
movl %ebx, %r11d
movl %r9d, %r14d
movq %r11, (%r8,%r14,8)
incl %r9d
incq %r10
movl %ebx, %r11d
cmpq %r10, %rdi
jne 0xd2b13
jmp 0xd2b85
xorl %r9d, %r9d
movl %r9d, 0x10(%rsi)
movq 0x8(%rsp), %r15
incq %rdx
cmpq %rax, %rdx
jne 0xd2adb
movl 0x14(%r15), %ebx
andq $0x7fffffff, %rbx       # imm = 0x7FFFFFFF
je 0xd2e3a
xorl %r12d, %r12d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x20(%r15), %rdx
cmpl $0x0, (%rdx,%r12,8)
js 0xd2d7c
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rcx
leaq (%r12,%r12,4), %rax
leaq (%rcx,%rax,8), %rax
cmpl $0x0, 0x8(%rax)
je 0xd2d7c
cmpb $0x1, 0x4(%rdx,%r12,8)
je 0xd2d7c
movq 0x28(%r15), %rdx
movq %r12, %rsi
shlq $0x4, %rsi
movq (%rdx,%rsi), %rdi
movq %rdi, %r8
andq %r13, %r8
je 0xd2d7c
leaq (%r12,%r12), %rsi
movq 0x8(%r15), %r10
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %r9
leaq (%r10,%r9,8), %r10
addq $0xc, %r10
leaq (,%r8,8), %rdi
leaq (%rdi,%rdi,2), %rdi
xorl %r11d, %r11d
xorl %r14d, %r14d
movl (%r10,%r11), %r15d
notl %r15d
shrl $0x1f, %r15d
addl %r15d, %r14d
addq $0x18, %r11
cmpq %r11, %rdi
jne 0xd2c2f
cmpl $0x1, %r14d
jne 0xd2d77
testq %r8, %r8
je 0xd2c80
movq 0x8(%rsp), %r15
movq 0x8(%r15), %r8
leaq (%r8,%r9,8), %r8
addq $0xc, %r8
xorl %r9d, %r9d
cmpl $0x0, (%r8,%r9)
js 0xd2c75
movl -0x4(%r8,%r9), %ebp
addq $0x18, %r9
cmpq %r9, %rdi
jne 0xd2c69
jmp 0xd2c85
movq 0x8(%rsp), %r15
movl %ebp, %r8d
xorl $0x1, %r8d
movq (%rdx,%r8,8), %rdi
movq %rdi, %r9
andq %r13, %r9
je 0xd2d7c
movq 0x8(%r15), %r10
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%r10,%rdi,8), %rdi
addq $0xc, %rdi
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %r10d, %r10d
xorl %r11d, %r11d
movl (%rdi,%r10), %r14d
notl %r14d
shrl $0x1f, %r14d
addl %r14d, %r11d
addq $0x18, %r10
cmpq %r10, %r9
jne 0xd2cbe
cmpl $0x1, %r11d
jne 0xd2d7c
movl %ebp, %edi
shrl %edi
leaq (%rdi,%rdi,4), %rdi
cmpl $0x0, 0x8(%rcx,%rdi,8)
jne 0xd2d7c
movl %r8d, 0x4c(%rsp)
movq 0x8(%rdx,%rsi,8), %rsi
movq %rsi, %rdi
andq %r13, %rdi
je 0xd2d7c
movq 0x8(%r15), %r10
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r9
leaq (%r10,%r9,8), %r10
addq $0xc, %r10
leaq (,%rdi,8), %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %r11d, %r11d
xorl %r14d, %r14d
movl (%r10,%r11), %r15d
notl %r15d
shrl $0x1f, %r15d
addl %r15d, %r14d
addq $0x18, %r11
cmpq %r11, %rsi
jne 0xd2d2a
cmpl $0x1, %r14d
jne 0xd2d77
testq %rdi, %rdi
je 0xd2d8d
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rdi
leaq (%rdi,%r9,8), %rdi
addq $0xc, %rdi
xorl %r9d, %r9d
cmpl $0x0, (%rdi,%r9)
js 0xd2d6c
movl -0x4(%rdi,%r9), %ebp
addq $0x18, %r9
cmpq %r9, %rsi
jne 0xd2d60
jmp 0xd2d92
movq 0x8(%rsp), %r15
incq %r12
cmpq %rbx, %r12
jne 0xd2bb4
jmp 0xd2e3a
movq 0x8(%rsp), %r15
movl %ebp, %esi
xorl $0x1, %esi
movq (%rdx,%rsi,8), %rdx
movq %rdx, %rsi
andq %r13, %rsi
je 0xd2d7c
movq 0x8(%r15), %rdi
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rdi,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,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 0xd2dc4
cmpl $0x1, %r9d
jne 0xd2d7c
movl %ebp, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rdx
cmpl $0x0, 0x8(%rcx,%rdx,8)
jne 0xd2d7c
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 0xede3e
addq $0x20, %rsp
jmp 0xd2d7c
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
testq %rax, %rax
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r14
je 0xd2e72
movq 0x8(%rdi), %rcx
xorl %edx, %edx
cmpl $-0x2, (%rcx,%rdx,4)
jg 0xd2e68
movq 0x8(%rdi), %rsi
movl $0xffffffff, (%rsi,%rdx,4) # imm = 0xFFFFFFFF
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xd2e57
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0xa0004
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 0xd2efb
movq 0x20(%rax), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %edx, %edx
cmpl $0x0, (%rax)
js 0xd2eee
movq 0x10(%rbx), %rsi
cmpl $0x0, 0xc(%rsi,%rdx)
je 0xd2eee
movl 0x10(%rsi,%rdx), %edi
movl %edi, 0x4(%rsi,%rdx)
addq $0x8, %rax
addq $0x28, %rdx
cmpq %rdx, %rcx
jne 0xd2ed6
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq