// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %r9, 0x10(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, 0x28(%rsp)
movq %rdi, %r15
movq 0xc0(%rsp), %rcx
movq 0xb8(%rsp), %rdx
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rbp
movl 0x98(%rsp), %ebx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rdx)
movl %eax, (%rsi)
movl %eax, (%rcx)
movq $0x0, (%rbp)
cmpq $0x0, 0x8(%rbp)
je 0xf55a0
movq 0x10(%rbp), %rax
xorl %ecx, %ecx
jmp 0xf55c6
movq $0x2, 0x8(%rbp)
movq 0x10(%rbp), %rdi
movl $0x8, %esi
callq 0x2370
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rbp)
movq (%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbp)
movl %ebx, (%rax,%rcx,4)
testq %rdi, %rdi
je 0xf55dd
movq $0x0, (%rdi)
xorl %eax, %eax
testq %r15, %r15
je 0xf598e
movl 0xd0(%rsp), %ebp
movl 0xc8(%rsp), %eax
movl 0x90(%rsp), %ecx
movq %rcx, 0x20(%rsp)
cmpl $0x1, %eax
adcl $0x0, %eax
movl %eax, 0x18(%rsp)
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl $0x1, %ebx
movl %eax, 0xc(%rsp)
movq %r12, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movl %ebx, 0x8(%rsp)
movq (%r15), %rbx
movq %rbx, %rdi
shrq $0x20, %rdi
movl 0xc(%r15), %eax
testl %ebp, %ebp
jne 0xf566e
leaq 0x1a0bee(%rip), %rcx    # 0x296230
movq 0x28(%rcx), %rcx
movb (%rcx,%rdi), %dl
xorl %esi, %esi
cmpb $0x1, %dl
sete %sil
leal (%rsi,%rsi,2), %esi
decl %esi
cmpb $0x2, %dl
movl $0x1, %edx
cmovel %edx, %esi
movzbl (%rcx,%rax), %ecx
cmpl %ecx, %esi
je 0xf598c
leaq (%rax,%rax,2), %rcx
leaq (,%rcx,4), %rdx
addq %r13, %rdx
movl (%rdx), %esi
testl %esi, %esi
jns 0xf598c
movq 0x18(%r15), %rcx
testq %rcx, %rcx
jns 0xf598c
movq 0x10(%rsp), %r8
movq 0x20(%r8), %r8
cmpl $0x0, (%r8,%rax,8)
jns 0xf598c
cmpq %rax, (%r12)
jbe 0xf56be
movq 0x8(%r12), %r8
cmpl $-0x1, (%r8,%rax,4)
jl 0xf598c
movl 0x4(%rdx), %edx
andl %r10d, %esi
subl %esi, %edx
testl %ecx, %ecx
js 0xf56dc
movl 0x10(%r15), %r9d
movl 0x14(%r15), %r8d
subl %r8d, %edx
movl %edx, %esi
movl %r9d, %edx
jmp 0xf56ea
movl 0x10(%r15), %r9d
movl 0x14(%r15), %r8d
subl %r8d, %edx
movl %r9d, %esi
movl 0x80(%rsp), %r11d
leaq (%rdi,%rdi,2), %rdi
leaq (,%rdi,4), %rdi
addq %r13, %rdi
movl 0x4(%rdi), %r13d
movl (%rdi), %edi
andl %r10d, %edi
subl %edi, %r13d
cmpl %ebx, %edx
movl %ebx, %r14d
cmovll %edx, %r14d
movl 0x8(%r15), %edi
movl %r13d, %r12d
subl %edi, %r12d
cmpl %esi, %r12d
movl %esi, %r10d
cmovll %r12d, %r10d
cmpl %r11d, %r14d
jg 0xf598c
cmpl %r11d, %r10d
jg 0xf598c
movl %edi, %r11d
subl %ebx, %r11d
cvtsi2ss %r11, %xmm1
addl %r14d, %r11d
addl %r10d, %r11d
cvtsi2ss %r11, %xmm2
leaq 0x19ffff(%rip), %rbp    # 0x295758
movss 0x158(%rbp), %xmm0
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf598c
subl %r9d, %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %r14d, %r10d
addl %r8d, %r10d
xorps %xmm2, %xmm2
cvtsi2ss %r10, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
ja 0xf598c
cmpl %esi, %r12d
setl %r8b
setg %r14b
cmpl %edx, %ebx
setg %r12b
setl %r9b
orb %r14b, %r12b
cmpb $0x1, %r12b
jne 0xf598c
orb %r8b, %r9b
je 0xf598c
movl 0x88(%rsp), %r8d
cmpl %r8d, %r11d
jl 0xf598c
movq %r8, %r11
cmpl %r11d, %r10d
jl 0xf598c
testl %ecx, %ecx
setns %r8b
addl %edi, %esi
movl %r13d, %r9d
subl %esi, %r9d
xorl %esi, %esi
movl %ebx, %r12d
subl %edx, %r12d
setle %dil
setg %dl
cmovlel %r9d, %r12d
testl %r12d, %r12d
js 0xf598c
movb %dil, %sil
shrq $0x1f, %rbx
andl $-0x2, %ebx
orq %rsi, %rbx
movl 0x20(%rsp), %esi
cmpl %esi, %ebx
jne 0xf598c
xorb %dl, %r8b
movzbl %r8b, %edx
leal (%rdx,%rax,2), %r14d
shrl $0x10, %ecx
andl $0x4000, %ecx           # imm = 0x4000
movl 0xc(%rsp), %edx
andl $0x3fff, %edx           # imm = 0x3FFF
orl %ecx, %edx
movzbl 0x21(%r15), %eax
shll $0xf, %eax
orl %edx, %eax
movl %eax, %ebp
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0xf58dd
movb 0x20(%r15), %r15b
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0xf58a8
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
callq 0x2370
movl 0x88(%rsp), %r11d
movq 0xa8(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
shlq $0x20, %rbx
leaq (%r12,%rbx), %rdx
subl %r12d, %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
leaq 0x1(%rcx), %rsi
movq %rsi, (%rdi)
leaq (%rcx,%rcx,2), %rcx
movq %rdx, (%rax,%rcx,8)
movl %r14d, 0x8(%rax,%rcx,8)
movl %r13d, 0xc(%rax,%rcx,8)
movw %bp, 0x10(%rax,%rcx,8)
movb %r15b, 0x12(%rax,%rcx,8)
movl %ebp, 0xc(%rsp)
movq 0xa0(%rsp), %rcx
movl (%rcx), %eax
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %r13
movq %rcx, %r10
movq %r13, %rcx
movq 0x38(%rsp), %r12
movq %r12, %r8
movl 0x80(%rsp), %r9d
movl 0xd0(%rsp), %ebp
pushq %rbp
pushq %rax
pushq 0x10(%r10)
leaq 0x34(%rsp), %rax
pushq %rax
pushq %r14
pushq %r11
callq 0x106a2e
addq $0x30, %rsp
testl %eax, %eax
jne 0xf599d
movl 0x8(%rsp), %ebx
cmpl %ebx, 0x18(%rsp)
je 0xf598c
subq $0x8, %rsp
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r13, %rcx
movq %r12, %r8
movl 0x88(%rsp), %r9d
pushq %rbp
movq %r14, 0x30(%rsp)
pushq %r14
movl 0xa0(%rsp), %eax
pushq %rax
callq 0xf4d0d
addq $0x20, %rsp
movq %rax, %r15
incl %ebx
testq %rax, %rax
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jne 0xf5625
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb0(%rsp), %rcx
movl %eax, (%rcx)
movl 0x1c(%rsp), %eax
movq 0xb8(%rsp), %rcx
movl %eax, (%rcx)
movq 0xc0(%rsp), %rax
movl 0x8(%rsp), %ecx
movl %ecx, (%rax)
movl $0x1, %eax
jmp 0xf598e