// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, 0x20(%rsp)
movq %rcx, %rbp
movq %rdx, %r12
movq %rsi, 0x38(%rsp)
movq 0xe8(%rsp), %r14
movq 0xd8(%rsp), %rax
movq 0xd0(%rsp), %rcx
orl $-0x1, (%rcx)
movq 0xc8(%rsp), %rcx
orl $-0x1, (%rcx)
movq 0xc0(%rsp), %rcx
orl $-0x1, (%rax)
movq %r8, 0x40(%rsp)
movq 0x18(%r8), %rax
movq %rax, 0x48(%rsp)
andq $0x0, (%rcx)
movl 0xb0(%rsp), %eax
movq %rax, 0x10(%rsp)
movl 0xa8(%rsp), %r13d
movl 0xa0(%rsp), %r15d
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movq %rbp, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
testq %rdi, %rdi
je 0xb6273
movq (%rdi), %rax
shrq $0x20, %rax
movl 0xc(%rdi), %esi
movl 0x100(%rsp), %ecx
testl %ecx, %ecx
jne 0xb6054
leaq 0x19e20b(%rip), %rcx    # 0x254230
movq 0x28(%rcx), %rcx
movb (%rcx,%rax), %dl
xorl %r8d, %r8d
cmpb $0x1, %dl
sete %r8b
leal (%r8,%r8,2), %r8d
decl %r8d
cmpb $0x2, %dl
pushq $0x1
popq %rdx
cmovel %edx, %r8d
movzbl (%rcx,%rsi), %ecx
cmpl %ecx, %r8d
je 0xb6273
imulq $0xc, %rsi, %rdx
addq %r12, %rdx
movl (%rdx), %ecx
testl %ecx, %ecx
jns 0xb6273
cmpq $0x0, 0x18(%rdi)
jns 0xb6273
movq (%rbp), %r9
cmpq %rsi, %r9
jbe 0xb6273
movq 0x8(%rbp), %r8
movslq (%r8,%rsi,4), %rsi
testq %rsi, %rsi
js 0xb6273
cmpq %rsi, %r9
jbe 0xb6273
movq 0x20(%rsp), %r9
movq 0x20(%r9), %r9
cmpl $0x0, (%r9,%rsi,8)
js 0xb6273
movl (%r8,%rsi,4), %esi
cmpl $-0x2, %esi
jg 0xb6273
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rsi,8)
js 0xb6273
imulq $0xc, %rax, %rax
addq %r12, %rax
movl 0x4(%rax), %esi
movl (%rax), %eax
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %eax
subl %eax, %esi
movl 0x4(%rdx), %edx
andl %r8d, %ecx
subl %ecx, %edx
leaq 0x19d660(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl %r15d, %ecx
movl %r13d, %r8d
leaq 0x50(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xb6273
movq 0x50(%rsp), %rbx
movq %rbx, %rax
shrq $0x20, %rax
cmpl 0x10(%rsp), %eax
jne 0xb6273
movq 0xc0(%rsp), %rsi
movq (%rsi), %rcx
movq 0x10(%rsi), %rax
cmpq 0x8(%rsi), %rcx
jne 0xb6178
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rsi)
imulq $0x18, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xc0(%rsp), %rsi
movq %r15, %r11
movq %r13, %r10
movq %rax, 0x10(%rsi)
movq (%rsi), %rcx
jmp 0xb617e
movq %r15, %r11
movq %r13, %r10
leaq 0x1(%rcx), %rdx
movq %rdx, (%rsi)
imulq $0x18, %rcx, %rcx
movq 0x60(%rsp), %rdx
movq %rdx, 0x10(%rax,%rcx)
movups 0x50(%rsp), %xmm0
movups %xmm0, (%rax,%rcx)
shrq $0x21, %rbx
cmpb $0x0, (%r14,%rbx)
jne 0xb6273
movl 0x58(%rsp), %eax
movq %rax, 0x10(%rsp)
shrl %eax
cmpb $0x0, (%r14,%rax)
jne 0xb6273
movb $0x1, %cl
movb %cl, (%r14,%rax)
movb %cl, (%r14,%rbx)
movq %rbp, %r8
movq %r12, %rcx
movq 0x40(%rsp), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
movl 0x100(%rsp), %r14d
movq 0x38(%rsp), %rbp
movq %rbp, %rdx
movq 0xe0(%rsp), %r9
pushq %r14
pushq 0xf8(%rsp)
leaq 0x2c(%rsp), %rax
pushq %rax
movq 0x28(%rsp), %r14
pushq %r14
pushq %r10
pushq %r11
movq %r10, %r13
movq %r11, %r15
callq 0xc45b0
addq $0x30, %rsp
testl %eax, %eax
jne 0xb62b0
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
movq 0x30(%rsp), %r12
movq %r12, %rcx
movq 0x38(%rsp), %rbp
movq %rbp, %r8
movl %r15d, %r9d
movl 0x108(%rsp), %eax
pushq %rax
pushq %r14
pushq %r13
callq 0xb558b
addq $0x20, %rsp
movq %rax, %rdi
incl 0xc(%rsp)
movq 0xe8(%rsp), %r14
jmp 0xb6000
xorl %eax, %eax
xorl %ecx, %ecx
movq 0xc0(%rsp), %rdi
movl %ecx, %ecx
cmpq %rcx, (%rdi)
jbe 0xb631a
movq 0x10(%rdi), %rdx
imulq $0x18, %rcx, %rsi
movq (%rdx,%rsi), %rdx
shrq $0x21, %rdx
movb %al, (%r14,%rdx)
movq 0x10(%rdi), %rdx
movl 0x8(%rdx,%rsi), %edx
shrl %edx
movb %al, (%r14,%rdx)
incl %ecx
jmp 0xb627f
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x1c(%rsp), %eax
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xd8(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, (%rax)
xorl %ecx, %ecx
pushq $0x1
popq %rax
xorl %edx, %edx
movq 0xe8(%rsp), %r8
movq 0xc0(%rsp), %r9
movl %edx, %edx
cmpq %rdx, (%r9)
jbe 0xb631a
movq 0x10(%r9), %rsi
imulq $0x18, %rdx, %rdi
movq (%rsi,%rdi), %rsi
shrq $0x21, %rsi
movb %cl, (%r8,%rsi)
movq 0x10(%r9), %rsi
movl 0x8(%rsi,%rdi), %esi
shrl %esi
movb %cl, (%r8,%rsi)
incl %edx
jmp 0xb62ed
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq