// File: /workspace/github_cpp_makefile_star2_O1/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
subq $0x58, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movl 0x98(%rsp), %eax
movl %eax, %ecx
shrl %ecx
leaq 0x198be6(%rip), %r8     # 0x28b230
movq 0x28(%r8), %rbx
movb (%rbx,%rcx), %r10b
xorl %r8d, %r8d
cmpb $0x1, %r10b
sete %r8b
leal (%r8,%r8,2), %r8d
decl %r8d
cmpb $0x2, %r10b
movl $0x1, %r12d
cmovnel %r8d, %r12d
leaq (%rcx,%rcx,2), %rcx
movl 0xc(%rdx,%rcx,8), %ebp
testq %rbp, %rbp
je 0xf285f
movq $0x0, 0x28(%rsp)
movl 0xa0(%rsp), %r14d
leaq (%rdx,%rcx,8), %rcx
movq 0x18(%rdi), %rdx
movq %rdx, 0x38(%rsp)
movq (%rcx), %r13
leaq 0x1980ad(%rip), %rcx    # 0x28a758
movss 0x158(%rcx), %xmm0
movl %eax, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x8(%rsp), %r10
movl 0xc(%r13), %r8d
leaq (%r8,%r8,2), %rcx
movl (%r15,%rcx,4), %eax
testl %eax, %eax
jns 0xf2839
cmpq $0x0, 0x18(%r13)
jns 0xf2839
movl 0x4(%r13), %edx
testl %r14d, %r14d
jne 0xf26fe
movzbl (%rbx,%r8), %edi
cmpl %edi, %r12d
je 0xf2839
movq (%r10), %r11
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpq %r8, %r11
jbe 0xf2728
movq 0x8(%r10), %r10
movl (%r10,%r8,4), %r8d
cmpl $-0x1, %r8d
je 0xf2728
movl %r8d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
shrl $0x1f, %r8d
jmp 0xf272b
xorl %r8d, %r8d
cmpl $-0x1, %edi
sete %r10b
orb %r8b, %r10b
jne 0xf2834
movq 0x20(%rsi), %r8
movl %edi, %r10d
cmpl $0x0, (%r8,%r10,8)
js 0xf2834
movb $0x1, %r8b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
cmpq %r10, %r11
jbe 0xf277d
movq 0x8(%rsp), %r11
movq 0x8(%r11), %r11
movl (%r11,%r10,4), %r10d
cmpl $-0x1, %r10d
je 0xf277d
movl %r10d, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
testl %r10d, %r10d
setns %r8b
cmpl $-0x1, %edi
sete %r10b
orb %r8b, %r10b
jne 0xf2834
movq 0x38(%rsp), %r8
movq 0x20(%r8), %r8
movl %edi, %edi
cmpl $0x0, (%r8,%rdi,8)
js 0xf2834
movq %rsi, 0x20(%rsp)
leaq (%rdx,%rdx,2), %rdx
leaq (%r15,%rdx,4), %rdx
leaq (%r15,%rcx,4), %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
andl %edi, %eax
subl %eax, %edx
movq %r13, %rdi
movl %r9d, 0x14(%rsp)
movl %r9d, %ecx
movss %xmm0, 0x10(%rsp)
movl 0x90(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xf2848
movq 0x30(%rsp), %rax
cmpl %eax, 0x44(%rsp)
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %r10
jne 0xf282d
movl 0x4c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x28(%rsp), %rcx
cmpl %ecx, %eax
cmoval %eax, %ecx
movq %rcx, 0x28(%rsp)
movq 0x18(%rsp), %rax
cmovaq %r13, %rax
movq %rax, 0x18(%rsp)
movl 0x14(%rsp), %r9d
jmp 0xf2857
movq 0x8(%rsp), %r10
addq $0x28, %r13
decq %rbp
jne 0xf26c8
jmp 0xf2868
movl 0x14(%rsp), %r9d
movq 0x20(%rsp), %rsi
movq 0x8(%rsp), %r10
movss 0x10(%rsp), %xmm0
jmp 0xf2839
movq $0x0, 0x18(%rsp)
movq 0x18(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq