// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_contained_reads_chain(ma_hit_t*, 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, unsigned int)
int get_contained_reads_chain(ma_hit_t *h, ma_hit_t_alloc* 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, uint32_t ignore_trio_flag)
{
    (*return_chainLen) = (*return_ava_cur) = (*return_ava_ol) = (uint32_t)-1;
    uint32_t trio_flag, non_trio_flag = (uint32_t)-1, contain_rId, 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;
    

    ///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)]);
        trio_flag = R_INF.trio_flag[Get_qn(*h)];

        ///don't want to edges between different haps
        non_trio_flag = (uint32_t)-1;
        if(ignore_trio_flag == 0)
        {
            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) break;
        }

        ///just need deleted edges
        ///sq might be deleted or not
        if(!st->del) break;
        if(!h->del) break;

        ///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) break;
        if(r_g->seq[contain_rId].del) break;

        get_R_to_U(ruIndex, contain_rId, &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

        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, sources, coverage_cut, ruIndex, 
                    max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, 
                    ignore_trio_flag);
        //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_contained_read(ug, r_g, sources, coverage_cut, ruIndex, 
            max_hang, min_ovlp, endRid, ignore_trio_flag);
    }

    return 0;    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rsi
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %r8
movl 0xa8(%rsp), %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rdx)
movl %eax, (%rsi)
movq %r8, %rsi
movl %eax, (%rcx)
movq 0x18(%r15), %rax
movq %rax, 0x50(%rsp)
movq $0x0, (%r8)
cmpq $0x0, 0x8(%r8)
je 0xf503a
movq 0x10(%rsi), %rax
xorl %ecx, %ecx
jmp 0xf5067
movq $0x2, 0x8(%rsi)
movq 0x10(%rsi), %rdi
movl $0x8, %esi
callq 0x2370
movq 0xb0(%rsp), %rsi
movq 0xb8(%rsp), %rdi
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
movl %ebp, (%rax,%rcx,4)
testq %rdi, %rdi
je 0xf507d
movq $0x0, (%rdi)
xorl %eax, %eax
testq %rbx, %rbx
je 0xf54ed
movl 0xe0(%rsp), %ebp
movl 0xd8(%rsp), %eax
movl 0xa0(%rsp), %ecx
movq %rcx, 0x8(%rsp)
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, 0x20(%rsp)
movl $0x1, %r11d
movl %eax, 0x14(%rsp)
movq %r14, 0x40(%rsp)
movq %r15, 0x38(%rsp)
movq %r12, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq (%rbx), %r13
movq %r13, %rsi
shrq $0x20, %rsi
movl 0xc(%rbx), %eax
testl %ebp, %ebp
jne 0xf510e
leaq 0x1a114e(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %rcx
movb (%rcx,%rsi), %dl
xorl %edi, %edi
cmpb $0x1, %dl
sete %dil
leal (%rdi,%rdi,2), %edi
decl %edi
cmpb $0x2, %dl
movl $0x1, %edx
cmovel %edx, %edi
movzbl (%rcx,%rax), %ecx
cmpl %ecx, %edi
je 0xf54eb
leaq (%rax,%rax,2), %rcx
movq 0x18(%rsp), %rdx
leaq (%rdx,%rcx,4), %rdx
movl (%rdx), %edi
testl %edi, %edi
jns 0xf54eb
movq 0x18(%rbx), %rcx
testq %rcx, %rcx
jns 0xf54eb
movq (%r12), %r10
cmpq %rax, %r10
jbe 0xf54eb
movq 0x8(%r12), %r8
movslq (%r8,%rax,4), %r9
testq %r9, %r9
js 0xf54eb
cmpq %r9, %r10
jbe 0xf54eb
movq 0x20(%r14), %r10
cmpl $0x0, (%r10,%r9,8)
js 0xf54eb
movl (%r8,%r9,4), %ebp
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmpq %r8, %rbp
je 0xf54eb
testl %ebp, %ebp
jns 0xf54eb
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq 0x50(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rbp,8)
js 0xf54eb
movl %r11d, 0x4(%rsp)
movl 0x4(%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edi
subl %edi, %edx
testl %ecx, %ecx
js 0xf51c7
movl 0x10(%rbx), %r9d
movl 0x14(%rbx), %r8d
subl %r8d, %edx
movl %edx, %edi
movl %r9d, %edx
jmp 0xf51d5
movl 0x10(%rbx), %r9d
movl 0x14(%rbx), %r8d
subl %r8d, %edx
movl %r9d, %edi
movl 0x90(%rsp), %r11d
leaq (%rsi,%rsi,2), %rsi
movq 0x18(%rsp), %r10
leaq (%r10,%rsi,4), %rsi
movl 0x4(%rsi), %r14d
movl (%rsi), %esi
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
andl %r10d, %esi
subl %esi, %r14d
cmpl %r13d, %edx
movl %r13d, %r15d
cmovll %edx, %r15d
movl 0x8(%rbx), %esi
movl %r14d, %r12d
subl %esi, %r12d
cmpl %edi, %r12d
movl %edi, %r10d
cmovll %r12d, %r10d
cmpl %r11d, %r15d
jg 0xf54eb
cmpl %r11d, %r10d
jg 0xf54eb
movq %rax, 0x48(%rsp)
movl %esi, %r11d
subl %r13d, %r11d
cvtsi2ss %r11, %xmm1
addl %r15d, %r11d
addl %r10d, %r11d
cvtsi2ss %r11, %xmm2
leaq 0x1a050b(%rip), %rax    # 0x295758
movss 0x158(%rax), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf54eb
subl %r9d, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %r15d, %r10d
addl %r8d, %r10d
xorps %xmm2, %xmm2
cvtsi2ss %r10, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
ja 0xf54eb
cmpl %edi, %r12d
setl %r8b
setg %r15b
cmpl %edx, %r13d
setg %r12b
setl %r9b
orb %r15b, %r12b
cmpb $0x1, %r12b
jne 0xf54eb
orb %r8b, %r9b
je 0xf54eb
movl 0x98(%rsp), %r8d
cmpl %r8d, %r11d
jl 0xf54eb
movq %r8, %r11
cmpl %r11d, %r10d
jl 0xf54eb
testl %ecx, %ecx
setns %r8b
addl %esi, %edi
movl %r14d, %r9d
subl %edi, %r9d
xorl %esi, %esi
movl %r13d, %r12d
subl %edx, %r12d
setle %dil
setg %dl
cmovlel %r9d, %r12d
testl %r12d, %r12d
js 0xf54eb
movb %dil, %sil
shrq $0x1f, %r13
andl $-0x2, %r13d
orq %rsi, %r13
movl 0x8(%rsp), %esi
cmpl %esi, %r13d
jne 0xf54eb
xorb %dl, %r8b
movzbl %r8b, %edx
movq 0x48(%rsp), %rax
leal (%rdx,%rax,2), %eax
movq %rax, 0x8(%rsp)
shrl $0x10, %ecx
andl $0x4000, %ecx           # imm = 0x4000
movl 0x14(%rsp), %edx
andl $0x3fff, %edx           # imm = 0x3FFF
orl %ecx, %edx
movzbl 0x21(%rbx), %eax
shll $0xf, %eax
orl %edx, %eax
movl %eax, %r8d
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0xf53e6
movb 0x20(%rbx), %bl
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0xf53ad
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rdi)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
movl %r8d, %r15d
callq 0x2370
movl %r15d, %r8d
movl 0x98(%rsp), %r11d
movq 0xb8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
shlq $0x20, %r13
leaq (%r12,%r13), %rdx
subl %r12d, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x8(%rsp), %rdx
movl %edx, 0x8(%rax,%rcx,8)
movl %r14d, 0xc(%rax,%rcx,8)
movw %r8w, 0x10(%rax,%rcx,8)
movb %bl, 0x12(%rax,%rcx,8)
movq 0xb0(%rsp), %rdx
movq (%rdx), %rcx
movq 0x10(%rdx), %rax
cmpq 0x8(%rdx), %rcx
movl %r8d, 0x14(%rsp)
jne 0xf543f
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq 0xb0(%rsp), %rcx
movq %rsi, 0x8(%rcx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x98(%rsp), %r11d
movq 0xb0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movq (%rdx), %rcx
movq 0x40(%rsp), %r14
movq 0x38(%rsp), %r15
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r13
leaq 0x1(%rcx), %r10
movq %r10, (%rdx)
movl %ebp, (%rax,%rcx,4)
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x18(%rsp), %rcx
movq %r12, %r8
movl 0x90(%rsp), %r9d
movl 0xe0(%rsp), %ebp
pushq %rbp
pushq %r10
pushq %rax
leaq 0x3c(%rsp), %rax
pushq %rax
pushq 0x28(%rsp)
pushq %r11
callq 0x106a2e
addq $0x30, %rsp
testl %eax, %eax
jne 0xf54fc
movl 0x4(%rsp), %eax
cmpl %eax, 0x20(%rsp)
je 0xf54eb
subq $0x8, %rsp
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq 0x20(%rsp), %rcx
movq %r12, %r8
movl 0x98(%rsp), %r9d
pushq %rbp
pushq 0x18(%rsp)
movl 0xb0(%rsp), %eax
pushq %rax
callq 0xf4a14
movl 0x24(%rsp), %r11d
addq $0x20, %rsp
movq %rax, %rbx
incl %r11d
testq %rax, %rax
jne 0xf50ca
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xc0(%rsp), %rcx
movl %eax, (%rcx)
movl 0x24(%rsp), %eax
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
movq 0xd0(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movl $0x1, %eax
jmp 0xf54ed