// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_no_coverage_reads_chain(ma_hit_t*, ma_hit_t_alloc*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, ma_ug_t*, asg_t*, int, int, unsigned int, unsigned int, kvec_t_u32_warp*, kvec_asg_arc_t_warp*, unsigned int*, unsigned int*, unsigned int*, unsigned int)
int get_no_coverage_reads_chain(ma_hit_t *h, ma_hit_t_alloc* sources, 
ma_hit_t_alloc* reverse_sources, ma_sub_t *coverage_cut, R_to_U* ruIndex, ma_ug_t *ug, 
asg_t *r_g, int max_hang, int min_ovlp, uint32_t endRid, uint32_t uId, 
kvec_t_u32_warp* chain_buffer, kvec_asg_arc_t_warp* chain_edges, uint32_t* return_ava_cur, 
uint32_t* return_ava_ol, uint32_t* return_chainLen, uint32_t thresLen)
{
    (*return_chainLen) = (*return_ava_cur) = (*return_ava_ol) = (uint32_t)-1;
    uint32_t init_contain_uId = (uint32_t)-1, contain_uId, is_Unitig;
    uint32_t chainLen = 0, ava_cur, test_oLen;
    int ql, tl;
    int32_t r;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    asg_arc_t t;
    asg_t* nsg = ug->g;
    chain_buffer->a.n = 0;
    kv_push(uint32_t, chain_buffer->a, uId);
    if(chain_edges) chain_edges->a.n = 0;

    if(!h) return 0;

    while(h)
    {
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);
        ///sq and st cannot be deleted
        ///for h, deleted or not does not matter
        if(sq->del || st->del) return 0;
        if(r_g->seq[Get_qn(*h)].del || r_g->seq[Get_tn(*h)].del) return 0;
        get_R_to_U(ruIndex, Get_tn(*h), &contain_uId, &is_Unitig);
        if(contain_uId == (uint32_t)-1 || is_Unitig != 1) return 0;
        if(nsg->seq[contain_uId].del) return 0;
        ql = sq->e - sq->s; tl = st->e - st->s;
        r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);
        if(r < 0) return 0;
        if((t.ul>>32) != endRid) return 0;
        if(chain_edges) kv_push(asg_arc_t, chain_edges->a, t);
        chainLen++;
        kv_push(uint32_t, chain_buffer->a, contain_uId);
        ava_cur = get_num_edges2existing_nodes_advance(ug, r_g, reverse_sources, coverage_cut, 
                ruIndex, max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, 1);
        //means find an aim
        if(ava_cur > 0)
        {
            (*return_ava_cur) = ava_cur;
            (*return_ava_ol) = test_oLen;
            (*return_chainLen) = chainLen;
            h = NULL;
            return 1;
        }
        

        if(chainLen >= thresLen) break;

        ///endRid is (t.ul>>32), and t.v is a contained read
        ///haven't found a existing unitig from t.v
        ///check if t.v can link to a new contained read
        endRid = t.v; 
        h = get_best_no_coverage_read(ug, r_g, sources, coverage_cut, ruIndex, 
            max_hang, min_ovlp, endRid, init_contain_uId);
    }
    
    

    ///continue
    ///need to update h, endRid, chainLen, chain_buffer and chain_edges
    while (h)
    {
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);

        ///sq, st, and h cannot be deleted
        if(sq->del || st->del || h->del) break;
        if(r_g->seq[Get_qn(*h)].del || r_g->seq[Get_tn(*h)].del) break;

        get_R_to_U(ruIndex, Get_tn(*h), &contain_uId, &is_Unitig);
        if(contain_uId == (uint32_t)-1 || is_Unitig != 1) break;
        if(nsg->seq[contain_uId].del) break;
        ///contain_uId must be a unitig

        if(init_contain_uId != (uint32_t)-1)
        {
            init_contain_uId = contain_uId;
        }
        else
        {
            if(init_contain_uId != contain_uId) break;
        }
         

        ql = sq->e - sq->s; tl = st->e - st->s;
        r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);

        ///if st is contained in sq, or vice verse, skip
        if(r < 0) break;

        ///if sq and v are not in the same direction, skip
        ///endRid is (t.ul>>32), and t.v is a contained read
        if((t.ul>>32) != endRid) break;

        if(chain_edges) kv_push(asg_arc_t, chain_edges->a, t);
        chainLen++;
        kv_push(uint32_t, chain_buffer->a, contain_uId);
        ///endRid is (t.ul>>32), and t.v is a contained read
        ///find edges from t.v to existing unitigs
        ava_cur = get_num_edges2existing_nodes_advance(ug, r_g, reverse_sources, coverage_cut, 
            ruIndex, max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, 1);
        //means find an aim
        if(ava_cur > 0)
        {
            /**
            //do nothing if the chainLen == 1
            if(chainLen > 1)
            {
                asg_arc_t t_max;
                ma_hit_t_alloc* x = &(sources[(endRid>>1)]);
                uint32_t ava_ol_max = 0, ava_max = 0, k;
                for (k = 0; k < x->length; k++)
                {
                    h = &(x->buffer[k]);
                    sq = &(coverage_cut[Get_qn(*h)]);
                    st = &(coverage_cut[Get_tn(*h)]);
                    trio_flag = R_INF.trio_flag[Get_qn(*h)];

                    ///don't want to edges between different haps
                    non_trio_flag = (uint32_t)-1;
                    if(trio_flag == FATHER) non_trio_flag = MOTHER;
                    if(trio_flag == MOTHER) non_trio_flag = FATHER;
                    if(R_INF.trio_flag[Get_tn(*h)] == non_trio_flag) continue;
                    
                    ///just need deleted edges
                    ///sq might be deleted or not
                    if(!st->del) continue;
                    if(!h->del) continue;

                    ///tn must be contained in another existing read
                    get_R_to_U(ruIndex, Get_tn(*h), &contain_rId, &is_Unitig);
                    if(contain_rId == (uint32_t)-1 || is_Unitig == 1) continue;
                    if(r_g->seq[contain_rId].del) continue;

                    get_R_to_U(ruIndex, contain_rId, &contain_uId, &is_Unitig);
                    if(contain_uId == (uint32_t)-1 || is_Unitig != 1) continue;
                    if(nsg->seq[contain_uId].del) continue;
                    ///contain_uId must be a unitig

                    ql = sq->e - sq->s; tl = st->e - st->s;
                    r = ma_hit2arc(h, ql, tl, max_hang, asm_opt.max_hang_rate, min_ovlp, &t);

                    ///if st is contained in sq, or vice verse, skip
                    if(r < 0) continue;

                    ///if sq and v are not in the same direction, skip
                    if((t.ul>>32) != endRid) continue;

                    ///pop the last contain_uId
                    chain_buffer->a.n--;
                    kv_push(uint32_t, chain_buffer->a, contain_uId);

                    ///note: t.v is a contained read
                    ///we need to find existing reads linked with w
                    ava_cur = get_num_edges2existing_nodes_advance(ug, r_g, sources, coverage_cut, ruIndex, 
                    max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, 1);

                    if((ava_cur > ava_max) || (ava_cur == ava_max && test_oLen > ava_ol_max))
                    {
                        ava_max = ava_cur;
                        ava_ol_max = test_oLen;
                        t_max = t;
                    }
                }

                if(ava_max > 0)
                {
                    ava_cur = ava_max;
                    test_oLen = ava_ol_max;
                    if(chain_edges)
                    {
                        //pop the last edge
                        chain_edges->a.n--;
                        kv_push(asg_arc_t, chain_edges->a, t_max);
                    } 
                }
                else
                {
                    break;
                }
            }
            **/

            (*return_ava_cur) = ava_cur;
            (*return_ava_ol) = test_oLen;
            (*return_chainLen) = chainLen;
            h = NULL;
            return 1;
        }

        if(chainLen >= thresLen) break;

        ///endRid is (t.ul>>32), and t.v is a contained read
        ///haven't found a existing unitig from t.v
        ///check if t.v can link to a new contained read
        endRid = t.v; 
        h = get_best_no_coverage_read(ug, r_g, reverse_sources, coverage_cut, ruIndex, 
            max_hang, min_ovlp, endRid, init_contain_uId);
    }

    return 0;    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r12
movq %r8, %r14
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbp
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rdx
orl $-0x1, (%rdx)
movq 0xb8(%rsp), %rdx
orl $-0x1, (%rdx)
movq 0xa8(%rsp), %r13
orl $-0x1, (%rax)
movq 0x18(%r9), %rax
movq %rax, 0x28(%rsp)
andq $0x0, (%r13)
movl 0xa0(%rsp), %ebx
cmpq $0x0, 0x8(%r13)
movq %rcx, 0x8(%rsp)
je 0xbe2de
movq 0x10(%r13), %rax
xorl %esi, %esi
jmp 0xbe2ff
movq $0x2, 0x8(%r13)
movq 0x10(%r13), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq 0x8(%rsp), %rcx
movq %rax, 0x10(%r13)
movq (%r13), %rsi
leaq 0x1(%rsi), %rdx
movq %rdx, (%r13)
movl %ebx, (%rax,%rsi,4)
movq 0xb0(%rsp), %rax
testq %rax, %rax
je 0xbe31b
andq $0x0, (%rax)
xorl %eax, %eax
testq %rbp, %rbp
je 0xbe564
movl 0x98(%rsp), %ebx
pushq $0x1
popq %r15
testq %rbp, %rbp
je 0xbe562
movq (%rbp), %r8
shrq $0x20, %r8
imulq $0xc, %r8, %r10
movl (%rcx,%r10), %eax
testl %eax, %eax
js 0xbe562
movl 0xc(%rbp), %esi
imulq $0xc, %rsi, %rdx
addq %rcx, %rdx
movl (%rdx), %edi
testl %edi, %edi
js 0xbe562
movq 0x80(%rsp), %r9
movq 0x20(%r9), %r9
cmpl $0x0, (%r9,%r8,8)
js 0xbe562
cmpl $0x0, (%r9,%rsi,8)
js 0xbe562
cmpq %rsi, (%r14)
jbe 0xbe562
movq %r14, %r8
movq %r12, %r14
movq %r8, %r13
movq 0x8(%r8), %r8
movl (%r8,%rsi,4), %r12d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %rsi, %r12
je 0xbe562
testl %r12d, %r12d
jns 0xbe562
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movq 0x28(%rsp), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%r12,8)
js 0xbe562
addq %rcx, %r10
movl 0x4(%r10), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %edi, %edx
leaq 0x19536f(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movq %rbp, %rdi
movl 0x88(%rsp), %ecx
movl 0x90(%rsp), %r8d
leaq 0x30(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xbe562
cmpl %ebx, 0x34(%rsp)
jne 0xbe562
movq 0xb0(%rsp), %rbp
testq %rbp, %rbp
movq 0xa8(%rsp), %rbx
je 0xbe487
movq (%rbp), %rcx
movq 0x10(%rbp), %rax
cmpq 0x8(%rbp), %rcx
jne 0xbe468
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbp)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
imulq $0x18, %rcx, %rcx
movq 0x40(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
movq (%rbx), %rdx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rdx
jne 0xbe4b9
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
movq 0x8(%rsp), %rcx
leaq 0x1(%rdx), %r10
movq %r10, (%rbx)
movl %r12d, (%rax,%rdx,4)
movl 0x38(%rsp), %ebx
movq %r14, %r12
movq %r14, %rdi
movq 0x80(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r13, %r14
movq %r13, %r8
movl 0x88(%rsp), %r9d
pushq $0x1
pushq %r10
pushq %rax
leaq 0x2c(%rsp), %rax
pushq %rax
pushq %rbx
movl 0xb8(%rsp), %eax
pushq %rax
callq 0xc4346
addq $0x30, %rsp
testl %eax, %eax
jne 0xbe573
cmpl 0xd0(%rsp), %r15d
jae 0xbe562
subq $0x8, %rsp
movq %r12, %rdi
movq 0x88(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %r13
movq %r13, %rcx
movq %r14, %r8
movl 0x90(%rsp), %r9d
pushq $-0x1
pushq %rbx
movl 0xa8(%rsp), %eax
pushq %rax
callq 0xbdde4
movq %r13, %rcx
addq $0x20, %rsp
movq %rax, %rbp
incl %r15d
jmp 0xbe331
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x14(%rsp), %eax
movq 0xc0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xc8(%rsp), %rax
movl %r15d, (%rax)
pushq $0x1
popq %rax
jmp 0xbe564