// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rsi, %r12
movl 0x88(%rsp), %eax
shrl %eax
imulq $0x18, %rax, %rax
leaq (%rdx,%rax), %rbx
movq 0x18(%rdi), %rcx
movq %rcx, 0x28(%rsp)
movl 0xc(%rdx,%rax), %eax
leaq 0x19593b(%rip), %rcx    # 0x253758
movss 0x158(%rcx), %xmm0
imulq $0x28, %rax, %rbp
xorl %r14d, %r14d
movq $0x0, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
cmpq %r14, %rbp
je 0xbdf74
movq (%rbx), %r13
movq (%r13,%r14), %r11
shrq $0x20, %r11
imulq $0xc, %r11, %rcx
movl (%r15,%rcx), %eax
testl %eax, %eax
js 0xbdf6b
movl 0xc(%r13,%r14), %esi
imulq $0xc, %rsi, %rdx
addq %r15, %rdx
movl (%rdx), %edi
testl %edi, %edi
js 0xbdf6b
cmpq $0x0, 0x18(%r13,%r14)
js 0xbdf6b
movq 0x20(%r12), %r10
cmpl $0x0, (%r10,%r11,8)
js 0xbdf6b
cmpl $0x0, (%r10,%rsi,8)
js 0xbdf6b
cmpq %rsi, (%r8)
jbe 0xbdf6b
movq 0x8(%r8), %r10
movl (%r10,%rsi,4), %esi
cmpl $-0x2, %esi
jg 0xbdf6b
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
cmpl 0x90(%rsp), %esi
jne 0xbdf6b
movq 0x28(%rsp), %r10
movq 0x20(%r10), %r10
cmpl $0x0, (%r10,%rsi,8)
js 0xbdf6b
movq %r8, 0x20(%rsp)
addq %r14, %r13
addq %r15, %rcx
movl 0x4(%rcx), %esi
subl %eax, %esi
movl 0x4(%rdx), %edx
subl %edi, %edx
movq %r13, %rdi
movl %r9d, 0x10(%rsp)
movl %r9d, %ecx
movss %xmm0, 0xc(%rsp)
movl 0x80(%rsp), %r8d
leaq 0x30(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xbdf5b
movl 0x88(%rsp), %eax
cmpl %eax, 0x34(%rsp)
movq 0x20(%rsp), %r8
jne 0xbdf54
movl 0x3c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl 0x14(%rsp), %edx
cmpl %edx, %eax
movq 0x18(%rsp), %rcx
cmovaq %r13, %rcx
movq %rcx, 0x18(%rsp)
cmoval %eax, %edx
movl %edx, 0x14(%rsp)
movl 0x10(%rsp), %r9d
jmp 0xbdf65
movl 0x10(%rsp), %r9d
movq 0x20(%rsp), %r8
movss 0xc(%rsp), %xmm0
addq $0x28, %r14
jmp 0xbde3d
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq