// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl 0x18(%rbp), %eax
shrl %eax
movl %eax, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl -0x30(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x98(%rbp)
leaq 0x273a1a(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x30(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x9c(%rbp)
movl $0xffffffff, -0xa0(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x20(%rbp)
jne 0x1518ff
cmpl $0x1, -0x9c(%rbp)
jne 0x1518ea
movl $0x2, -0xa0(%rbp)
cmpl $0x2, -0x9c(%rbp)
jne 0x1518fd
movl $0x1, -0xa0(%rbp)
jmp 0x1518ff
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movq -0x78(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x151ae3
movq -0x78(%rbp), %rax
movq (%rax), %rax
movl -0x7c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x151970
jmp 0x151ad5
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
shrq $0x3f, %rax
cmpl $0x0, %eax
jne 0x151986
jmp 0x151ad5
leaq 0x273943(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0xa0(%rbp), %eax
jne 0x1519a9
jmp 0x151ad5
movq -0x28(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0xc(%rax), %esi
leaq -0x84(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x84(%rbp)
je 0x1519d3
cmpl $0x1, -0x80(%rbp)
jne 0x1519d8
jmp 0x151ad5
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x84(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x1519f6
jmp 0x151ad5
movq -0x28(%rbp), %rdi
movl -0x84(%rbp), %esi
leaq -0x88(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x88(%rbp)
je 0x151a1f
cmpl $0x1, -0x80(%rbp)
je 0x151a24
jmp 0x151ad5
movq -0x98(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x88(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x151a45
jmp 0x151ad5
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x68(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %esi
movq -0x70(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x70(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movl -0x2c(%rbp), %ecx
leaq 0x272d7d(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x10(%rbp), %r8d
leaq -0x50(%rbp), %r9
callq 0xd0300
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x151a9b
jmp 0x151ad5
movq -0x50(%rbp), %rax
shrq $0x20, %rax
movl 0x18(%rbp), %ecx
cmpq %rcx, %rax
je 0x151aad
jmp 0x151ad5
movl -0x44(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl -0x8c(%rbp), %eax
jbe 0x151ad3
movl -0x44(%rbp), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x8c(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x151ad5
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x151906
movq -0x60(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq