// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %rax
movq 0xc8(%rsp), %rbp
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %r13
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, (%rsi)
movl %ecx, (%rdi)
movl %ecx, (%rdx)
movq 0x18(%r8), %rcx
movq %rcx, 0x28(%rsp)
movq $0x0, (%r13)
testq %rax, %rax
je 0xf3442
movq %r8, %r15
movl 0x90(%rsp), %ecx
movq %rcx, 0x8(%rsp)
movl $0x1, 0x4(%rsp)
movq (%rax), %rcx
shrq $0x20, %rcx
movl 0xc(%rax), %r8d
movl 0xe0(%rsp), %edx
testl %edx, %edx
jne 0xf31be
leaq 0x1980a2(%rip), %rdx    # 0x28b230
movq 0x28(%rdx), %rdx
movb (%rdx,%rcx), %sil
xorl %edi, %edi
cmpb $0x1, %sil
sete %dil
leal (%rdi,%rdi,2), %edi
decl %edi
cmpb $0x2, %sil
movl $0x1, %esi
cmovel %esi, %edi
movzbl (%rdx,%r8), %edx
cmpl %edx, %edi
je 0xf3442
leaq (%r8,%r8,2), %rdx
leaq (%r14,%rdx,4), %rdx
movl (%rdx), %edi
testl %edi, %edi
jns 0xf3442
cmpq $0x0, 0x18(%rax)
jns 0xf3442
movq 0x10(%rsp), %r10
movq (%r10), %rsi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
cmpq %r8, %rsi
jbe 0xf320c
movq 0x8(%r10), %r10
movl (%r10,%r8,4), %r8d
cmpl $-0x1, %r8d
je 0xf320c
movl %r8d, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
shrl $0x1f, %r8d
jmp 0xf320f
xorl %r8d, %r8d
cmpl $-0x1, %r9d
je 0xf3442
testb %r8b, %r8b
jne 0xf3442
movq 0x20(%rsp), %r8
movq 0x20(%r8), %r8
movl %r9d, %r10d
cmpl $0x0, (%r8,%r10,8)
js 0xf3442
movb $0x1, %r9b
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmpq %r10, %rsi
jbe 0xf3269
movq 0x10(%rsp), %rsi
movq 0x8(%rsi), %rsi
movl (%rsi,%r10,4), %esi
cmpl $-0x1, %esi
je 0xf3269
movl %esi, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
testl %esi, %esi
setns %r9b
cmpl $-0x1, %r8d
je 0xf3442
testb %r9b, %r9b
jne 0xf3442
movq 0x28(%rsp), %rsi
movq 0x20(%rsi), %rsi
movl %r8d, %r8d
cmpl $0x0, (%rsi,%r8,8)
js 0xf3442
leaq (%rcx,%rcx,2), %rcx
leaq (%r14,%rcx,4), %rcx
movl 0x4(%rcx), %esi
movl (%rcx), %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %ecx
subl %ecx, %esi
movl 0x4(%rdx), %edx
andl %r8d, %edi
subl %edi, %edx
leaq 0x19749e(%rip), %rcx    # 0x28a758
movss 0x158(%rcx), %xmm0
movq %rax, %rdi
movl 0x80(%rsp), %ecx
movl 0x88(%rsp), %r8d
leaq 0x30(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xf3442
movq 0x30(%rsp), %r12
movq %r12, %rax
shrq $0x20, %rax
cmpl 0x8(%rsp), %eax
jne 0xf3442
movq %r15, %rdi
movq 0x8(%r13), %rax
cmpq %rax, (%r13)
jne 0xf3335
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %r15, %rdi
movq %rax, 0x10(%r13)
movq (%r13), %rax
movq 0x10(%r13), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r13)
leaq (%rax,%rax,2), %rax
movq 0x40(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x30(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
shrq $0x21, %r12
cmpb $0x0, (%rbp,%r12)
jne 0xf3442
movl 0x38(%rsp), %eax
movq %rax, 0x8(%rsp)
shrl %eax
cmpb $0x0, (%rbp,%rax)
jne 0xf3442
movb $0x1, %cl
movb %cl, (%rbp,%rax)
movb %cl, (%rbp,%r12)
movq 0x20(%rsp), %r12
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
movq %r14, %r13
movq 0x10(%rsp), %r14
movq %r14, %r8
movq 0xc0(%rsp), %r9
movl 0xe0(%rsp), %eax
pushq %rax
pushq 0xd8(%rsp)
leaq 0x2c(%rsp), %rax
pushq %rax
movq 0x20(%rsp), %rbp
pushq %rbp
movl 0xa8(%rsp), %eax
pushq %rax
movl 0xa8(%rsp), %eax
pushq %rax
movq %rdi, %r15
callq 0x102ae2
addq $0x30, %rsp
testl %eax, %eax
jne 0xf3495
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
movq %r14, %r8
movl 0x88(%rsp), %r9d
movl 0xe8(%rsp), %eax
pushq %rax
pushq %rbp
movl 0xa0(%rsp), %eax
pushq %rax
callq 0xf2622
addq $0x20, %rsp
incl 0x4(%rsp)
testq %rax, %rax
movq %r13, %r14
movq 0xc8(%rsp), %rbp
movq 0xa0(%rsp), %r13
jne 0xf3171
cmpq $0x0, (%r13)
je 0xf3484
movl $0x1, %ecx
xorl %eax, %eax
xorl %edx, %edx
movq 0x10(%r13), %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rsi,%rdx), %rsi
shrq $0x21, %rsi
movb %al, (%rbp,%rsi)
movq 0x10(%r13), %rsi
movl 0x8(%rsi,%rdx), %edx
shrl %edx
movb %al, (%rbp,%rdx)
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%r13)
ja 0xf3452
jmp 0xf3486
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x1c(%rsp), %eax
movq 0xb0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xb8(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0xa0(%rsp), %r9
cmpq $0x0, (%r9)
je 0xf3513
movl $0x1, %eax
xorl %ecx, %ecx
movl $0x1, %edx
xorl %esi, %esi
movq 0xc8(%rsp), %r8
movq 0x10(%r9), %rdi
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdi,%rsi), %rdi
shrq $0x21, %rdi
movb %cl, (%r8,%rdi)
movq 0x10(%r9), %rdi
movl 0x8(%rdi,%rsi), %esi
shrl %esi
movb %cl, (%r8,%rsi)
movl %edx, %esi
incl %edx
cmpq %rsi, (%r9)
ja 0xf34df
jmp 0xf3486
movl $0x1, %eax
jmp 0xf3486