// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_no_coverage_reads_chain_simple(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_simple(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;
    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;
    }
    else
    {
        return 0;
    }
    

    ///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 $0x58, %rsp
movq %rdi, %r14
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %r10
movq 0xc8(%rsp), %rsi
movq 0xc0(%rsp), %rbp
movq 0xb8(%rsp), %r11
movl 0xb0(%rsp), %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%r10)
movl %eax, (%rsi)
movl %eax, (%rdi)
movq 0x18(%r9), %rax
movq %rax, 0x8(%rsp)
movq $0x0, (%r11)
cmpq $0x0, 0x8(%r11)
je 0x100644
movq 0x10(%r11), %rax
xorl %esi, %esi
jmp 0x10068f
movq $0x2, 0x8(%r11)
movq 0x10(%r11), %rdi
movl $0x8, %esi
movq %r14, %rbp
movq %rdx, %r14
movq %rcx, %r13
movq %r9, %r15
movq %r8, %r12
callq 0x2370
movq %r12, %r8
movq %r15, %r9
movq %r13, %rcx
movq %r14, %rdx
movq %rbp, %r14
movq 0xb8(%rsp), %r11
movq 0xc0(%rsp), %rbp
movq %rax, 0x10(%r11)
movq (%r11), %rsi
leaq 0x1(%rsi), %r15
movq %r15, (%r11)
movl %ebx, (%rax,%rsi,4)
testq %rbp, %rbp
je 0x1006a6
movq $0x0, (%rbp)
xorl %ebx, %ebx
testq %r14, %r14
je 0x100851
movq (%r14), %r13
movq %r13, %r11
shrq $0x20, %r11
leaq (%r11,%r11,2), %rsi
movl (%rcx,%rsi,4), %edi
testl %edi, %edi
js 0x100851
movq %r8, 0x40(%rsp)
movl 0xc(%r14), %r8d
movq %r8, 0x10(%rsp)
leaq (%r8,%r8,2), %r8
leaq (%rcx,%r8,4), %r10
movl (%r10), %r12d
testl %r12d, %r12d
js 0x100851
movq 0x90(%rsp), %r8
movq 0x20(%r8), %rbx
cmpl $0x0, (%rbx,%r11,8)
js 0x10084f
movq 0x10(%rsp), %r11
cmpl $0x0, (%rbx,%r11,8)
js 0x10084f
movq 0x40(%rsp), %r8
cmpq %r11, (%r8)
movl $0x0, %ebx
jbe 0x100851
movq 0x8(%r8), %r11
movq 0x10(%rsp), %r8
movl (%r11,%r8,4), %r8d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpq %r11, %r8
je 0x100851
testl %r8d, %r8d
jns 0x100851
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r11
movq 0x20(%r11), %r11
cmpl $0x0, (%r11,%r8,8)
js 0x100851
movq %r8, 0x50(%rsp)
leaq (%rcx,%rsi,4), %r8
movl 0x4(%r8), %r11d
subl %edi, %r11d
movl 0x4(%r10), %r8d
subl %r12d, %r8d
movq 0x18(%r14), %rsi
movq %rsi, 0x38(%rsp)
testl %esi, %esi
js 0x1007a4
movl 0x10(%r14), %esi
movq %r14, %rdi
movl 0x14(%r14), %r14d
subl %r14d, %r8d
movl %r8d, %r10d
movl %esi, 0x28(%rsp)
movl %esi, %r8d
jmp 0x1007b7
movl 0x10(%r14), %r10d
movq %r14, %rdi
movl 0x14(%r14), %r14d
subl %r14d, %r8d
movl %r10d, 0x28(%rsp)
cmpl %r13d, %r8d
movl %r13d, %esi
cmovll %r8d, %esi
movq %rdi, 0x48(%rsp)
movl 0x8(%rdi), %r12d
movl %r11d, 0x1c(%rsp)
movl %r11d, %edi
movl %r12d, 0x8(%rsp)
subl %r12d, %edi
cmpl %r10d, %edi
movl %r10d, 0x20(%rsp)
movl %r10d, %r11d
cmovll %edi, %r11d
movl 0x98(%rsp), %r10d
cmpl %r10d, %esi
jg 0x100851
cmpl %r10d, %r11d
jg 0x100851
leaq 0x194f56(%rip), %rbx    # 0x295758
movl 0x8(%rsp), %r10d
subl %r13d, %r10d
cvtsi2ss %r10, %xmm1
addl %esi, %r10d
addl %r11d, %r10d
cvtsi2ss %r10, %xmm2
movss 0x158(%rbx), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x10084f
subl 0x28(%rsp), %r14d
xorps %xmm1, %xmm1
cvtsi2ss %r14, %xmm1
addl %esi, %r11d
addl %r14d, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x100862
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl 0x20(%rsp), %edi
setl %dil
setg %bl
cmpl %r8d, %r13d
setg %r14b
setl %sil
orb %bl, %r14b
cmpb $0x1, %r14b
jne 0x10084f
orb %dil, %sil
movl $0x0, %ebx
je 0x100851
movl 0xa0(%rsp), %esi
cmpl %esi, %r10d
jl 0x100851
cmpl %esi, %r11d
movl 0x20(%rsp), %edi
jl 0x100851
cmpl $0x0, 0x38(%rsp)
setns %sil
addl 0x8(%rsp), %edi
movl 0x1c(%rsp), %r10d
subl %edi, %r10d
xorl %r11d, %r11d
movl %r13d, %edi
subl %r8d, %edi
setle %r11b
setg %r8b
cmovlel %r10d, %edi
shrq $0x1f, %r13
andl $-0x2, %r13d
orq %r11, %r13
testl %edi, %edi
setns %r10b
movl 0xa8(%rsp), %r11d
cmpq %r11, %r13
sete %r11b
andb %r10b, %r11b
cmpb $0x1, %r11b
jne 0x100851
movq %rdx, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %r9, %rbx
xorb %r8b, %sil
movzbl %sil, %r8d
movq 0x10(%rsp), %rcx
leal (%r8,%rcx,2), %ecx
movq %rcx, 0x8(%rsp)
testq %rbp, %rbp
je 0x1009df
shlq $0x20, %r13
leaq (%rdi,%r13), %rax
movq %rax, 0x10(%rsp)
movl 0x1c(%rsp), %r12d
subl %edi, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl %r12d, %r13d
movq 0x38(%rsp), %rax
shrl $0x10, %eax
andl $0x4000, %eax           # imm = 0x4000
movq 0x48(%rsp), %r12
movb 0x20(%r12), %r14b
movzbl 0x21(%r12), %r15d
shll $0xf, %r15d
orl %eax, %r15d
movq (%rbp), %rcx
cmpq 0x8(%rbp), %rcx
jne 0x10099e
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
jmp 0x1009a2
movq 0x10(%rbp), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rsp), %rdx
movq %rdx, (%rax,%rcx,8)
movq 0x8(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx,8)
movl %r13d, 0xc(%rax,%rcx,8)
movw %r15w, 0x10(%rax,%rcx,8)
movb %r14b, 0x12(%rax,%rcx,8)
movq 0xb8(%rsp), %rax
movq (%rax), %r15
movq 0x10(%rax), %rax
movq 0xb8(%rsp), %r14
cmpq 0x8(%r14), %r15
jne 0x100a14
leaq (%r15,%r15), %rdx
testq %r15, %r15
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %r15
leaq 0x1(%r15), %r10
movq %r10, (%r14)
movq 0x50(%rsp), %rcx
movl %ecx, (%rax,%r15,4)
leaq 0x34(%rsp), %r11
movq %rbx, %rdi
movq 0x90(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x40(%rsp), %r8
movl 0x98(%rsp), %r9d
pushq $0x1
pushq %r10
pushq %rax
pushq %r11
pushq 0x28(%rsp)
movl 0xc8(%rsp), %eax
pushq %rax
callq 0x106a2e
addq $0x30, %rsp
testl %eax, %eax
movl $0x0, %ebx
je 0x100851
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x34(%rsp), %eax
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xd8(%rsp), %rax
movl $0x1, (%rax)
movl $0x1, %ebx
jmp 0x100851