// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_best_no_coverage_read(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int, unsigned int, unsigned int)
ma_hit_t* get_best_no_coverage_read(ma_ug_t *ug, asg_t *r_g, ma_hit_t_alloc* reverse_sources, 
ma_sub_t *coverage_cut, R_to_U* ruIndex, int max_hang, int min_ovlp, uint32_t query,
uint32_t init_contain_uId)
{
    uint32_t qn = query>>1;
    int32_t r;
    asg_arc_t t;
    ma_hit_t *h = NULL, *return_h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    ma_hit_t_alloc* x = &(reverse_sources[qn]);
    uint32_t i, is_Unitig, contain_uId;
    uint32_t maxOlen = 0;
    asg_t* nsg = ug->g;


    //scan all edges of qn
    for (i = 0; i < x->length; i++)
    {
        h = &(x->buffer[i]);
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);

        ///all of them cannot be removed
        if(sq->del || st->del || h->del) continue;
        if(r_g->seq[Get_qn(*h)].del || r_g->seq[Get_tn(*h)].del) continue;

        get_R_to_U(ruIndex, Get_tn(*h), &contain_uId, &is_Unitig);
        if(contain_uId == (uint32_t)-1 || is_Unitig != 1) continue;
        if(nsg->seq[contain_uId].del) continue;
        ///contain_uId must be a unitig

        if(init_contain_uId != contain_uId) continue;

        r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                asm_opt.max_hang_rate, min_ovlp, &t);
        
        ///if it is a contained read, skip
        if(r < 0) continue;

        if((t.ul>>32) != query) continue;

        if(t.ol > maxOlen)
        {
            maxOlen = t.ol;
            return_h = h;
        }
    }

    return return_h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r15
movl 0x88(%rsp), %eax
movl %eax, %ecx
shrl %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0xc(%rdx,%rcx,8), %ebx
testq %rbx, %rbx
je 0xfc989
movq %r8, %r14
movq %rsi, %r12
movq $0x0, 0x18(%rsp)
movl 0x90(%rsp), %ebp
leaq (%rdx,%rcx,8), %rcx
movq 0x18(%rdi), %rdx
movq %rdx, 0x28(%rsp)
movq (%rcx), %r13
leaq 0x18df15(%rip), %rcx    # 0x28a758
movss 0x158(%rcx), %xmm0
movl %eax, %eax
movq %rax, 0x20(%rsp)
movq $0x0, 0x10(%rsp)
movq (%r13), %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rcx
movl (%r15,%rcx,4), %eax
testl %eax, %eax
js 0xfc97a
movl 0xc(%r13), %r11d
leaq (%r11,%r11,2), %rdx
leaq (%r15,%rdx,4), %rdx
movl (%rdx), %edi
testl %edi, %edi
js 0xfc97a
cmpq $0x0, 0x18(%r13)
js 0xfc97a
movq 0x20(%r12), %r8
cmpl $0x0, (%r8,%rsi,8)
js 0xfc97a
cmpl $0x0, (%r8,%r11,8)
js 0xfc97a
movb $0x1, %r8b
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %r11, (%r14)
jbe 0xfc8da
movq 0x8(%r14), %r10
movl (%r10,%r11,4), %r10d
cmpl $-0x1, %r10d
je 0xfc8da
movl %r10d, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
testl %r10d, %r10d
setns %r8b
cmpl $-0x1, %esi
sete %r10b
orb %r8b, %r10b
jne 0xfc97a
cmpl %ebp, %esi
jne 0xfc97a
movq 0x28(%rsp), %r8
movq 0x20(%r8), %r8
movl %esi, %esi
cmpl $0x0, (%r8,%rsi,8)
js 0xfc97a
leaq (%r15,%rcx,4), %rcx
movl 0x4(%rcx), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %edi, %edx
movq %r13, %rdi
movl %r9d, %ebp
movl %r9d, %ecx
movss %xmm0, 0xc(%rsp)
movl 0x80(%rsp), %r8d
leaq 0x30(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xfc96a
movq 0x20(%rsp), %rax
cmpl %eax, 0x34(%rsp)
jne 0xfc96a
movl 0x3c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x18(%rsp), %rcx
cmpl %ecx, %eax
movq 0x10(%rsp), %rdx
cmovaq %r13, %rdx
movq %rdx, 0x10(%rsp)
cmoval %eax, %ecx
movq %rcx, 0x18(%rsp)
movl %ebp, %r9d
movl 0x90(%rsp), %ebp
movss 0xc(%rsp), %xmm0
addq $0x28, %r13
decq %rbx
jne 0xfc85b
jmp 0xfc992
movq $0x0, 0x10(%rsp)
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq