// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_edge2existing_node_advance(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int, unsigned int, unsigned int*, unsigned int, unsigned int*, asg_arc_t*, unsigned int)
inline uint32_t get_edge2existing_node_advance(ma_ug_t *ug, asg_t *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* skip_uId, uint32_t skip_uId_n, 
uint32_t* index, asg_arc_t* t, uint32_t ignore_trio_flag)
{
    uint32_t qn = query>>1, uId, is_Unitig, v, w, k;
    int32_t r;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    ma_hit_t_alloc* x = &(sources[qn]);

    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;
    }
    


    for (; (*index) < x->length; (*index)++)
    {
        h = &(x->buffer[(*index)]);
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);
        ///sq has already been removed
        ///st must not be removed
        ///g-seq must not be removed
        if(st->del || g->seq[Get_tn(*h)].del) continue;
        if(R_INF.trio_flag[Get_tn(*h)] == non_trio_flag) 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;
        get_R_to_U(ruIndex, t->v>>1, &uId, &is_Unitig);
        /****************************may have bugs********************************/
        if(uId == (uint32_t)-1 || is_Unitig == 0) continue;
        /****************************may have bugs********************************/
        for (k = 0; k < skip_uId_n; k++)
        {
            if(uId == skip_uId[k]) break;
        }
        if(k != skip_uId_n) continue;
        // if(uId == skip_uId1) continue;
        // if(uId == skip_uId2) continue;
        /****************************may have bugs********************************/
        if(ug->g->seq[uId].del) continue;
        /****************************may have bugs********************************/
        
        if((t->v != ug->u.a[uId].start) 
            && 
           (t->v != ug->u.a[uId].end))
        {
            continue;
        }

        /**********for debug*************/
        if(t->v == ug->u.a[uId].start)
        {
            ///v = uId<<1;
            v = (uId<<1)^1;
        }

        if(t->v == ug->u.a[uId].end)
        {
            ///v = (uId<<1)^1;
            v = uId<<1;
        }

        if(get_real_length(ug->g, v, NULL)==1)
        {
            get_real_length(ug->g, v, &w);
            if(get_real_length(ug->g, w^1, NULL)==1)
            {
                continue;
            }
        }
        /**********for debug*************/

        (*index)++;
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl 0x40(%rbp), %eax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movl 0x18(%rbp), %eax
shrl %eax
movl %eax, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x20(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
leaq 0x25b91c(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x74(%rbp)
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
cmpl $0x0, 0x40(%rbp)
jne 0x1699eb
cmpl $0x1, -0x74(%rbp)
jne 0x1699dc
movl $0x2, -0x78(%rbp)
cmpl $0x2, -0x74(%rbp)
jne 0x1699e9
movl $0x1, -0x78(%rbp)
jmp 0x1699eb
jmp 0x1699ed
movq 0x30(%rbp), %rax
movl (%rax), %eax
movq -0x70(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x169c8d
movq -0x70(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rbp), %rcx
movl (%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
movl %ecx, %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x169a72
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x169a77
jmp 0x169c7d
leaq 0x25b852(%rip), %rax    # 0x3c52d0
movq 0x28(%rax), %rax
movq -0x58(%rbp), %rcx
movl 0xc(%rcx), %ecx
movzbl (%rax,%rcx), %eax
cmpl -0x78(%rbp), %eax
jne 0x169a97
jmp 0x169c7d
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x60(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %esi
movq -0x68(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x68(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
subl %eax, %edx
movl -0x34(%rbp), %ecx
leaq 0x25ad2b(%rip), %rax    # 0x3c47f8
movss 0x158(%rax), %xmm0
movl 0x10(%rbp), %r8d
movq 0x38(%rbp), %r9
callq 0xd0300
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jge 0x169af0
jmp 0x169c7d
movq 0x38(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl 0x18(%rbp), %ecx
cmpq %rcx, %rax
je 0x169b08
jmp 0x169c7d
movq -0x30(%rbp), %rdi
movq 0x38(%rbp), %rax
movl 0x8(%rax), %esi
shrl %esi
leaq -0x3c(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x3c(%rbp)
je 0x169b2e
cmpl $0x0, -0x40(%rbp)
jne 0x169b33
jmp 0x169c7d
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl 0x28(%rbp), %eax
jae 0x169b60
movl -0x3c(%rbp), %eax
movq 0x20(%rbp), %rcx
movl -0x4c(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
jne 0x169b53
jmp 0x169b60
jmp 0x169b55
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x169b3a
movl -0x4c(%rbp), %eax
cmpl 0x28(%rbp), %eax
je 0x169b6d
jmp 0x169c7d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x3c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x169b8c
jmp 0x169c7d
movq 0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
je 0x169bcd
movq 0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
je 0x169bcd
jmp 0x169c7d
movq 0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x4(%rcx), %eax
jne 0x169bf6
movl -0x3c(%rbp), %eax
shll %eax
xorl $0x1, %eax
movl %eax, -0x44(%rbp)
movq 0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x3c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
jne 0x169c1c
movl -0x3c(%rbp), %eax
shll %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x44(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x169c69
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x44(%rbp), %esi
leaq -0x48(%rbp), %rdx
callq 0x166b50
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x48(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x169c67
jmp 0x169c7d
jmp 0x169c69
movq 0x30(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x169c94
movq 0x30(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1699ed
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq