// File: /workspace/github_cpp_makefile_star2_O1/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 0xec35a
addq $0x10, %rsp
testq %r14, %r14
je 0xededd
movq 0x10(%r12), %rdx
movq 0x18(%r12), %rax
movq 0x20(%rax), %rsi
xorl %edi, %edi
xorl %eax, %eax
xorl %ebx, %ebx
movl (%r15,%rdi,8), %r8d
leaq (%r8,%r8,4), %r9
movl 0x10(%rdx,%r9,8), %r9d
addq %r9, %rbx
movl (%rsi,%r8,8), %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
addq %r8, %rax
incq %rdi
movl %edi, %r8d
cmpq %r14, %r8
jb 0xedeb2
jmp 0xedee1
xorl %ebx, %ebx
xorl %eax, %eax
movl 0x8(%rsp), %ecx
movl %ecx, %edx
shrl %edx
leaq (%rdx,%rdx,4), %rsi
movq 0x10(%r12), %rdi
movq 0x18(%r12), %r11
movl 0x10(%rdi,%rsi,8), %esi
movl 0xc(%rsp), %r8d
movl %r8d, %r9d
shrl %r9d
leaq (%r9,%r9,4), %r10
movl 0x10(%rdi,%r10,8), %edi
leal (%rsi,%rdi), %r10d
incl %r10d
addq %rbx, %r10
movq 0x20(%r11), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%rdi,%rdx,8), %edx
andl %esi, %edx
andl (%rdi,%r9,8), %esi
addl %edx, %esi
addq %rax, %rsi
addq %r10, %r10
leaq 0x2(,%rsi,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 0x102076
addq $0x20, %rsp
movl %eax, %ebx
testl %eax, %eax
jne 0xedfb7
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 0xedb37
addq $0x30, %rsp
movl %eax, %ebx
cmpl $0x1, %ebx
jne 0xee0e5
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 0x10109f
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 0x10109f
addq $0x20, %rsp
testq %r14, %r14
je 0xee085
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 0xee064
cmpb $0x1, 0x4(%rax,%rsi,8)
jne 0xee074
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 0xee053
cvtsi2sd %rdx, %xmm0
jmp 0xee089
xorpd %xmm0, %xmm0
movq 0x38(%rsp), %rax
addq 0x40(%rsp), %rax
cvtsi2sd %rax, %xmm1
mulsd 0x1373e8(%rip), %xmm1   # 0x225488
ucomisd %xmm0, %xmm1
jb 0xee0e5
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 0xece7c
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 0xec2b9
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq