// File: /workspace/github_cpp_makefile_star2_O2/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 %rcx, %r15
movl 0x98(%rsp), %eax
movl 0xa0(%rsp), %ebx
shrl %eax
imulq $0x18, %rax, %rcx
leaq (%rdx,%rcx), %r12
movq 0x18(%rdi), %rdi
movq %rdi, 0x30(%rsp)
leaq 0x19ec6c(%rip), %rdi    # 0x254230
movq 0x28(%rdi), %rdi
movq %rdi, 0x38(%rsp)
movb (%rdi,%rax), %al
xorl %edi, %edi
cmpb $0x1, %al
sete %dil
leal (%rdi,%rdi,2), %edi
decl %edi
cmpb $0x2, %al
pushq $0x1
popq %rax
cmovnel %edi, %eax
movl %eax, 0x1c(%rsp)
movl 0xc(%rdx,%rcx), %eax
leaq 0x19e164(%rip), %rcx    # 0x253758
movss 0x158(%rcx), %xmm0
imulq $0x28, %rax, %r14
xorl %ebp, %ebp
movl $0x0, 0xc(%rsp)
movq $0x0, 0x10(%rsp)
cmpq %rbp, %r14
je 0xb5771
movq (%r12), %r13
movl 0xc(%r13,%rbp), %r11d
imulq $0xc, %r11, %rcx
movl (%r15,%rcx), %eax
testl %eax, %eax
jns 0xb5768
cmpq $0x0, 0x18(%r13,%rbp)
jns 0xb5768
movl 0x4(%r13,%rbp), %edx
testl %ebx, %ebx
jne 0xb565e
movq 0x38(%rsp), %rdi
movzbl (%rdi,%r11), %edi
cmpl %edi, 0x1c(%rsp)
je 0xb5768
movq (%r8), %r10
cmpq %r11, %r10
jbe 0xb5768
movq 0x8(%r8), %rdi
movslq (%rdi,%r11,4), %r11
testq %r11, %r11
js 0xb5768
cmpq %r11, %r10
jbe 0xb5768
movq 0x20(%rsi), %r10
cmpl $0x0, (%r10,%r11,8)
js 0xb5768
movl (%rdi,%r11,4), %r10d
cmpl $-0x2, %r10d
jg 0xb5768
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x30(%rsp), %rdi
movq 0x20(%rdi), %rdi
cmpl $0x0, (%rdi,%r10,8)
js 0xb5768
movq %rsi, 0x20(%rsp)
movq %r8, 0x28(%rsp)
addq %rbp, %r13
imulq $0xc, %rdx, %rdx
addq %r15, %rdx
addq %r15, %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, 0x8(%rsp)
movl %r9d, %ecx
movss %xmm0, 0x4(%rsp)
movl 0x90(%rsp), %r8d
leaq 0x40(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xb5753
movl 0x98(%rsp), %eax
cmpl %eax, 0x44(%rsp)
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %rsi
jne 0xb574c
movl 0x4c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
cmoval %eax, %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%rsp), %rax
cmovaq %r13, %rax
movq %rax, 0x10(%rsp)
movl 0x8(%rsp), %r9d
jmp 0xb5762
movl 0x8(%rsp), %r9d
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %rsi
movss 0x4(%rsp), %xmm0
addq $0x28, %rbp
jmp 0xb5613
movq 0x10(%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq