// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xe8, %rsp
movl 0x60(%rbp), %eax
movl 0x58(%rbp), %eax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
movq 0x48(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x40(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x50(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x68(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x48(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xb0(%rbp)
movq 0x30(%rbp), %rax
movq $0x0, (%rax)
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x151e6b
movq 0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x151e2a
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x151e38
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x151e38
movq -0xb8(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl 0x28(%rbp), %edx
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
cmpq $0x0, 0x38(%rbp)
je 0x151e9c
movq 0x38(%rbp), %rax
movq $0x0, (%rax)
jmp 0x151e9e
cmpq $0x0, -0x28(%rbp)
je 0x1522c3
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
leaq 0x2733e8(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x54(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x60(%rbp)
jne 0x151f49
cmpl $0x1, -0x54(%rbp)
jne 0x151f1a
movl $0x2, -0x58(%rbp)
cmpl $0x2, -0x54(%rbp)
jne 0x151f27
movl $0x1, -0x58(%rbp)
leaq 0x2733a2(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x58(%rbp), %eax
jne 0x151f47
jmp 0x1522c3
jmp 0x151f49
movq -0x90(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x151f5f
jmp 0x1522c3
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
jne 0x151f75
jmp 0x1522c3
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0xc(%rax), %esi
leaq -0x5c(%rbp), %rdx
leaq -0x64(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x5c(%rbp)
je 0x151f99
cmpl $0x1, -0x64(%rbp)
jne 0x151f9e
jmp 0x1522c3
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x5c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x151fb9
jmp 0x1522c3
movq -0x40(%rbp), %rdi
movl -0x5c(%rbp), %esi
leaq -0x60(%rbp), %rdx
leaq -0x64(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x60(%rbp)
je 0x151fd9
cmpl $0x1, -0x64(%rbp)
je 0x151fde
jmp 0x1522c3
movq -0xb0(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x151ffc
jmp 0x1522c3
movq -0x88(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x88(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x74(%rbp)
movq -0x90(%rbp), %rax
movl 0x4(%rax), %eax
movq -0x90(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
subl %ecx, %eax
movl %eax, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x78(%rbp), %edx
movl 0x10(%rbp), %ecx
leaq 0x2727ac(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x18(%rbp), %r8d
leaq -0xa8(%rbp), %r9
callq 0xd0300
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jge 0x152072
jmp 0x1522c3
movq -0xa8(%rbp), %rax
shrq $0x20, %rax
movl 0x20(%rbp), %ecx
cmpq %rcx, %rax
je 0x15208a
jmp 0x1522c3
cmpq $0x0, 0x38(%rbp)
je 0x152147
jmp 0x152097
movq 0x38(%rbp), %rax
movq (%rax), %rax
movq 0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x152105
movq 0x38(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1520c7
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xc0(%rbp)
jmp 0x1520d5
movl $0x2, %eax
movq %rax, -0xc0(%rbp)
jmp 0x1520d5
movq -0xc0(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rbp), %rax
imulq $0x18, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq -0xa8(%rbp), %rcx
movq %rcx, (%rax)
movq -0xa0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x98(%rbp), %rcx
movq %rcx, 0x10(%rax)
jmp 0x152147
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1521c1
movq 0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x152180
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xc8(%rbp)
jmp 0x15218e
movl $0x2, %eax
movq %rax, -0xc8(%rbp)
jmp 0x15218e
movq -0xc8(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x60(%rbp), %edx
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movl 0x10(%rbp), %r9d
movl 0x18(%rbp), %r15d
movl -0xa0(%rbp), %r14d
movq 0x30(%rbp), %rax
movq 0x10(%rax), %r11
movq 0x30(%rbp), %rax
movq (%rax), %rax
movl %eax, %r10d
movl 0x60(%rbp), %eax
leaq -0x70(%rbp), %rbx
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl %r10d, 0x20(%rsp)
movl %eax, 0x28(%rsp)
callq 0x1692c0
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jbe 0x152272
movl -0x6c(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x70(%rbp), %ecx
movq 0x48(%rbp), %rax
movl %ecx, (%rax)
movl -0x68(%rbp), %ecx
movq 0x50(%rbp), %rax
movl %ecx, (%rax)
movq $0x0, -0x28(%rbp)
movl $0x1, -0x1c(%rbp)
jmp 0x1522ca
movl -0x68(%rbp), %eax
cmpl 0x58(%rbp), %eax
jb 0x15227c
jmp 0x1522c3
movl -0xa0(%rbp), %eax
movl %eax, 0x20(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movl 0x10(%rbp), %r9d
movl 0x18(%rbp), %r11d
movl 0x20(%rbp), %r10d
movl 0x60(%rbp), %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x151830
movq %rax, -0x28(%rbp)
jmp 0x151e9e
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xe8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)