// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %rax
movq 0xd8(%rsp), %rbx
movq 0xd0(%rsp), %r10
movq 0xc8(%rsp), %r14
movq 0xc0(%rsp), %r11
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%r10)
movl %edi, (%r14)
movl %edi, (%rbx)
movq 0x18(%r8), %rdi
movq %rdi, 0x60(%rsp)
movq $0x0, (%r11)
xorl %r10d, %r10d
testq %rax, %rax
je 0xf5ed1
movq %rsi, 0x50(%rsp)
movq %rdx, 0x28(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x58(%rsp)
movq %r9, 0x30(%rsp)
movl 0xb0(%rsp), %ecx
movq %rcx, 0x18(%rsp)
movl $0x1, 0x20(%rsp)
movl %ecx, 0x24(%rsp)
movq (%rax), %r12
movq %r12, %rdx
shrq $0x20, %rdx
movl 0xc(%rax), %ecx
movq %rcx, 0x10(%rsp)
movl 0x100(%rsp), %ecx
testl %ecx, %ecx
jne 0xf5aad
leaq 0x1a07b7(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %rcx
movb (%rcx,%rdx), %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
movq 0x10(%rsp), %rsi
movzbl (%rcx,%rsi), %ecx
cmpl %ecx, %edi
je 0xf5e7c
movq 0x10(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x28(%rsp), %rsi
leaq (%rsi,%rcx,4), %rcx
movl (%rcx), %esi
testl %esi, %esi
jns 0xf5e7c
movq 0x18(%rax), %r8
movq %r8, 0x40(%rsp)
testq %r8, %r8
jns 0xf5e7c
movq 0x38(%rsp), %rdi
movq (%rdi), %r9
cmpq 0x10(%rsp), %r9
jbe 0xf5e7c
movq 0x38(%rsp), %rdi
movq 0x8(%rdi), %rdi
movq 0x10(%rsp), %r8
movslq (%rdi,%r8,4), %r8
testq %r8, %r8
js 0xf5e7c
cmpq %r8, %r9
jbe 0xf5e7c
movq 0x30(%rsp), %r9
movq 0x20(%r9), %r9
cmpl $0x0, (%r9,%r8,8)
js 0xf5e7c
movl (%rdi,%r8,4), %edi
cmpl $-0x2, %edi
jg 0xf5e7c
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq 0x60(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0xf5e7c
movl 0x4(%rcx), %ecx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %esi
subl %esi, %ecx
cmpl $0x0, 0x40(%rsp)
js 0xf5b70
movl 0x10(%rax), %r8d
movl 0x14(%rax), %edi
subl %edi, %ecx
movl %ecx, %esi
movl %r8d, %ecx
jmp 0xf5b7c
movl 0x10(%rax), %r8d
movl 0x14(%rax), %edi
subl %edi, %ecx
movl %r8d, %esi
movq 0x28(%rsp), %r9
movl 0xa0(%rsp), %r10d
leaq (%rdx,%rdx,2), %rdx
leaq (%r9,%rdx,4), %rdx
movl 0x4(%rdx), %r13d
movl (%rdx), %edx
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edx
subl %edx, %r13d
cmpl %r12d, %ecx
movl %r12d, %ebx
cmovll %ecx, %ebx
movl 0x8(%rax), %edx
movl %r13d, %r11d
subl %edx, %r11d
cmpl %esi, %r11d
movl %esi, %r9d
cmovll %r11d, %r9d
cmpl %r10d, %ebx
jg 0xf5e7c
movl 0xa0(%rsp), %r10d
cmpl %r10d, %r9d
jg 0xf5e7c
movl %edx, %r10d
subl %r12d, %r10d
cvtsi2ss %r10, %xmm1
addl %ebx, %r10d
addl %r9d, %r10d
cvtsi2ss %r10, %xmm2
leaq 0x19fb62(%rip), %r14    # 0x295758
movss 0x158(%r14), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf5e7c
subl %r8d, %edi
xorps %xmm1, %xmm1
cvtsi2ss %rdi, %xmm1
addl %ebx, %r9d
addl %edi, %r9d
xorps %xmm2, %xmm2
cvtsi2ss %r9, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
ja 0xf5e7c
cmpl %esi, %r11d
setl %dil
setg %r11b
cmpl %ecx, %r12d
setg %bl
setl %r8b
orb %r11b, %bl
cmpb $0x1, %bl
jne 0xf5e7c
orb %dil, %r8b
je 0xf5e7c
movl 0xa8(%rsp), %edi
cmpl %edi, %r10d
jl 0xf5e7c
movl 0xa8(%rsp), %edi
cmpl %edi, %r9d
jl 0xf5e7c
cmpl $0x0, 0x40(%rsp)
setns %r15b
addl %edx, %esi
movl %r13d, %edx
subl %esi, %edx
xorl %ebp, %ebp
movl %r12d, %r14d
subl %ecx, %r14d
setle %cl
setg %sil
cmovlel %edx, %r14d
testl %r14d, %r14d
js 0xf5e7c
movb %cl, %bpl
shrq $0x1f, %r12
andl $-0x2, %r12d
orq %r12, %rbp
movl 0x18(%rsp), %ecx
cmpl %ecx, %ebp
jne 0xf5e7c
movb 0x20(%rax), %r10b
movzbl 0x21(%rax), %ebx
movq 0xc0(%rsp), %rdi
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0xf5d26
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rdi)
shlq $0x3, %rdx
movb %sil, 0xf(%rsp)
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
movb %r10b, 0x18(%rsp)
callq 0x2370
movb 0xf(%rsp), %sil
movb 0x18(%rsp), %r10b
movq 0xc0(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
xorb %sil, %r15b
movzbl %r15b, %edx
movq 0x10(%rsp), %rsi
leal (%rdx,%rsi,2), %r8d
shlq $0x20, %rbp
leaq (%r14,%rbp), %rdx
subl %r14d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
movq 0x40(%rsp), %r9
shrl $0x10, %r9d
andl $0x4000, %r9d           # imm = 0x4000
movl 0x24(%rsp), %esi
andl $0x3fff, %esi           # imm = 0x3FFF
orl %r9d, %esi
shll $0xf, %ebx
orl %esi, %ebx
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movq %r8, 0x18(%rsp)
movl %r8d, 0x8(%rax,%rcx,8)
movl %r13d, 0xc(%rax,%rcx,8)
movl %ebx, 0x24(%rsp)
movw %bx, 0x10(%rax,%rcx,8)
movb %r10b, 0x12(%rax,%rcx,8)
shrl %r12d
movq 0xe8(%rsp), %rax
cmpb $0x0, (%rax,%r12)
jne 0xf5e7c
movq 0x10(%rsp), %rdx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpb $0x0, (%rax,%rdx)
jne 0xf5e7c
movq 0xe8(%rsp), %rbx
movb $0x1, %al
movb %al, (%rbx,%rdx)
movb %al, (%rbx,%r12)
movq 0x58(%rsp), %r15
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rbp
movq %rbp, %rdx
movq 0x28(%rsp), %r13
movq %r13, %rcx
movq 0x38(%rsp), %r12
movq %r12, %r8
movq 0xe0(%rsp), %r9
movl 0x100(%rsp), %ebx
pushq %rbx
pushq 0xf8(%rsp)
leaq 0x5c(%rsp), %rax
pushq %rax
movq 0x30(%rsp), %r14
pushq %r14
movl 0xc8(%rsp), %ebx
pushq %rbx
movl 0xc8(%rsp), %ebx
pushq %rbx
callq 0x106eaa
addq $0x30, %rsp
testl %eax, %eax
jne 0xf5ee3
subq $0x8, %rsp
movq %r15, %rdi
movq 0x38(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
movq %r12, %r8
movl %ebx, %r9d
movl 0x108(%rsp), %eax
pushq %rax
pushq %r14
movl 0xc0(%rsp), %eax
pushq %rax
callq 0xf4a14
addq $0x20, %rsp
incl 0x20(%rsp)
testq %rax, %rax
jne 0xf5a55
movq 0xc0(%rsp), %rsi
cmpq $0x0, (%rsi)
movl $0x0, %r10d
je 0xf5ed1
movl $0x1, %eax
xorl %r10d, %r10d
xorl %ecx, %ecx
movq 0xe8(%rsp), %rdi
movq 0x10(%rsi), %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx), %rdx
shrq $0x21, %rdx
movb %r10b, (%rdi,%rdx)
movq 0x10(%rsi), %rdx
movl 0x8(%rdx,%rcx), %ecx
shrl %ecx
movb %r10b, (%rdi,%rcx)
movl %eax, %ecx
incl %eax
cmpq %rcx, (%rsi)
ja 0xf5ea2
movl %r10d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe8(%rsp), %r8
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x4c(%rsp), %eax
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xd8(%rsp), %rax
movl 0x20(%rsp), %ecx
movl %ecx, (%rax)
movq 0xc0(%rsp), %rdi
cmpq $0x0, (%rdi)
je 0xf5f62
movl $0x1, %r10d
xorl %eax, %eax
movl $0x1, %ecx
xorl %edx, %edx
movq 0x10(%rdi), %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rsi,%rdx), %rsi
shrq $0x21, %rsi
movb %al, (%r8,%rsi)
movq 0x10(%rdi), %rsi
movl 0x8(%rsi,%rdx), %edx
shrl %edx
movb %al, (%r8,%rdx)
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%rdi)
ja 0xf5f2e
jmp 0xf5ed1
movl $0x1, %r10d
jmp 0xf5ed1