// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movss %xmm0, 0x10(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, 0x20(%rsp)
movq %rdi, %r12
movl 0x98(%rsp), %ebp
movq 0x90(%rsp), %rdi
movq 0x18(%r12), %r13
andq $0x0, (%rdi)
leaq 0x14(%rsp), %rax
leaq 0x18(%rsp), %r10
movq %r13, %rsi
movl %r8d, 0x8(%rsp)
movl %r9d, 0xc(%rsp)
pushq %rax
pushq %r10
callq 0xaee2b
popq %rax
popq %rcx
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %eax, %eax
movl %ecx, %ecx
cmpq %r14, %rcx
jae 0xb02ba
movl (%r15,%rcx,8), %edx
imulq $0x28, %rdx, %rsi
movq 0x10(%r12), %rdi
movq 0x18(%r12), %r8
movl 0x10(%rdi,%rsi), %esi
addq %rsi, %rbx
movq 0x20(%r8), %rsi
movl (%rsi,%rdx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %rax
incl %ecx
jmp 0xb0286
movl 0x8(%rsp), %ecx
movl %ecx, %r11d
shrl %r11d
imulq $0x28, %r11, %rdx
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdi
movl 0x10(%rsi,%rdx), %edx
movl 0xc(%rsp), %r8d
movl %r8d, %r10d
shrl %r10d
imulq $0x28, %r10, %r9
movl 0x10(%rsi,%r9), %esi
addl %esi, %edx
incl %edx
addq %rdx, %rbx
movq 0x20(%rdi), %rdx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl (%rdx,%r11,8), %edi
andl %esi, %edi
andl (%rdx,%r10,8), %esi
addl %edi, %esi
addq %rax, %rsi
addq %rbx, %rbx
movq %rbp, %rax
leaq 0x2(,%rsi,2), %rbp
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0xa0(%rsp), %r9
pushq %rbp
pushq %rbx
pushq %rax
pushq 0xc0(%rsp)
callq 0xc3e2a
addq $0x20, %rsp
testl %eax, %eax
jne 0xb0383
movq %r12, %rdi
movq 0x90(%rsp), %rsi
movss 0x10(%rsp), %xmm0
movq %r15, %rdx
movq %r14, %rcx
movl 0x8(%rsp), %r8d
movl 0xc(%rsp), %r9d
pushq $0x30
pushq %rbp
pushq %rbx
movl 0xb0(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
callq 0xaffc0
addq $0x30, %rsp
pushq $0x1
popq %rbp
cmpl $0x1, %eax
jne 0xb049b
movq 0x20(%rsp), %rbp
movq 0x18(%rbp), %rdi
movl 0x8(%rsp), %edx
pushq $0x1
popq %rax
xorl %eax, %edx
leaq 0x38(%rsp), %r11
leaq 0x40(%rsp), %r10
leaq 0x1c(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x48(%rsp), %r9
movq %rbp, %rsi
movl %edx, 0x10(%rsp)
pushq $0x0
pushq $0x1
popq %rax
pushq %rax
pushq %r11
pushq %r10
callq 0xc2ebf
addq $0x20, %rsp
movq 0x18(%rbp), %rdi
leaq 0x28(%rsp), %r8
movq %rbp, %rsi
pushq $0x1
popq %rbp
movl 0xc(%rsp), %edx
leaq 0x1c(%rsp), %rcx
leaq 0x48(%rsp), %r9
pushq $0x0
pushq %rbp
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
xorl %eax, %eax
xorl %ecx, %ecx
movl %eax, %eax
cmpq %r14, %rax
jae 0xb043d
movl (%r15,%rax,8), %edx
movq 0x20(%r13), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xb0429
cmpb $0x1, 0x4(%rsi,%rdx,8)
jne 0xb0439
movq 0x10(%r12), %rsi
imulq $0x28, %rdx, %rdx
movl 0x10(%rsi,%rdx), %edx
addq %rdx, %rcx
incl %eax
jmp 0xb040d
cvtsi2sd %rcx, %xmm0
movq 0x28(%rsp), %rax
addq 0x30(%rsp), %rax
cvtsi2sd %rax, %xmm1
mulsd 0x13307f(%rip), %xmm1   # 0x1e34d8
xorl %eax, %eax
ucomisd %xmm0, %xmm1
jb 0xb049b
leaq 0x54(%rsp), %rcx
movl 0x10(%rsp), %eax
movl %eax, (%rcx)
subq $0x8, %rsp
pushq $0x1
popq %r8
movq %r13, %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
movq %rbx, %r9
pushq %r12
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
callq 0xaf676
addq $0x20, %rsp
movl 0x18(%rsp), %r10d
movl 0x14(%rsp), %r11d
subl %eax, %ebp
subq $0x8, %rsp
movq %r13, %rdi
movq 0x98(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movl 0x10(%rsp), %r8d
movl 0x14(%rsp), %r9d
pushq %rbp
pushq %r11
pushq %r10
callq 0xaed99
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq