// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_break_point_idx(ma_utg_t*, All_reads*, unsigned char*, ma_hit_t_alloc*, asg_t*, R_to_U*, unsigned int, unsigned int, double)
uint32_t get_break_point_idx(ma_utg_t *u, All_reads *RNF, uint8_t* r_flag, ma_hit_t_alloc* sources, 
asg_t* read_g, R_to_U* ruIndex, uint32_t p_beg, uint32_t p_end, double m_rate)
{
    uint32_t k, i, tn, is_Unitig, min_k = (uint32_t)-1, min_l = (uint32_t)-1, l, c_beg, c_end, index;
    uint32_t e_occ = 0, ne_occ = 0;
    double e_occ_dir[3], ne_occ_dir[3], rate[2];
    ma_hit_t_alloc* x = NULL;
    ma_hit_t *h = NULL;

    e_occ = ne_occ = 0;
    for (k = l = 0; k < u->n; k++)
    {
        c_beg = l;
        c_end = c_beg + Get_READ_LENGTH((*RNF), (u->a[k]>>33));
        l += (uint32_t)u->a[k];

        if((p_beg != p_end && c_beg <= p_beg && c_end >= p_end) || 
                    (p_beg == p_end && c_beg < p_beg && c_end > p_end))
        {

            if(min_k == (uint32_t)-1) min_k = k, min_l = c_beg;

            x = &(sources[u->a[k]>>33]);
            for (i = 0; i < x->length; i++)
            {
                h = &(x->buffer[i]);
                tn = Get_tn((*h));
                if(read_g->seq[tn].del == 1)
                {
                    ///get the id of read that contains it 
                    get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                    if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
                }
                if(read_g->seq[tn].del == 1) continue;
                if(!(r_flag[tn]&1)) continue;
                if(h->el) e_occ++;
                else ne_occ++;
            }
        }
        else if(min_k != (uint32_t)-1)
        {
            break;
        }
    }

    if(min_k == (uint32_t)-1) return (uint32_t)-1;
    if(e_occ <= ((e_occ+ne_occ)*m_rate)) goto c_break;
    
    e_occ = ne_occ = 0;
    for (k = min_k, l = min_l; k < u->n; k++)
    {
        c_beg = l;
        c_end = c_beg + Get_READ_LENGTH((*RNF), (u->a[k]>>33));
        l += (uint32_t)u->a[k];
        
        if((p_beg != p_end && c_beg <= p_beg && c_end >= p_end) || 
                    (p_beg == p_end && c_beg < p_beg && c_end > p_end))
        {
            e_occ_dir[0] = e_occ_dir[1] = e_occ_dir[2] = 0;
            ne_occ_dir[0] = ne_occ_dir[1] = ne_occ_dir[2] = 0;

            x = &(sources[u->a[k]>>33]);
            for (i = 0; i < x->length; i++)
            {
                h = &(x->buffer[i]);
                tn = Get_tn((*h));
                if(read_g->seq[tn].del == 1)
                {
                    ///get the id of read that contains it 
                    get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                    if(tn == (uint32_t)-1 || is_Unitig == 1 || read_g->seq[tn].del == 1) continue;
                }
                if(read_g->seq[tn].del == 1) continue;
                if(!(r_flag[tn]&1)) continue;

                tn = Get_tn((*h));///must!!!!

                index = get_overlap_contig_dir(u->a[k]>>32, tn, h, RNF, c_beg, p_beg, p_end);

                ///debug_r_contig_pos(u, tn, RNF);
                if(h->el) e_occ_dir[index]++;
                else ne_occ_dir[index]++;
            }

            e_occ_dir[0] += e_occ_dir[2]; e_occ_dir[1] += e_occ_dir[2];
            ne_occ_dir[0] += ne_occ_dir[2]; ne_occ_dir[1] += ne_occ_dir[2];
            rate[0] = ne_occ_dir[0] / (ne_occ_dir[0] + e_occ_dir[0]);
            rate[1] = ne_occ_dir[1] / (ne_occ_dir[1] + e_occ_dir[1]);
            if(rate[0] >= rate[1])
            {
                e_occ += e_occ_dir[0];
                ne_occ += ne_occ_dir[0];
            }
            else
            {
                e_occ += e_occ_dir[1];
                ne_occ += ne_occ_dir[1];
            }
        }
        else if(min_k != (uint32_t)-1)
        {
            break;
        }
    }
    
    if(e_occ <= ((e_occ+ne_occ)*m_rate)) goto c_break;
    return (uint32_t)-1;


    c_break:
    k = min_k; c_beg = min_l; c_end = c_beg + Get_READ_LENGTH((*RNF), (u->a[k]>>33));
    if((p_beg - c_beg) <= (c_end - p_end))
    {
        if(k == 0 && !u->circ) return (uint32_t)-1;
        return k;
    }
    else
    {
        if((k+1) < u->n) return k+1;
        if((k+1) == u->n && u->circ) return 0;
        return (uint32_t)-1;
    }

    return (uint32_t)-1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, %r13
movq %rcx, 0x58(%rsp)
movq %rdx, %rbx
movq %rsi, 0x20(%rsp)
movq %rdi, 0x10(%rsp)
movl 0x10(%rdi), %r14d
xorl %edx, %edx
pushq $-0x1
popq %rax
movl %eax, %r11d
movl %eax, 0x8(%rsp)
xorl %edi, %edi
xorl %ecx, %ecx
xorl %eax, %eax
movq %r14, 0x28(%rsp)
cmpq %r14, %rdx
je 0x7e789
movq 0x20(%rsp), %rsi
movq 0x18(%rsi), %r9
movq 0x10(%rsp), %rsi
movq 0x18(%rsi), %r8
movq (%r8,%rdx,8), %r8
movq %r8, %r10
shrq $0x21, %r10
movl (%r9,%r10,8), %r9d
addl %edi, %r9d
movl 0xf8(%rsp), %esi
cmpl %esi, 0xf0(%rsp)
je 0x7e6a8
cmpl 0xf0(%rsp), %edi
ja 0x7e6a8
cmpl 0xf8(%rsp), %r9d
jae 0x7e6d0
cmpl %esi, 0xf0(%rsp)
jne 0x7e771
cmpl 0xf0(%rsp), %edi
jae 0x7e771
cmpl 0xf8(%rsp), %r9d
jbe 0x7e771
cmpl $-0x1, %r11d
movl %edx, %r9d
movl %r11d, 0xc(%rsp)
cmovnel %r11d, %r9d
imulq $0x18, %r10, %r11
movq 0x58(%rsp), %rsi
leaq (%rsi,%r11), %r10
movl 0xc(%rsi,%r11), %r11d
imulq $0x28, %r11, %r11
xorl %r15d, %r15d
movq 0x18(%rsp), %rsi
cmpq %r15, %r11
je 0x7e75a
movq (%r10), %r14
movl 0xc(%r14,%r15), %r12d
movq 0x20(%r13), %rbp
cmpl $0x0, (%rbp,%r12,8)
js 0x7e72a
testb $0x1, (%rbx,%r12)
je 0x7e754
cmpb $0x0, 0x20(%r14,%r15)
je 0x7e74b
incl %ecx
jmp 0x7e754
cmpq %r12, (%rsi)
jbe 0x7e754
movq 0x8(%rsi), %rsi
movslq (%rsi,%r12,4), %r12
testq %r12, %r12
js 0x7e74f
cmpl $0x0, (%rbp,%r12,8)
movq 0x18(%rsp), %rsi
jns 0x7e717
jmp 0x7e754
incl %eax
jmp 0x7e754
movq 0x18(%rsp), %rsi
addq $0x28, %r15
jmp 0x7e6fe
cmpl $-0x1, 0xc(%rsp)
movl 0x8(%rsp), %esi
cmovel %edi, %esi
movl %esi, 0x8(%rsp)
movq 0x28(%rsp), %r14
jmp 0x7e77b
cmpl $-0x1, %r11d
jne 0x7e793
pushq $-0x1
popq %r9
addl %r8d, %edi
incq %rdx
movl %r9d, %r11d
jmp 0x7e658
cmpl $-0x1, %r11d
je 0x7ea9f
movl %ecx, %edx
cvtsi2sd %rdx, %xmm0
addl %ecx, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x7ea19
movl %r11d, %ecx
xorl %eax, %eax
movq %rcx, 0xa0(%rsp)
movq %rcx, %rdi
movl 0x8(%rsp), %ecx
movl %ecx, %r8d
xorl %r9d, %r9d
movl %r11d, 0xc(%rsp)
movl %eax, %r10d
cmpq %r14, %rdi
jae 0x7ea1e
movq 0x20(%rsp), %rcx
movq 0x18(%rcx), %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rdi,8), %rcx
movq %rcx, %r15
shrq $0x21, %rcx
movl (%rdx,%rcx,8), %edx
addl %r8d, %edx
movl 0xf8(%rsp), %esi
cmpl %esi, 0xf0(%rsp)
je 0x7e822
cmpl 0xf0(%rsp), %r8d
ja 0x7e822
cmpl 0xf8(%rsp), %edx
jae 0x7e84a
cmpl %esi, 0xf0(%rsp)
jne 0x7ea1e
cmpl 0xf0(%rsp), %r8d
jae 0x7ea1e
cmpl 0xf8(%rsp), %edx
jbe 0x7ea1e
movq %r10, 0x38(%rsp)
movq %r9, 0x40(%rsp)
movq %rdi, 0x50(%rsp)
movl %r8d, %eax
movq %rax, 0xa8(%rsp)
andq $0x0, 0x90(%rsp)
addl %r15d, %r8d
movq %r8, 0x48(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
imulq $0x18, %rcx, %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax), %r14
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movl 0xc(%rcx,%rax), %eax
shrq $0x20, %r15
movq %r15, 0xb0(%rsp)
imulq $0x28, %rax, %r15
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0x7e965
movq (%r14), %r12
movl 0xc(%r12,%rbp), %esi
movq 0x20(%r13), %rax
cmpl $0x0, (%rax,%rsi,8)
movq %rsi, %rcx
js 0x7e936
testb $0x1, (%rbx,%rcx)
je 0x7e95c
leaq (%r12,%rbp), %rdx
movl 0xf8(%rsp), %eax
movl %eax, (%rsp)
movq 0xb0(%rsp), %rdi
movq 0x20(%rsp), %rcx
movq 0xa8(%rsp), %r8
movl 0xf0(%rsp), %r9d
callq 0x7e554
cmpb $0x0, 0x20(%r12,%rbp)
leaq 0x80(%rsp), %rcx
leaq 0x60(%rsp), %rdx
cmoveq %rdx, %rcx
movl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
addsd 0x164971(%rip), %xmm0   # 0x1e32a0
movsd %xmm0, (%rcx,%rax,8)
jmp 0x7e95c
movq 0x18(%rsp), %rcx
cmpq %rsi, (%rcx)
jbe 0x7e95c
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rcx
movslq (%rcx,%rsi,4), %rcx
testq %rcx, %rcx
js 0x7e95c
cmpl $0x0, (%rax,%rcx,8)
jns 0x7e8cf
addq $0x28, %rbp
jmp 0x7e8b1
movsd 0x90(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
addpd 0x80(%rsp), %xmm0
movapd %xmm0, 0x80(%rsp)
movsd 0x70(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
addpd 0x60(%rsp), %xmm2
movapd %xmm2, 0x60(%rsp)
movapd %xmm0, %xmm1
addpd %xmm2, %xmm1
movapd %xmm2, %xmm3
divpd %xmm1, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
ucomisd %xmm1, %xmm3
jae 0x7e9bc
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
movl 0xc(%rsp), %r11d
movq 0x28(%rsp), %r14
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %rax
movq 0x38(%rsp), %rcx
cvtsi2sd %rcx, %xmm4
movl %eax, %eax
cvtsi2sd %rax, %xmm5
addsd %xmm2, %xmm5
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
cmpnlesd %xmm3, %xmm1
andpd %xmm1, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
cvttsd2si %xmm5, %r9
addsd %xmm4, %xmm1
cvttsd2si %xmm1, %rax
incq %rdi
jmp 0x7e7d0
movl %r11d, %edx
jmp 0x7ea3f
cvtsi2sd %r10, %xmm0
addl %eax, %r9d
cvtsi2sd %r9, %xmm1
mulsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq 0xa0(%rsp), %rdx
jb 0x7ea9f
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x10(%rsp), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movl 0xf0(%rsp), %edx
movl 0x8(%rsp), %esi
subl %esi, %edx
subl 0xf8(%rsp), %esi
addl (%rax,%rcx,8), %esi
cmpl %esi, %edx
jbe 0x7ea90
incl %r11d
cmpl %r14d, %r11d
jb 0x7eaa3
cmpl %r14d, %r11d
jne 0x7ea9f
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
jns 0x7ea9f
xorl %r11d, %r11d
jmp 0x7eaa3
testl %r11d, %r11d
jne 0x7eaa3
movq 0x10(%rsp), %rax
cmpl $0x0, (%rax)
js 0x7eaa3
pushq $-0x1
popq %r11
movl %r11d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq