// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_contained_reads_chain_by_broken_bub(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_asg_arc_t_warp*, unsigned int*, unsigned int*, unsigned int*, unsigned char*, unsigned char*, unsigned char*, unsigned int, unsigned int)
int get_contained_reads_chain_by_broken_bub(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_asg_arc_t_warp* chain_edges, uint32_t* return_ava_cur, uint32_t* return_ava_ol, uint32_t* return_chainLen, 
uint8_t* expect_vis, uint8_t* circle_vis, uint8_t* utg_vis, 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, i;
    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_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;

        kv_push(asg_arc_t, chain_edges->a, t);
        chainLen++;

        if(circle_vis[t.ul>>33] || circle_vis[t.v>>1]) break;

        circle_vis[t.ul>>33] = circle_vis[t.v>>1] = 1;
        ///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_by_broken_bub(ug, r_g, sources, 
        coverage_cut, ruIndex, expect_vis, max_hang, min_ovlp, t.v, &test_oLen, utg_vis, ignore_trio_flag);
        //means find an aim
        if(ava_cur > 0)
        {
            (*return_ava_cur) = ava_cur;
            (*return_ava_ol) = test_oLen;
            (*return_chainLen) = chainLen;
            h = NULL;
            for (i = 0; i < chain_edges->a.n; i++)
            {
                circle_vis[chain_edges->a.a[i].ul>>33] = 0;
                circle_vis[chain_edges->a.a[i].v>>1] = 0;
            }
            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);
    }

    for (i = 0; i < chain_edges->a.n; i++)
    {
        circle_vis[chain_edges->a.a[i].ul>>33] = 0;
        circle_vis[chain_edges->a.a[i].v>>1] = 0;
    }
    return 0;    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xd8, %rsp
movl 0x70(%rbp), %eax
movl 0x68(%rbp), %eax
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
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 0x40(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x38(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x48(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x6c(%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)
cmpq $0x0, -0x28(%rbp)
je 0x152c5e
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 0x272a54(%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, 0x70(%rbp)
jne 0x1528dd
cmpl $0x1, -0x54(%rbp)
jne 0x1528ae
movl $0x2, -0x58(%rbp)
cmpl $0x2, -0x54(%rbp)
jne 0x1528bb
movl $0x1, -0x58(%rbp)
leaq 0x272a0e(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x58(%rbp), %eax
jne 0x1528db
jmp 0x152c5e
jmp 0x1528dd
movq -0x90(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x1528f3
jmp 0x152c5e
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
jne 0x152909
jmp 0x152c5e
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 0x15292d
cmpl $0x1, -0x64(%rbp)
jne 0x152932
jmp 0x152c5e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x5c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x15294d
jmp 0x152c5e
movq -0x40(%rbp), %rdi
movl -0x5c(%rbp), %esi
leaq -0x60(%rbp), %rdx
leaq -0x64(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x60(%rbp)
je 0x15296d
cmpl $0x1, -0x64(%rbp)
je 0x152972
jmp 0x152c5e
movq -0xb0(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x60(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x152990
jmp 0x152c5e
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, -0x78(%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, -0x7c(%rbp)
movq -0x28(%rbp), %rdi
movl -0x78(%rbp), %esi
movl -0x7c(%rbp), %edx
movl 0x10(%rbp), %ecx
leaq 0x271e18(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x18(%rbp), %r8d
leaq -0xa8(%rbp), %r9
callq 0xd0300
movl %eax, -0x80(%rbp)
cmpl $0x0, -0x80(%rbp)
jge 0x152a06
jmp 0x152c5e
movq -0xa8(%rbp), %rax
shrq $0x20, %rax
movl 0x20(%rbp), %ecx
cmpq %rcx, %rax
je 0x152a1e
jmp 0x152c5e
jmp 0x152a20
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x152a8e
movq 0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x152a50
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x152a5e
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x152a5e
movq -0xb8(%rbp), %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x30(%rbp), %rax
imulq $0x18, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x30(%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)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq 0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
shrq $0x21, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x152b00
movq 0x58(%rbp), %rax
movl -0xa0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x152b05
jmp 0x152c5e
movq 0x58(%rbp), %rax
movl -0xa0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq 0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
shrq $0x21, %rcx
movb $0x1, (%rax,%rcx)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movq 0x50(%rbp), %r9
movl 0x10(%rbp), %r15d
movl 0x18(%rbp), %r14d
movl -0xa0(%rbp), %ebx
movq 0x60(%rbp), %r10
movl 0x70(%rbp), %eax
leaq -0x74(%rbp), %r11
movl %r15d, (%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movl %eax, 0x28(%rsp)
callq 0x169640
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jbe 0x152c17
movl -0x70(%rbp), %ecx
movq 0x38(%rbp), %rax
movl %ecx, (%rax)
movl -0x74(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
movq 0x48(%rbp), %rax
movl %ecx, (%rax)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq 0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x152c0b
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x68(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
movb $0x0, (%rax,%rcx)
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x68(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x152bb2
movl $0x1, -0x1c(%rbp)
jmp 0x152cc5
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 0x70(%rbp), %eax
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x151830
movq %rax, -0x28(%rbp)
jmp 0x152832
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq 0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x152cbe
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x68(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
movb $0x0, (%rax,%rcx)
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x68(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x152c65
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xd8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)