// File: /workspace/github_cpp_makefile_star2_O3/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
movl 0x40(%rsp), %eax
movl %eax, %r10d
shrl %r10d
leaq (%r10,%r10,2), %r11
movl 0xc(%rdx,%r11,8), %r10d
movq $0x0, -0x18(%rsp)
testq %r10, %r10
je 0x1005c0
movl %r9d, -0x1c(%rsp)
movl 0x38(%rsp), %r12d
leaq (%rdx,%r11,8), %rdx
movq 0x18(%rdi), %rdi
movq %rdi, -0x8(%rsp)
movq (%rdx), %rdi
leaq 0x1953a4(%rip), %rdx    # 0x295758
movss 0x158(%rdx), %xmm0
movl %eax, %eax
movq %rax, -0x10(%rsp)
movq $0x0, -0x28(%rsp)
movq (%rdi), %r15
movq %r15, %r14
shrq $0x20, %r14
leaq (%r14,%r14,2), %r13
movl (%rcx,%r13,4), %ebx
testl %ebx, %ebx
js 0x100514
movl 0xc(%rdi), %r11d
leaq (%r11,%r11,2), %rax
leaq (%rcx,%rax,4), %rdx
movl (%rdx), %eax
testl %eax, %eax
js 0x100514
movq 0x18(%rdi), %rbp
testq %rbp, %rbp
js 0x100514
movl %r12d, %r9d
movq 0x20(%rsi), %r12
cmpl $0x0, (%r12,%r14,8)
js 0x100511
cmpl $0x0, (%r12,%r11,8)
js 0x100511
cmpq %r11, (%r8)
movl %r9d, %r12d
jbe 0x100514
movq 0x8(%r8), %r14
movl (%r14,%r11,4), %r11d
cmpl $-0x2, %r11d
jg 0x100514
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
cmpl 0x48(%rsp), %r11d
jne 0x100514
movq -0x8(%rsp), %r14
movq 0x20(%r14), %r14
cmpl $0x0, (%r14,%r11,8)
js 0x100514
movl 0x4(%rdx), %r12d
subl %eax, %r12d
testl %ebp, %ebp
js 0x10048b
movl 0x10(%rdi), %eax
movl 0x14(%rdi), %r14d
subl %r14d, %r12d
movl %r12d, %edx
movl %eax, -0x30(%rsp)
movl %eax, %r12d
jmp 0x100499
movl 0x10(%rdi), %edx
movl 0x14(%rdi), %r14d
subl %r14d, %r12d
movl %edx, -0x30(%rsp)
leaq (%rcx,%r13,4), %rax
movl 0x4(%rax), %ebp
subl %ebx, %ebp
cmpl %r15d, %r12d
movl %r15d, %eax
cmovll %r12d, %eax
movl 0x8(%rdi), %ebx
movl %ebp, %r13d
subl %ebx, %r13d
cmpl %edx, %r13d
movl %edx, -0x2c(%rsp)
movl %edx, %r11d
cmovll %r13d, %r11d
movl -0x1c(%rsp), %edx
cmpl %edx, %eax
jg 0x100511
cmpl %edx, %r11d
jg 0x100511
movl %ebx, %edx
subl %r15d, %edx
cvtsi2ss %rdx, %xmm1
addl %eax, %edx
addl %r11d, %edx
cvtsi2ss %rdx, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0x100511
subl -0x30(%rsp), %r14d
xorps %xmm1, %xmm1
cvtsi2ss %r14, %xmm1
addl %eax, %r11d
addl %r14d, %r11d
xorps %xmm2, %xmm2
cvtsi2ss %r11, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
jbe 0x100526
movl %r9d, %r12d
addq $0x28, %rdi
decq %r10
jne 0x1003cc
jmp 0x1005c9
cmpl -0x2c(%rsp), %r13d
setl -0x30(%rsp)
setg %r13b
cmpl %r12d, %r15d
setg %al
setl %r14b
orb %r13b, %al
cmpb $0x1, %al
jne 0x100511
orb -0x30(%rsp), %r14b
je 0x100511
movl %r9d, %eax
cmpl %r9d, %edx
jl 0x1005b8
cmpl %eax, %r11d
jl 0x100511
movl -0x2c(%rsp), %eax
addl %ebx, %eax
movl %ebp, %ebx
subl %eax, %ebx
xorl %edx, %edx
movl %r15d, %eax
subl %r12d, %eax
setle %r11b
cmovlel %ebx, %eax
testl %eax, %eax
js 0x100511
movb %r11b, %dl
shrq $0x1f, %r15
andl $-0x2, %r15d
orq %rdx, %r15
cmpl -0x10(%rsp), %r15d
movl %r9d, %r12d
jne 0x100514
subl %eax, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
movq -0x18(%rsp), %rax
cmpl %eax, %ebp
movq -0x28(%rsp), %rdx
cmovaq %rdi, %rdx
movq %rdx, -0x28(%rsp)
cmoval %ebp, %eax
movq %rax, -0x18(%rsp)
jmp 0x100514
movl %eax, %r12d
jmp 0x100514
movq $0x0, -0x28(%rsp)
movq -0x28(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq