// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movl %r9d, %r15d
movq %r8, 0x10(%rsp)
movq %rcx, %rbx
movq %rdx, %r14
movl %esi, 0x18(%rsp)
movl %edi, %esi
xorpd %xmm0, %xmm0
leaq 0xb0(%rsp), %r9
movapd %xmm0, (%r9)
movapd %xmm0, 0x10(%r9)
movapd %xmm0, 0x20(%r9)
movapd %xmm0, 0x30(%r9)
movapd %xmm0, 0x40(%r9)
movapd %xmm0, 0x50(%r9)
andq $0x0, 0x60(%r9)
leaq 0x40(%rsp), %r13
movapd %xmm0, (%r13)
movapd %xmm0, 0x10(%r13)
movapd %xmm0, 0x20(%r13)
movapd %xmm0, 0x30(%r13)
andq $0x0, 0x60(%r13)
movapd %xmm0, 0x40(%r13)
andq $0x0, 0x38(%r9)
movapd %xmm0, 0x50(%r13)
andq $0x0, 0x38(%r13)
movl %r15d, (%rsp)
leaq 0x24(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x28(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %r8
callq 0x709b2
movl %eax, %r12d
movl %r15d, (%rsp)
leaq 0x20(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movq %r14, %rdi
movl 0x18(%rsp), %esi
movq %rbp, %r8
movq %r13, %r9
callq 0x709b2
cmpl $0x7, %r12d
sete %cl
cmpl $0x7, %eax
sete %dl
orb %cl, %dl
pushq $-0x1
popq %r11
jne 0x714d3
testl %r12d, %r12d
je 0x71340
cmpl $0x5, %r12d
je 0x71340
decq 0xe8(%rsp)
testl %eax, %eax
je 0x7134e
cmpl $0x5, %eax
je 0x7134e
decq 0x78(%rsp)
movq 0x158(%rsp), %rax
movq 0xe8(%rsp), %rsi
movq 0xf8(%rsp), %rdi
movq 0x78(%rsp), %r8
movq 0x88(%rsp), %rcx
movq %r8, %r10
movq %rsi, %rdx
testq %rsi, %rsi
jle 0x71397
testq %r10, %r10
jle 0x71397
leaq -0x1(%rdx), %rsi
leaq -0x1(%r10), %r8
movl -0x4(%rdi,%rdx,4), %r9d
cmpl -0x4(%rcx,%r10,4), %r9d
je 0x71373
movq %rdx, 0xe8(%rsp)
movq %r10, 0x78(%rsp)
movq 0x10(%rsp), %r8
cmpq %r8, %rdx
setg %sil
cmpq %r8, %r10
setg %r8b
andb %sil, %r8b
cmpb $0x1, %r8b
jne 0x714d3
cmpq %r10, %rdx
movq %r10, %rsi
cmovlq %rdx, %rsi
movq %rdi, %r8
cmovgq %rcx, %r8
cmovgq %rdi, %rcx
xorl %r9d, %r9d
testq %rsi, %rsi
cmovleq %r9, %rsi
cmpq %rdx, %r10
cmovgq %r10, %rdx
testq %rdx, %rdx
cmovleq %r9, %rdx
xorpd %xmm1, %xmm1
movsd 0x171ea3(%rip), %xmm0   # 0x1e32a0
xorpd %xmm2, %xmm2
cmpq %rsi, %r9
je 0x71479
movl (%r8,%r9,4), %r10d
imulq $0x18, %r10, %r11
leaq (%rbx,%r11), %r10
movl 0xc(%rbx,%r11), %r11d
xorl %r12d, %r12d
cmpq %r11, %r12
je 0x71474
movq (%r10), %r13
imulq $0x28, %r12, %rbp
movl 0xc(%r13,%rbp), %r13d
movq 0x20(%r14), %rbp
cmpl $0x0, (%rbp,%r13,8)
js 0x71455
addsd %xmm0, %xmm2
xorl %ebp, %ebp
cmpq %rbp, %rdx
je 0x7146f
leaq 0x1(%rbp), %r15
cmpl %r13d, (%rcx,%rbp,4)
movq %r15, %rbp
jne 0x7143d
addsd %xmm0, %xmm1
jmp 0x7146f
cmpq %r13, (%rax)
jbe 0x7146f
movq 0x8(%rax), %r15
movslq (%r15,%r13,4), %r13
testq %r13, %r13
js 0x7146f
cmpl $0x0, (%rbp,%r13,8)
jns 0x71437
incq %r12
jmp 0x7141a
incq %r9
jmp 0x71401
movsd %xmm1, 0x10(%rsp)
movsd %xmm2, 0x18(%rsp)
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x2220
movsd 0x18(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
pushq $-0x1
popq %r11
jne 0x714ad
jnp 0x714d3
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x714c0
jp 0x714c0
xorl %r11d, %r11d
jmp 0x714d3
divsd %xmm2, %xmm1
xorl %r11d, %r11d
ucomisd 0x171731(%rip), %xmm1   # 0x1e2c00
seta %r11b
movq %r11, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq