// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: unroll_tangle(ma_ug_t*, ma_ug_t*, unsigned long*, unsigned long, unsigned int, unsigned int, kvec_t_u64_warp*, unsigned int, buf_t*, unsigned char*, float)
void unroll_tangle(ma_ug_t *ug, ma_ug_t *length_ug, uint64_t* nodes, uint64_t nodes_n, 
uint32_t beg, uint32_t end, kvec_t_u64_warp* edges, uint32_t trio_flag, buf_t* bb, uint8_t* visit,
float drop_ratio)
{
    uint32_t v, w, is_found = 0, convex, i, beg_c, end_c;
    long long tmp, max_stop_nodeLen, max_stop_baseLen, begLen, endLen, missLen;
    long long totalNodeLen = 0, totalBaseLen = 0;
    asg_t* nsg = ug->g;
    edges->a.n = 0;
    save_all_edges(edges, nsg, nodes, nodes_n, beg, end, &beg_c, &end_c);


    for (i = 0; i < nodes_n; i++)
    {
        v = (uint32_t)nodes[i];
        totalNodeLen += ug->u.a[v].n;
        totalBaseLen += ug->g->seq[v].len;
    }
    totalNodeLen += ug->u.a[beg>>1].n + ug->u.a[end>>1].n + 1;
    totalBaseLen += ug->g->seq[beg>>1].len + ug->g->seq[end>>1].len + 1;
    ///each might be visited twice
    totalNodeLen = totalNodeLen * 2;
    totalBaseLen = totalBaseLen * 2;

    /**************************debug**************************/
    // uint32_t debug_is_access = 0, debug_is_circle = 0;
    // check_connective(nsg, beg, end, totalNodeLen, bb, visit, ug, nodes, nodes_n, &debug_is_access, 
    // &debug_is_circle);
    // if(debug_is_access==0) fprintf(stderr, "Cannot approch end: beg: %u, end: %u\n", beg>>1, end>>1);
    // if(debug_is_circle==0) fprintf(stderr, "Not circle: beg: %u, end: %u\n", beg>>1, end>>1);
    // if(debug_is_circle==1) fprintf(stderr, "Circle: beg: %u, end: %u\n", beg>>1, end>>1);
    /**************************debug**************************/
    is_found = process_tangles(ug, nodes, nodes_n, beg, end, bb, visit, trio_flag, totalNodeLen, 
    totalBaseLen);
    ///if(is_found == 1) fprintf(stderr, "***Found: beg>>1: %u, end>>1: %u\n", beg>>1, end>>1);

    if(is_found == 0)
    {
        is_found = cut_edges_progressive(ug, edges, drop_ratio, nodes, nodes_n, 
        beg, end, bb, visit, trio_flag, totalNodeLen, totalBaseLen, 48);

        ///if(is_found == 1) fprintf(stderr, "***Cutting Found: beg>>1: %u, end>>1: %u\n", beg>>1, end>>1);
    }

    if(is_found == 1)
    {
        get_unitig(length_ug->g, length_ug, beg^1, &convex, &begLen, &tmp, &max_stop_nodeLen, 
                        &max_stop_baseLen, 1, NULL);

        get_unitig(length_ug->g, length_ug, end, &convex, &endLen, &tmp, &max_stop_nodeLen, 
                        &max_stop_baseLen, 1, NULL);


        missLen = 0;
        for (i = 0; i < nodes_n; i++)
        {
            v = (uint32_t)nodes[i];
            if(nsg->seq[v].del || nsg->seq[v].c == ALTER_LABLE)
            {
                missLen += ug->u.a[v].n;
            }
        }
        ///resolved!
        if(missLen <= (begLen+endLen)*TANGLE_MISSED_THRES)
        {
            is_found = 0;
            w = beg^1;
            is_found = find_spec_node(nsg, beg, end, &w, 1, totalNodeLen, bb, visit, ug);
        }
        else
        {
            is_found = 0;
        }        
    }

    recover_edges(nsg, edges, nodes, nodes_n, beg, end, beg_c, end_c, 1-is_found);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movss %xmm0, 0x18(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x30(%rsp)
movq %rdi, %r12
movq 0xa0(%rsp), %rdi
movq 0x18(%r12), %rsi
movq $0x0, (%rdi)
leaq 0x1c(%rsp), %rax
leaq 0x20(%rsp), %r10
movq %rsi, 0x10(%rsp)
movl %r8d, 0x8(%rsp)
movl %r9d, 0xc(%rsp)
pushq %rax
pushq %r10
callq 0xedc5e
addq $0x10, %rsp
movq 0x10(%r12), %rdx
movq 0x18(%r12), %rax
movq 0x20(%rax), %rax
testq %r14, %r14
je 0xefd59
xorl %edi, %edi
xorl %ebx, %ebx
xorl %esi, %esi
movl (%r15,%rdi,8), %r8d
leaq (%r8,%r8,4), %r9
movl 0x10(%rdx,%r9,8), %r9d
addq %r9, %rsi
movl (%rax,%r8,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r8, %rbx
incq %rdi
movl %edi, %r8d
cmpq %r14, %r8
jb 0xefd2e
jmp 0xefd5d
xorl %esi, %esi
xorl %ebx, %ebx
movl 0x8(%rsp), %ecx
movl %ecx, %edi
shrl %edi
leaq (%rdi,%rdi,4), %r8
movl 0x10(%rdx,%r8,8), %r11d
movl 0xc(%rsp), %r8d
movl %r8d, %r9d
shrl %r9d
leaq (%r9,%r9,4), %r10
movl 0x10(%rdx,%r10,8), %edx
leal (%r11,%rdx), %r10d
incl %r10d
addq %rsi, %r10
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl (%rax,%rdi,8), %esi
andl %edx, %esi
andl (%rax,%r9,8), %edx
addl %esi, %edx
addq %rbx, %rdx
addq %r10, %r10
leaq 0x2(,%rdx,2), %rbp
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0xb0(%rsp), %r9
pushq %rbp
movq %r10, 0x30(%rsp)
pushq %r10
movl 0xb8(%rsp), %r13d
pushq %r13
pushq 0xd0(%rsp)
callq 0x106399
addq $0x20, %rsp
movl %eax, %ebx
testl %eax, %eax
jne 0xefe26
movq %r12, %rdi
movq 0xa0(%rsp), %rsi
movss 0x18(%rsp), %xmm0
movq %r15, %rdx
movq %r14, %rcx
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
pushq $0x30
pushq %rbp
pushq 0x38(%rsp)
pushq %r13
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0xef90c
addq $0x30, %rsp
movl %eax, %ebx
cmpl $0x1, %ebx
jne 0xeff54
movq 0x30(%rsp), %rbp
movq 0x18(%rbp), %rdi
movl $0x1, %eax
movl 0x8(%rsp), %r13d
xorl %eax, %r13d
movl $0x1, %eax
xorl %ebx, %ebx
leaq 0x48(%rsp), %r11
leaq 0x50(%rsp), %r10
leaq 0x24(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x58(%rsp), %r9
movq %rbp, %rsi
movl %r13d, %edx
pushq %rbx
pushq %rax
pushq %r11
pushq %r10
callq 0x1053a7
addq $0x20, %rsp
movq 0x18(%rbp), %rdi
leaq 0x38(%rsp), %r8
movq %rbp, %rsi
movl 0xc(%rsp), %edx
leaq 0x24(%rsp), %rcx
leaq 0x58(%rsp), %r9
pushq %rbx
movl $0x1, %eax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
testq %r14, %r14
je 0xefef4
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl (%r15,%rcx,8), %esi
cmpl $0x0, (%rax,%rsi,8)
js 0xefed3
cmpb $0x1, 0x4(%rax,%rsi,8)
jne 0xefee3
movq 0x10(%r12), %rdi
leaq (%rsi,%rsi,4), %rsi
movl 0x10(%rdi,%rsi,8), %esi
addq %rsi, %rdx
incq %rcx
movl %ecx, %esi
cmpq %r14, %rsi
jb 0xefec2
cvtsi2sd %rdx, %xmm0
jmp 0xefef8
xorpd %xmm0, %xmm0
movq 0x38(%rsp), %rax
addq 0x40(%rsp), %rax
cvtsi2sd %rax, %xmm1
mulsd 0x141679(%rip), %xmm1   # 0x231588
ucomisd %xmm0, %xmm1
jb 0xeff54
leaq 0x64(%rsp), %rcx
movl %r13d, (%rcx)
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movl $0x1, %r8d
movq 0x30(%rsp), %r9
pushq %r12
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
callq 0xee7a2
addq $0x20, %rsp
movl %eax, %ebx
movl 0x20(%rsp), %eax
movl 0x1c(%rsp), %r10d
movl $0x1, %r11d
subl %ebx, %r11d
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movl 0x10(%rsp), %r8d
movl 0x14(%rsp), %r9d
pushq %r11
pushq %r10
pushq %rax
callq 0xedbc0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq