// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_best_cmk_read(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int, unsigned int, unsigned int, unsigned char*)
ma_hit_t* get_best_cmk_read(ma_ug_t *ug, asg_t *rg, 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, uint8_t *cmk)
{
    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, maxOlen = 0;
    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) || (!h->del) || (!rg->seq[Get_tn(*h)].del)) continue;
        // if(cmk[Get_tn(*h)] != ((uint8_t)-1)) continue;
        // if(cmk[Get_tn(*h)] == ((uint8_t)-1)) 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(is_Unitig == 1) 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
movl %eax, %ecx
shrl %ecx
leaq 0x19898e(%rip), %rsi    # 0x28b230
movq 0x28(%rsi), %rsi
movq %rsi, 0x28(%rsp)
movb (%rsi,%rcx), %sil
xorl %edi, %edi
cmpb $0x1, %sil
sete %dil
leal (%rdi,%rdi,2), %edi
decl %edi
cmpb $0x2, %sil
movl $0x1, %r14d
cmovnel %edi, %r14d
leaq (%rcx,%rcx,2), %rcx
movl 0xc(%rdx,%rcx,8), %ebp
movq $0x0, 0x18(%rsp)
testq %rbp, %rbp
je 0xf2a09
movl 0x90(%rsp), %r11d
leaq (%rdx,%rcx,8), %rcx
movq (%rcx), %r13
leaq 0x197e5c(%rip), %rcx    # 0x28a758
movss 0x158(%rcx), %xmm0
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %ebx, %ebx
movl 0xc(%r13), %edx
leaq (%rdx,%rdx,2), %rcx
movl (%r15,%rcx,4), %r10d
testl %r10d, %r10d
jns 0xf29fa
cmpq $0x0, 0x18(%r13)
jns 0xf29fa
movq 0x20(%r12), %rsi
cmpl $0x0, (%rsi,%rdx,8)
jns 0xf29fa
movl 0x4(%r13), %esi
testl %r11d, %r11d
jne 0xf2957
movq 0x28(%rsp), %rax
movzbl (%rax,%rdx), %edi
cmpl %edi, %r14d
je 0xf29fa
cmpq %rdx, (%r8)
jbe 0xf296a
movq 0x8(%r8), %rdi
cmpl $-0x1, (%rdi,%rdx,4)
jl 0xf29fa
movq %r8, 0x10(%rsp)
leaq (%rsi,%rsi,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, %r10d
subl %r10d, %edx
movq %r13, %rdi
movl %r9d, %ecx
movl %r9d, 0xc(%rsp)
movss %xmm0, 0x8(%rsp)
movl 0x80(%rsp), %r8d
leaq 0x30(%rsp), %r9
callq 0xa159b
testl %eax, %eax
js 0xf29e2
movq 0x20(%rsp), %rax
cmpl %eax, 0x34(%rsp)
jne 0xf29e2
movl 0x3c(%rsp), %edx
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %edx
movq 0x18(%rsp), %rcx
cmpl %ecx, %edx
cmovaq %r13, %rbx
cmoval %edx, %ecx
movq %rcx, 0x18(%rsp)
movl 0xc(%rsp), %r9d
movq 0x10(%rsp), %r8
movl 0x90(%rsp), %r11d
movss 0x8(%rsp), %xmm0
addq $0x28, %r13
decq %rbp
jne 0xf290d
jmp 0xf2a0b
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq