// File: chhylp123[P]hifiasm/Overlaps.cpp
// Function: check_if_diploid_primary_complex(unsigned int, unsigned int, asg_t*, ma_hit_t_alloc*, long long, unsigned int, int, R_to_U*)
long long check_if_diploid_primary_complex(uint32_t v1, uint32_t v2, asg_t *g, 
ma_hit_t_alloc* reverse_sources, long long min_edge_length, uint32_t stops_threshold,
int if_drop, R_to_U* ruIndex)
{
    buf_t b_0, b_1;
    memset(&b_0, 0, sizeof(buf_t));
    memset(&b_1, 0, sizeof(buf_t));

    uint32_t convex1, convex2;
    long long l1, l2, max_stop_Len;

    b_0.b.n = 0;
    b_1.b.n = 0;
    uint32_t flag1 = detect_single_path_with_dels_n_stops(g, v1, &convex1, &l1, 
    &max_stop_Len, &b_0, stops_threshold);
    
    uint32_t flag2 = detect_single_path_with_dels_n_stops(g, v2, &convex2, &l2, 
    &max_stop_Len, &b_1, stops_threshold);
    
    if(flag1 == LOOP || flag2 == LOOP)
    {
        return -1;
    }

    if(flag1 != END_TIPS && flag1 != LONG_TIPS)
    {
        l1--;
        b_0.b.n--;
    }

    if(flag2 != END_TIPS && flag2 != LONG_TIPS)
    {
        l2--;
        b_1.b.n--;
    }


    long long i, j, k;
    i = b_0.b.n; i--;
    j = b_1.b.n; j--;
    while (i>=0 && j>=0)
    {
        if(b_0.b.a[i] == b_1.b.a[j])
        {
            i--;
            j--;
        }
        else
        {
            break;
        }
        
    }
    b_0.b.n = i+1; l1 = i+1;
    b_1.b.n = j+1; l2 = j+1;


    if(l1 <= min_edge_length || l2 <= min_edge_length)
    {
        return -1;
    }

    buf_t* b_min;
    buf_t* b_max;
    if(l1<=l2)
    {
        b_min = &b_0;
        b_max = &b_1;
    }
    else
    {
        b_min = &b_1;
        b_max = &b_0;
    }

    
    double max_count = 0;
    double min_count = 0;
    uint32_t qn, tn, is_Unitig;
    for (i = 0; i < (long long)b_min->b.n; i++)
    {
        qn = b_min->b.a[i];
        for (j = 0; j < (long long)reverse_sources[qn].length; j++)
        {
            tn = Get_tn(reverse_sources[qn].buffer[j]);
            /****************************may have bugs********************************/
            ///if(g->seq[tn].del == 1 || (if_drop == 1 && g->seq[tn].c == ALTER_LABLE)) continue;
            if(g->seq[tn].del == 1)
            {
                get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                if(tn == (uint32_t)-1 || is_Unitig == 1 || g->seq[tn].del == 1) continue;
            }
            /****************************may have bugs********************************/
            min_count++;
            for (k = 0; k < (long long)b_max->b.n; k++)
            {
                if(b_max->b.a[k]==tn)
                {
                    max_count++;
                    break;
                }
            }
        }
    }


    free(b_0.b.a);
    free(b_1.b.a);
    if(min_count == 0) return -1;
    if(max_count == 0) return 0;
    if(max_count/min_count>0.3) return 1;
    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x2100
movq $0x0, -0x60(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x2c(%rbp), %eax
leaq -0x104(%rbp), %rdx
leaq -0x110(%rbp), %rcx
leaq -0x120(%rbp), %r8
leaq -0x98(%rbp), %r9
movl %eax, (%rsp)
callq 0xdb440
movl %eax, -0x124(%rbp)
movq -0x18(%rbp), %rdi
movl -0x10(%rbp), %esi
movl -0x2c(%rbp), %eax
leaq -0x108(%rbp), %rdx
leaq -0x118(%rbp), %rcx
leaq -0x120(%rbp), %r8
leaq -0x100(%rbp), %r9
movl %eax, (%rsp)
callq 0xdb440
movl %eax, -0x128(%rbp)
cmpl $0x7, -0x124(%rbp)
je 0xdc4eb
cmpl $0x7, -0x128(%rbp)
jne 0xdc4f8
movq $-0x1, -0x8(%rbp)
jmp 0xdc913
cmpl $0x5, -0x124(%rbp)
je 0xdc528
cmpl $0x0, -0x124(%rbp)
je 0xdc528
movq -0x110(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x110(%rbp)
movq -0x60(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x60(%rbp)
cmpl $0x5, -0x128(%rbp)
je 0xdc55e
cmpl $0x0, -0x128(%rbp)
je 0xdc55e
movq -0x118(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x118(%rbp)
movq -0xc8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xc8(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x130(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x138(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x138(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x130(%rbp)
movb %al, -0x16d(%rbp)
jl 0xdc5be
cmpq $0x0, -0x138(%rbp)
setge %al
movb %al, -0x16d(%rbp)
movb -0x16d(%rbp), %al
testb $0x1, %al
jne 0xdc5ca
jmp 0xdc615
movq -0x50(%rbp), %rax
movq -0x130(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0xb8(%rbp), %rcx
movq -0x138(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jne 0xdc611
movq -0x130(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x130(%rbp)
movq -0x138(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x138(%rbp)
jmp 0xdc613
jmp 0xdc615
jmp 0xdc59b
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x110(%rbp)
movq -0x138(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc8(%rbp)
movq -0x138(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
movq -0x110(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0xdc674
movq -0x118(%rbp), %rax
cmpq -0x28(%rbp), %rax
jg 0xdc681
movq $-0x1, -0x8(%rbp)
jmp 0xdc913
movq -0x110(%rbp), %rax
cmpq -0x118(%rbp), %rax
jg 0xdc6af
leaq -0x98(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x100(%rbp), %rax
movq %rax, -0x150(%rbp)
jmp 0xdc6cb
leaq -0x100(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x98(%rbp), %rax
movq %rax, -0x150(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x158(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x160(%rbp)
movq $0x0, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq -0x148(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jge 0xdc894
movq -0x148(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x130(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x164(%rbp)
movq $0x0, -0x138(%rbp)
movq -0x138(%rbp), %rax
movq -0x20(%rbp), %rcx
movl -0x164(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpq %rcx, %rax
jge 0xdc87b
movq -0x20(%rbp), %rax
movl -0x164(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
imulq $0x28, -0x138(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, -0x168(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x168(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xdc7de
movq 0x18(%rbp), %rdi
movl -0x168(%rbp), %esi
leaq -0x168(%rbp), %rdx
leaq -0x16c(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x168(%rbp)
je 0xdc7d7
cmpl $0x1, -0x16c(%rbp)
je 0xdc7d7
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x168(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xdc7dc
jmp 0xdc864
jmp 0xdc7de
movsd 0x2a581a(%rip), %xmm0   # 0x382000
addsd -0x160(%rbp), %xmm0
movsd %xmm0, -0x160(%rbp)
movq $0x0, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq -0x150(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jge 0xdc862
movq -0x150(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x140(%rbp), %rcx
movl (%rax,%rcx,4), %eax
cmpl -0x168(%rbp), %eax
jne 0xdc84c
movsd 0x2a57c6(%rip), %xmm0   # 0x382000
addsd -0x158(%rbp), %xmm0
movsd %xmm0, -0x158(%rbp)
jmp 0xdc862
jmp 0xdc84e
movq -0x140(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x140(%rbp)
jmp 0xdc801
jmp 0xdc864
movq -0x138(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x138(%rbp)
jmp 0xdc72a
jmp 0xdc87d
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
jmp 0xdc6ec
movq -0x50(%rbp), %rdi
callq 0x2210
movq -0xb8(%rbp), %rdi
callq 0x2210
movsd -0x160(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xdc8c6
jp 0xdc8c6
movq $-0x1, -0x8(%rbp)
jmp 0xdc913
movsd -0x158(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xdc8e3
jp 0xdc8e3
movq $0x0, -0x8(%rbp)
jmp 0xdc913
movsd -0x158(%rbp), %xmm0
divsd -0x160(%rbp), %xmm0
movsd 0x2a504d(%rip), %xmm1   # 0x381948
ucomisd %xmm1, %xmm0
jbe 0xdc90b
movq $0x1, -0x8(%rbp)
jmp 0xdc913
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq