// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xc8, %rsp
movq 0x78(%rbp), %rax
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, -0x68(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq 0x30(%rbp), %rax
movq $0x0, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x15316d
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 0x2724ff(%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 0x152e32
cmpl $0x1, -0x54(%rbp)
jne 0x152e03
movl $0x2, -0x58(%rbp)
cmpl $0x2, -0x54(%rbp)
jne 0x152e10
movl $0x1, -0x58(%rbp)
leaq 0x2724b9(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x58(%rbp), %eax
jne 0x152e30
jmp 0x15316d
jmp 0x152e32
movq -0x90(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x152e6e
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
je 0x152e6e
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x152e73
jmp 0x15316d
movq -0x40(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0xc(%rax), %esi
leaq -0x5c(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0xd3d50
cmpl $0x1, -0x60(%rbp)
jne 0x152e96
jmp 0x15316d
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, -0x74(%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, -0x78(%rbp)
movq -0x28(%rbp), %rdi
movl -0x74(%rbp), %esi
movl -0x78(%rbp), %edx
movl 0x10(%rbp), %ecx
leaq 0x271912(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x18(%rbp), %r8d
leaq -0xa8(%rbp), %r9
callq 0xd0300
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x7c(%rbp)
jge 0x152f0c
jmp 0x15316d
movq -0xa8(%rbp), %rax
shrq $0x20, %rax
movl 0x20(%rbp), %ecx
cmpq %rcx, %rax
je 0x152f24
jmp 0x15316d
jmp 0x152f26
movq 0x30(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x152f94
movq 0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x152f56
movq 0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb0(%rbp)
jmp 0x152f64
movl $0x2, %eax
movq %rax, -0xb0(%rbp)
jmp 0x152f64
movq -0xb0(%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 -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movq 0x58(%rbp), %rax
movq -0xa8(%rbp), %rcx
shrq $0x21, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0x153006
movq 0x58(%rbp), %rax
movl -0xa0(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
cmpb $0x0, (%rax,%rcx)
je 0x15300b
jmp 0x15316d
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 -0x70(%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, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jbe 0x15311d
movl -0x6c(%rbp), %ecx
movq 0x38(%rbp), %rax
movl %ecx, (%rax)
movl -0x70(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x68(%rbp), %ecx
movq 0x48(%rbp), %rax
movl %ecx, (%rax)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq 0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x153111
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x64(%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 -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x1530b8
movl $0x1, -0x1c(%rbp)
jmp 0x1531d4
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), %ebx
movl 0x20(%rbp), %r11d
movl 0x70(%rbp), %r10d
movq 0x78(%rbp), %rax
movl %ebx, (%rsp)
movl %r11d, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x151af0
movq %rax, -0x28(%rbp)
jmp 0x152d87
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq 0x30(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1531cd
movq 0x58(%rbp), %rax
movq 0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x64(%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 -0x64(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x153174
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)