// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_best_contained_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_contained_read(ma_ug_t *ug, asg_t *r_g, ma_hit_t_alloc* sources, 
ma_sub_t *coverage_cut, R_to_U* ruIndex, int max_hang, int min_ovlp, uint32_t query,
uint32_t ignore_trio_flag)
{
    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 = &(sources[qn]);
    uint32_t i, is_Unitig, contain_rId, contain_uId;
    uint32_t maxOlen = 0;
    asg_t* nsg = ug->g;

    uint32_t trio_flag = R_INF.trio_flag[qn], 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;
    }

    //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)]);
        ///just need deleted edges
        ///sq might be deleted or not
        if(!st->del) continue;
        if(!h->del) continue;
        if(R_INF.trio_flag[Get_tn(*h)] == non_trio_flag) continue;

        ///tn must be contained in another existing read
        get_R_to_U(ruIndex, Get_tn(*h), &contain_rId, &is_Unitig);
        if(contain_rId == (uint32_t)-1 || is_Unitig == 1) continue;
        if(r_g->seq[contain_rId].del) continue;

        get_R_to_U(ruIndex, contain_rId, &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

        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
movq %rsi, -0x8(%rsp)
movl 0x40(%rsp), %eax
movl %eax, %esi
shrl %esi
leaq 0x1a17fe(%rip), %r10    # 0x296230
movq 0x28(%r10), %r14
movb (%r14,%rsi), %r10b
xorl %r11d, %r11d
cmpb $0x1, %r10b
sete %r11b
leal (%r11,%r11,2), %ebx
decl %ebx
cmpb $0x2, %r10b
movl $0x1, %r11d
cmovnel %ebx, %r11d
leaq (%rsi,%rsi,2), %rsi
movl 0xc(%rdx,%rsi,8), %ebx
movq $0x0, -0x20(%rsp)
testq %rbx, %rbx
je 0xf4d00
movl 0x48(%rsp), %r10d
leaq (%rdx,%rsi,8), %rdx
movl %r10d, %esi
movq 0x18(%rdi), %rdi
movq %rdi, -0x10(%rsp)
movq (%rdx), %rdi
leaq 0x1a0cc6(%rip), %rdx    # 0x295758
movss 0x158(%rdx), %xmm0
movl %eax, %eax
movq %rax, -0x18(%rsp)
xorl %eax, %eax
movq %r8, -0x40(%rsp)
movq %r14, -0x48(%rsp)
movl 0xc(%rdi), %r10d
leaq (%r10,%r10,2), %r13
movl (%rcx,%r13,4), %edx
testl %edx, %edx
jns 0xf4c49
movq 0x18(%rdi), %r12
testq %r12, %r12
jns 0xf4c49
movq (%rdi), %r15
testl %esi, %esi
jne 0xf4ae3
movzbl (%r14,%r10), %ebp
cmpl %ebp, %r11d
je 0xf4c49
movq (%r8), %rbp
cmpq %r10, %rbp
jbe 0xf4c49
movl %r9d, %esi
movq 0x8(%r8), %r14
movslq (%r14,%r10,4), %r9
testq %r9, %r9
js 0xf4c38
cmpq %r9, %rbp
jbe 0xf4c38
movq -0x8(%rsp), %r8
movq 0x20(%r8), %rbp
cmpl $0x0, (%rbp,%r9,8)
js 0xf4c38
movl (%r14,%r9,4), %r9d
cmpl $-0x2, %r9d
jg 0xf4c38
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movq -0x10(%rsp), %r8
movq 0x20(%r8), %r14
cmpl $0x0, (%r14,%r9,8)
js 0xf4c38
leaq (%rcx,%r13,4), %r9
movl 0x4(%r9), %ebp
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %ebp
testl %r12d, %r12d
js 0xf4b7d
movl 0x10(%rdi), %r8d
movl 0x14(%rdi), %edx
movq %rdx, -0x28(%rsp)
subl %edx, %ebp
movl %ebp, %r9d
movl %r8d, -0x34(%rsp)
movl %r8d, %ebp
jmp 0xf4b90
movl 0x10(%rdi), %r9d
movl 0x14(%rdi), %edx
movq %rdx, -0x28(%rsp)
subl %edx, %ebp
movl %r9d, -0x34(%rsp)
movq %r15, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,4), %rdx
movl 0x4(%rdx), %r12d
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %r12d
cmpl %r15d, %ebp
movl %r15d, %r13d
cmovll %ebp, %r13d
movl 0x8(%rdi), %r8d
movl %r12d, %r14d
subl %r8d, %r14d
cmpl %r9d, %r14d
movl %r9d, -0x38(%rsp)
movl %r9d, %edx
cmovll %r14d, %edx
movl %esi, %r9d
cmpl %esi, %r13d
movl 0x38(%rsp), %r10d
jg 0xf4c3b
cmpl %r9d, %edx
jg 0xf4c38
movl %r12d, -0x2c(%rsp)
movl %r8d, %r12d
subl %r15d, %r12d
cvtsi2ss %r12, %xmm1
addl %r13d, %r12d
addl %edx, %r12d
cvtsi2ss %r12, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf4c38
movl %r8d, -0x30(%rsp)
movq -0x28(%rsp), %r8
subl -0x34(%rsp), %r8d
xorps %xmm1, %xmm1
cvtsi2ss %r8, %xmm1
addl %r13d, %edx
addl %r8d, %edx
xorps %xmm2, %xmm2
cvtsi2ss %rdx, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
jbe 0xf4c5b
movl %esi, %r9d
movq -0x40(%rsp), %r8
movq -0x48(%rsp), %r14
movl 0x48(%rsp), %esi
addq $0x28, %rdi
decq %rbx
jne 0xf4aad
jmp 0xf4d02
cmpl -0x38(%rsp), %r14d
setl %r8b
setg %r13b
cmpl %ebp, %r15d
setg %r14b
setl %r9b
orb %r13b, %r14b
cmpb $0x1, %r14b
jne 0xf4c38
orb %r8b, %r9b
movl %esi, %r9d
movq -0x40(%rsp), %r8
movq -0x48(%rsp), %r14
je 0xf4c45
cmpl %r10d, %r12d
jl 0xf4c45
cmpl %r10d, %edx
jl 0xf4c45
movl -0x38(%rsp), %edx
addl -0x30(%rsp), %edx
movl -0x2c(%rsp), %r8d
movl %r8d, %r10d
subl %edx, %r10d
xorl %r14d, %r14d
movl %r15d, %edx
subl %ebp, %edx
setle %r9b
cmovlel %r10d, %edx
testl %edx, %edx
js 0xf4c38
movb %r9b, %r14b
shrq $0x1f, %r15
andl $-0x2, %r15d
orq %r14, %r15
cmpl -0x18(%rsp), %r15d
jne 0xf4c38
subl %edx, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq -0x20(%rsp), %rdx
cmpl %edx, %r8d
cmoval %r8d, %edx
movq %rdx, -0x20(%rsp)
cmovaq %rdi, %rax
jmp 0xf4c38
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq