// File: /workspace/github_cpp_makefile_star2_O2/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 0x90(%rsp), %r10d
movl 0x80(%rsp), %r11d
shrl %eax
imulq $0x18, %rax, %rcx
leaq (%rdx,%rcx), %rbx
leaq 0x19ea6f(%rip), %rsi    # 0x254230
movq 0x28(%rsi), %rsi
movq %rsi, 0x28(%rsp)
movb (%rsi,%rax), %al
xorl %esi, %esi
cmpb $0x1, %al
sete %sil
leal (%rsi,%rsi,2), %esi
decl %esi
cmpb $0x2, %al
pushq $0x1
popq %rax
cmovnel %esi, %eax
movl %eax, 0x24(%rsp)
movl 0xc(%rdx,%rcx), %eax
leaq 0x19df67(%rip), %rcx    # 0x253758
movss 0x158(%rcx), %xmm0
imulq $0x28, %rax, %r14
xorl %ebp, %ebp
movq $0x0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
cmpq %rbp, %r14
je 0xb5921
movq (%rbx), %r13
movl 0xc(%r13,%rbp), %edx
imulq $0xc, %rdx, %rcx
movl (%r15,%rcx), %eax
testl %eax, %eax
jns 0xb5918
cmpq $0x0, 0x18(%r13,%rbp)
jns 0xb5918
movq 0x20(%r12), %rsi
cmpl $0x0, (%rsi,%rdx,8)
jns 0xb5918
movl 0x4(%r13,%rbp), %esi
testl %r10d, %r10d
jne 0xb5869
movq 0x28(%rsp), %rdi
movzbl (%rdi,%rdx), %edi
cmpl %edi, 0x24(%rsp)
je 0xb5918
cmpq %rdx, (%r8)
jbe 0xb587c
movq 0x8(%r8), %rdi
cmpl $-0x1, (%rdi,%rdx,4)
jl 0xb5918
movq %r8, 0x10(%rsp)
addq %rbp, %r13
imulq $0xc, %rsi, %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 %r11d, %r8d
leaq 0x30(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xb58f8
movl 0x88(%rsp), %eax
cmpl %eax, 0x34(%rsp)
jne 0xb58f8
movl 0x3c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movl 0xc(%rsp), %edx
cmpl %edx, %eax
movq 0x18(%rsp), %rcx
cmovaq %r13, %rcx
movq %rcx, 0x18(%rsp)
cmoval %eax, %edx
movl %edx, 0xc(%rsp)
movl 0x8(%rsp), %r9d
movq 0x10(%rsp), %r8
movl 0x90(%rsp), %r10d
movl 0x80(%rsp), %r11d
movss 0x4(%rsp), %xmm0
addq $0x28, %rbp
jmp 0xb5810
movq 0x18(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq