// File: /workspace/github_cpp_makefile_star2_O3/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
movl %r9d, -0x14(%rsp)
movl 0x40(%rsp), %eax
movl %eax, %edi
shrl %edi
leaq 0x1a1505(%rip), %r9     # 0x296230
movq 0x28(%r9), %r15
movb (%r15,%rdi), %r10b
xorl %r9d, %r9d
cmpb $0x1, %r10b
sete %r9b
leal (%r9,%r9,2), %r9d
decl %r9d
cmpb $0x2, %r10b
movl $0x1, %r10d
cmovnel %r9d, %r10d
leaq (%rdi,%rdi,2), %rdi
movl 0xc(%rdx,%rdi,8), %r11d
xorl %r12d, %r12d
testq %r11, %r11
je 0xf4fa3
movl 0x48(%rsp), %ebp
leaq (%rdx,%rdi,8), %rdx
movq (%rdx), %rdx
leaq 0x1a09de(%rip), %rdi    # 0x295758
movss 0x158(%rdi), %xmm0
movl %eax, %eax
movq %rax, -0x8(%rsp)
movq $0x0, -0x10(%rsp)
movl 0xc(%rdx), %r9d
leaq (%r9,%r9,2), %r14
movl (%rcx,%r14,4), %eax
testl %eax, %eax
jns 0xf4ed6
movq 0x18(%rdx), %rdi
testq %rdi, %rdi
jns 0xf4ed6
movq 0x20(%rsi), %rbx
cmpl $0x0, (%rbx,%r9,8)
jns 0xf4ed6
movq (%rdx), %r13
testl %ebp, %ebp
jne 0xf4dd7
movzbl (%r15,%r9), %ebx
cmpl %ebx, %r10d
je 0xf4ed6
cmpq %r9, (%r8)
jbe 0xf4deb
movq 0x8(%r8), %rbx
cmpl $-0x1, (%rbx,%r9,4)
jl 0xf4ed6
movq %r12, -0x38(%rsp)
movq %r8, -0x30(%rsp)
leaq (%rcx,%r14,4), %r8
movl 0x4(%r8), %r14d
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %eax
subl %eax, %r14d
testl %edi, %edi
movq %r15, -0x20(%rsp)
js 0xf4e27
movl 0x10(%rdx), %eax
movl 0x14(%rdx), %ebp
subl %ebp, %r14d
movl %r14d, %r12d
movl %eax, -0x24(%rsp)
movl %eax, %r14d
jmp 0xf4e36
movl 0x10(%rdx), %r12d
movl 0x14(%rdx), %ebp
subl %ebp, %r14d
movl %r12d, -0x24(%rsp)
movl -0x14(%rsp), %r8d
movq %r13, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,4), %rdi
movl 0x4(%rdi), %eax
movl (%rdi), %edi
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %edi
subl %edi, %eax
cmpl %r13d, %r14d
movl %r13d, %edi
cmovll %r14d, %edi
movl 0x8(%rdx), %ebx
movl %eax, %r15d
subl %ebx, %r15d
cmpl %r12d, %r15d
movl %r12d, %r9d
cmovll %r15d, %r9d
cmpl %r8d, %edi
jg 0xf4ec3
cmpl %r8d, %r9d
jg 0xf4ec3
movl %ebx, %r8d
subl %r13d, %r8d
cvtsi2ss %r8, %xmm1
addl %edi, %r8d
addl %r9d, %r8d
cvtsi2ss %r8, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
ja 0xf4ec3
subl -0x24(%rsp), %ebp
xorps %xmm1, %xmm1
cvtsi2ss %rbp, %xmm1
addl %edi, %r9d
addl %ebp, %r9d
xorps %xmm2, %xmm2
cvtsi2ss %r9, %xmm2
mulss %xmm0, %xmm2
ucomiss %xmm1, %xmm2
jbe 0xf4ee8
movq -0x30(%rsp), %r8
movq -0x20(%rsp), %r15
movq -0x38(%rsp), %r12
movl 0x48(%rsp), %ebp
addq $0x28, %rdx
decq %r11
jne 0xf4d92
jmp 0xf4fac
cmpl %r12d, %r15d
setl -0x24(%rsp)
setg %r15b
cmpl %r14d, %r13d
setg %dil
setl %bpl
orb %r15b, %dil
cmpb $0x1, %dil
jne 0xf4ec3
orb -0x24(%rsp), %bpl
movq -0x20(%rsp), %r15
je 0xf4f99
movl 0x38(%rsp), %edi
cmpl %edi, %r8d
movl 0x48(%rsp), %ebp
jl 0xf4f8a
cmpl %edi, %r9d
jl 0xf4f8a
addl %ebx, %r12d
movl %eax, %ebx
subl %r12d, %ebx
xorl %r8d, %r8d
movl %r13d, %edi
subl %r14d, %edi
setle %r9b
cmovlel %ebx, %edi
testl %edi, %edi
js 0xf4f8a
movb %r9b, %r8b
shrq $0x1f, %r13
andl $-0x2, %r13d
orq %r8, %r13
cmpl -0x8(%rsp), %r13d
movq -0x30(%rsp), %r8
movq -0x38(%rsp), %r12
jne 0xf4ed6
subl %edi, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl %r12d, %eax
movq -0x10(%rsp), %rdi
cmovaq %rdx, %rdi
movq %rdi, -0x10(%rsp)
cmoval %eax, %r12d
jmp 0xf4ed6
movq -0x30(%rsp), %r8
movq -0x38(%rsp), %r12
jmp 0xf4ed6
movq -0x30(%rsp), %r8
jmp 0xf4ecd
movq $0x0, -0x10(%rsp)
movq -0x10(%rsp), %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq