// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_chimeric_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, unsigned char*)
int get_chimeric_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 *rg, 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, uint8_t *cmk)
{
    (*return_chainLen) = (*return_ava_cur) = (*return_ava_ol) = (uint32_t)-1;
    uint32_t trio_flag, non_trio_flag = (uint32_t)-1, contain_rId, 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; chain_edges->a.n = 0;
    
    ///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) || (!h->del) || (!rg->seq[Get_tn(*h)].del)) break;
        // if(cmk[Get_tn(*h)] != ((uint8_t)-1)) break;
        // if(cmk[Get_tn(*h)] == ((uint8_t)-1)) break;

        ///tn must be contained in another existing read
        get_R_to_U(ruIndex, Get_tn(*h), &contain_rId, &is_Unitig);
        if(is_Unitig == 1) break;

        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, rg, 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_cmk_read(ug, rg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, endRid, ignore_trio_flag, cmk);
    }

    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 $0x58, %rsp
movq %r8, 0x50(%rsp)
movq %rdi, %rax
movq 0xc8(%rsp), %r11
movq 0xc0(%rsp), %r8
movq 0xb8(%rsp), %rbx
movq 0xb0(%rsp), %r10
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl %edi, (%r8)
movl %edi, (%rbx)
movl %edi, (%r11)
movq $0x0, (%r10)
xorl %r8d, %r8d
testq %rax, %rax
je 0xf6427
movq %rsi, 0x48(%rsp)
movq %rdx, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %r9, 0x30(%rsp)
movl 0xa0(%rsp), %ecx
movq %rcx, 0x10(%rsp)
movl $0x1, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movq (%rax), %rbx
movq %rbx, %rsi
shrq $0x20, %rsi
movl 0xc(%rax), %ecx
movq %rcx, 0x8(%rsp)
movl 0xf0(%rsp), %ecx
testl %ecx, %ecx
jne 0xf6044
leaq 0x1a021d(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %rcx
movb (%rcx,%rsi), %dl
xorl %edi, %edi
cmpb $0x1, %dl
sete %dil
leal (%rdi,%rdi,2), %edi
decl %edi
cmpb $0x2, %dl
movl $0x1, %edx
cmovel %edx, %edi
movq 0x8(%rsp), %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %edi
je 0xf63d2
movq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x20(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
movl (%rcx), %edx
testl %edx, %edx
jns 0xf63d2
movq 0x18(%rax), %r8
movq %r8, 0x38(%rsp)
testq %r8, %r8
jns 0xf63d2
movq 0x30(%rsp), %rdi
movq 0x20(%rdi), %rdi
movq 0x8(%rsp), %r8
cmpl $0x0, (%rdi,%r8,8)
jns 0xf63d2
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %r8
cmpq %r8, (%rdi)
jbe 0xf60b3
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rdi
movq 0x8(%rsp), %r8
cmpl $-0x1, (%rdi,%r8,4)
jl 0xf63d2
movl 0x4(%rcx), %ecx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %edx
subl %edx, %ecx
cmpl $0x0, 0x38(%rsp)
js 0xf60d6
movl 0x10(%rax), %r8d
movl 0x14(%rax), %edi
subl %edi, %ecx
movl %ecx, %edx
movl %r8d, %ecx
jmp 0xf60e2
movl 0x10(%rax), %r8d
movl 0x14(%rax), %edi
subl %edi, %ecx
movl %r8d, %edx
movq 0x20(%rsp), %r9
movl 0x90(%rsp), %r10d
leaq (%rsi,%rsi,2), %rsi
leaq (%r9,%rsi,4), %rsi
movl 0x4(%rsi), %r15d
movl (%rsi), %esi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %esi
subl %esi, %r15d
cmpl %ebx, %ecx
movl %ebx, %ebp
cmovll %ecx, %ebp
movl 0x8(%rax), %esi
movl %r15d, %r11d
subl %esi, %r11d
cmpl %edx, %r11d
movl %edx, %r9d
cmovll %r11d, %r9d
cmpl %r10d, %ebp
jg 0xf63d2
movl 0x90(%rsp), %r10d
cmpl %r10d, %r9d
jg 0xf63d2
movl %esi, %r10d
subl %ebx, %r10d
cvtsi2ss %r10, %xmm1
addl %ebp, %r10d
addl %r9d, %r10d
cvtsi2ss %r10, %xmm2
leaq 0x19f5fe(%rip), %r14    # 0x295758
movss 0x158(%r14), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf63d2
subl %r8d, %edi
xorps %xmm1, %xmm1
cvtsi2ss %rdi, %xmm1
addl %ebp, %r9d
addl %edi, %r9d
xorps %xmm2, %xmm2
cvtsi2ss %r9, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
ja 0xf63d2
cmpl %edx, %r11d
setl %dil
setg %r11b
cmpl %ecx, %ebx
setg %bpl
setl %r8b
orb %r11b, %bpl
cmpb $0x1, %bpl
jne 0xf63d2
orb %dil, %r8b
je 0xf63d2
movl 0x98(%rsp), %edi
cmpl %edi, %r10d
jl 0xf63d2
movl 0x98(%rsp), %edi
cmpl %edi, %r9d
jl 0xf63d2
cmpl $0x0, 0x38(%rsp)
setns %r13b
addl %esi, %edx
movl %r15d, %esi
subl %edx, %esi
xorl %r12d, %r12d
movl %ebx, %r14d
subl %ecx, %r14d
setle %cl
setg %dl
cmovlel %esi, %r14d
testl %r14d, %r14d
js 0xf63d2
movb %cl, %r12b
shrq $0x1f, %rbx
andl $-0x2, %ebx
orq %rbx, %r12
movl 0x10(%rsp), %ecx
cmpl %ecx, %r12d
jne 0xf63d2
movb 0x20(%rax), %r10b
movzbl 0x21(%rax), %ebp
movq 0xb0(%rsp), %rdi
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0xf6289
movb %dl, 0x7(%rsp)
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rdi)
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rsi
movq %rax, %rdi
movb %r10b, 0x10(%rsp)
callq 0x2370
movb 0x7(%rsp), %dl
movb 0x10(%rsp), %r10b
movq 0xb0(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
xorb %dl, %r13b
movzbl %r13b, %edx
movq 0x8(%rsp), %rsi
leal (%rdx,%rsi,2), %r8d
shlq $0x20, %r12
leaq (%r14,%r12), %rdx
subl %r14d, %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x38(%rsp), %r9
shrl $0x10, %r9d
andl $0x4000, %r9d           # imm = 0x4000
movl 0x1c(%rsp), %esi
andl $0x3fff, %esi           # imm = 0x3FFF
orl %r9d, %esi
shll $0xf, %ebp
orl %esi, %ebp
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movq %r8, 0x10(%rsp)
movl %r8d, 0x8(%rax,%rcx,8)
movl %r15d, 0xc(%rax,%rcx,8)
movl %ebp, 0x1c(%rsp)
movw %bp, 0x10(%rax,%rcx,8)
movb %r10b, 0x12(%rax,%rcx,8)
shrl %ebx
movq 0xd8(%rsp), %rax
cmpb $0x0, (%rax,%rbx)
jne 0xf63d2
movq 0x8(%rsp), %rdx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpb $0x0, (%rax,%rdx)
jne 0xf63d2
movq 0xd8(%rsp), %r14
movb $0x1, %al
movb %al, (%r14,%rdx)
movb %al, (%r14,%rbx)
movq 0x50(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x48(%rsp), %r13
movq %r13, %rdx
movq 0x20(%rsp), %r12
movq %r12, %rcx
movq 0x28(%rsp), %r15
movq %r15, %r8
movq 0xd0(%rsp), %r9
movl 0xf0(%rsp), %ebp
pushq %rbp
pushq 0xe8(%rsp)
leaq 0x54(%rsp), %rax
pushq %rax
movq 0x28(%rsp), %rbx
pushq %rbx
movl 0xb8(%rsp), %r14d
pushq %r14
movl 0xb8(%rsp), %ebp
pushq %rbp
callq 0x106eaa
addq $0x30, %rsp
testl %eax, %eax
jne 0xf6439
subq $0x8, %rsp
movq 0x38(%rsp), %rsi
movq %r13, %rdx
movq %r12, %rcx
movq %r15, %r8
movl %ebp, %r9d
movl 0xf8(%rsp), %eax
pushq %rax
pushq %rbx
pushq %r14
callq 0xf4d0d
addq $0x20, %rsp
incl 0x18(%rsp)
testq %rax, %rax
jne 0xf5fef
movq 0xb0(%rsp), %rsi
cmpq $0x0, (%rsi)
movl $0x0, %r8d
je 0xf6427
movl $0x1, %eax
xorl %r8d, %r8d
xorl %ecx, %ecx
movq 0xd8(%rsp), %rdi
movq 0x10(%rsi), %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rdx,%rcx), %rdx
shrq $0x21, %rdx
movb %r8b, (%rdi,%rdx)
movq 0x10(%rsi), %rdx
movl 0x8(%rdx,%rcx), %ecx
shrl %ecx
movb %r8b, (%rdi,%rcx)
movl %eax, %ecx
incl %eax
cmpq %rcx, (%rsi)
ja 0xf63f8
movl %r8d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xd8(%rsp), %r9
movq 0xb8(%rsp), %rcx
movl %eax, (%rcx)
movl 0x44(%rsp), %eax
movq 0xc0(%rsp), %rcx
movl %eax, (%rcx)
movq 0xc8(%rsp), %rax
movl 0x18(%rsp), %ecx
movl %ecx, (%rax)
movq 0xb0(%rsp), %rdi
cmpq $0x0, (%rdi)
je 0xf64b8
movl $0x1, %r8d
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, (%r9,%rsi)
movq 0x10(%rdi), %rsi
movl 0x8(%rsi,%rdx), %edx
shrl %edx
movb %al, (%r9,%rdx)
movl %ecx, %edx
incl %ecx
cmpq %rdx, (%rdi)
ja 0xf6484
jmp 0xf6427
movl $0x1, %r8d
jmp 0xf6427