// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_chimeric_reads_chain(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_t_u32_warp*, kvec_asg_arc_t_warp*, unsigned int*, unsigned int*, unsigned int*, unsigned int, unsigned int, unsigned char*)
int get_chimeric_reads_chain(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_t_u32_warp* chain_buffer, kvec_asg_arc_t_warp* chain_edges, uint32_t* return_ava_cur,
uint32_t* return_ava_ol, uint32_t* return_chainLen, 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;
    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_buffer->a.n = 0; kv_push(uint32_t, chain_buffer->a, uId);
    if(chain_edges) 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) || (!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;

        if(chain_edges) kv_push(asg_arc_t, chain_edges->a, t);
        chainLen++;
        ///endRid is (t.ul>>32), and t.v is a contained read
        ///find edges from t.v to existing unitigs
        ///chain_buffer->a.n always be 1
        ava_cur = get_num_edges2existing_nodes_advance(ug, rg, sources, coverage_cut, ruIndex, max_hang, min_ovlp, t.v, &test_oLen, chain_buffer->a.a, chain_buffer->a.n, 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;
            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);
    }

    return 0;    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, (%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movq 0xb0(%rsp), %rcx
movq 0xa8(%rsp), %rdx
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %r13
movq 0x90(%rsp), %r14
movl 0x88(%rsp), %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rdx)
movl %eax, (%rsi)
movl %eax, (%rcx)
movq $0x0, (%r14)
cmpq $0x0, 0x8(%r14)
jne 0xf2e94
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movl %ebx, (%rcx,%rax,4)
testq %r13, %r13
je 0xf2eb2
movq $0x0, (%r13)
xorl %eax, %eax
testq %rbp, %rbp
je 0xf30b3
movl 0xc0(%rsp), %ebx
movl 0xb8(%rsp), %eax
movl 0x80(%rsp), %r14d
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, 0x8(%rsp)
movl $0x1, %r13d
movq (%rbp), %rax
shrq $0x20, %rax
movl 0xc(%rbp), %ecx
testl %ebx, %ebx
jne 0xf2f28
leaq 0x198337(%rip), %rdx    # 0x28b230
movq 0x28(%rdx), %rdx
movb (%rdx,%rax), %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,%rcx), %edx
cmpl %edx, %edi
je 0xf30b1
leaq (%rcx,%rcx,2), %rdx
leaq (%r12,%rdx,4), %rdx
movl (%rdx), %edi
testl %edi, %edi
jns 0xf30b1
cmpq $0x0, 0x18(%rbp)
jns 0xf30b1
movq (%rsp), %rsi
movq 0x20(%rsi), %rsi
cmpl $0x0, (%rsi,%rcx,8)
jns 0xf30b1
cmpq %rcx, (%r15)
jbe 0xf2f6a
movq 0x8(%r15), %rsi
cmpl $-0x1, (%rsi,%rcx,4)
jl 0xf30b1
leaq (%rax,%rax,2), %rax
leaq (%r12,%rax,4), %rax
movl 0x4(%rax), %esi
movl (%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
subl %eax, %esi
movl 0x4(%rdx), %edx
andl %ecx, %edi
subl %edi, %edx
leaq 0x1977ca(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movq %rbp, %rdi
movl 0x70(%rsp), %ecx
movl 0x78(%rsp), %r8d
leaq 0x20(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xf30b1
cmpl %r14d, 0x24(%rsp)
jne 0xf30b1
movq 0x98(%rsp), %r14
testq %r14, %r14
movq 0x90(%rsp), %rbp
je 0xf302b
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0xf3006
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
leaq (%rax,%rax,2), %rax
movq 0x30(%rsp), %rdx
movq %rdx, 0x10(%rcx,%rax,8)
movups 0x20(%rsp), %xmm0
movups %xmm0, (%rcx,%rax,8)
movl 0x28(%rsp), %r14d
movl (%rbp), %eax
movq 0x18(%rsp), %rdi
movq (%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
movl 0x70(%rsp), %r9d
pushq %rbx
pushq %rax
pushq 0x10(%rbp)
leaq 0x24(%rsp), %rax
pushq %rax
pushq %r14
movl 0xa0(%rsp), %eax
pushq %rax
callq 0x102777
addq $0x30, %rsp
testl %eax, %eax
jne 0xf30c2
cmpl %r13d, 0x8(%rsp)
je 0xf30b1
subq $0x8, %rsp
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r12, %rcx
movq %r15, %r8
movl 0x78(%rsp), %r9d
pushq %rbx
pushq %r14
movl 0x90(%rsp), %eax
pushq %rax
callq 0xf287c
addq $0x20, %rsp
movq %rax, %rbp
incl %r13d
testq %rax, %rax
jne 0xf2ee3
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa0(%rsp), %rcx
movl %eax, (%rcx)
movl 0xc(%rsp), %eax
movq 0xa8(%rsp), %rcx
movl %eax, (%rcx)
movq 0xb0(%rsp), %rax
movl %r13d, (%rax)
movl $0x1, %eax
jmp 0xf30b3