// File: /workspace/github_cpp_makefile_star2_O1/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, 0x48(%rsp)
movq %r9, %r15
movq %r8, 0x50(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbx
movl 0xf8(%rsp), %r13d
movl 0xf0(%rsp), %r12d
movl 0x10(%rdi), %r14d
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
testq %r14, %r14
movq %r14, 0x58(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
je 0xb896e
movq 0x18(%rsi), %rbp
movq 0x18(%rdi), %rax
movq %rax, 0x28(%rsp)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r9d, %r9d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rbp, 0x38(%rsp)
movq 0x28(%rsp), %rax
movq (%rax,%r8,8), %r10
movq %r10, %rsi
shrq $0x21, %rsi
movl (%rbp,%rsi,8), %eax
addl %r9d, %eax
cmpl %r13d, %r12d
je 0xb8862
cmpl %r12d, %r9d
ja 0xb8862
cmpl %r13d, %eax
jae 0xb887d
cmpl %r13d, %r12d
jne 0xb894f
cmpl %r12d, %r9d
jae 0xb894f
cmpl %r13d, %eax
jbe 0xb894f
cmpl $-0x1, %edi
movl %r8d, %r11d
cmovnel %edi, %r11d
movl 0x10(%rsp), %eax
cmovel %r9d, %eax
movl %eax, 0x10(%rsp)
leaq (%rsi,%rsi,2), %rsi
movq 0x40(%rsp), %rdi
movl 0xc(%rdi,%rsi,8), %eax
testq %rax, %rax
je 0xb895a
leaq (%rdi,%rsi,8), %rsi
movq (%rsi), %r14
movq 0x50(%rsp), %rsi
movq 0x20(%rsi), %r12
shlq $0x3, %rax
leaq (%rax,%rax,4), %r13
xorl %ebp, %ebp
movl 0xc(%r14,%rbp), %eax
cmpl $0x0, (%r12,%rax,8)
movl %eax, %esi
js 0xb88ec
movl %esi, %eax
cmpl $0x0, (%r12,%rax,8)
js 0xb892a
testb $0x1, (%rbx,%rax)
je 0xb892a
cmpb $0x0, 0x20(%r14,%rbp)
je 0xb8928
incl %ecx
jmp 0xb892a
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
cmpq %rax, (%r15)
jbe 0xb890f
movq 0x8(%r15), %rdi
movl (%rdi,%rax,4), %eax
cmpl $-0x1, %eax
je 0xb890f
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
shrl $0x1f, %eax
jmp 0xb8911
xorl %eax, %eax
cmpl $-0x1, %esi
sete %dil
orb %al, %dil
jne 0xb892a
movl %esi, %eax
cmpl $0x0, (%r12,%rax,8)
jns 0xb88d1
jmp 0xb892a
incl %edx
addq $0x28, %rbp
cmpq %rbp, %r13
jne 0xb88c3
movq 0x58(%rsp), %r14
movl 0xf0(%rsp), %r12d
movl 0xf8(%rsp), %r13d
movq 0x38(%rsp), %rbp
jmp 0xb895a
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
cmpl $-0x1, %edi
jne 0xb898d
addl %r10d, %r9d
incq %r8
movl %r11d, %edi
cmpq %r14, %r8
jne 0xb883c
jmp 0xb8980
xorl %ecx, %ecx
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
xorl %edx, %edx
movl %r11d, %edi
cmpl $-0x1, %r11d
je 0xb8cef
movl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addl %ecx, %edx
cvtsi2sd %rdx, %xmm1
mulsd 0x48(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0xb8c99
xorl %eax, %eax
movl $0x0, %edx
movl %edi, 0x14(%rsp)
cmpl %r14d, %edi
jae 0xb8c6a
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0xa0(%rsp)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x98(%rsp)
movl 0x14(%rsp), %esi
xorl %edx, %edx
xorl %eax, %eax
movl 0x10(%rsp), %ebp
movq %rax, 0x20(%rsp)
movq 0x98(%rsp), %rax
movq (%rax,%rsi,8), %rax
movq %rax, 0x8(%rsp)
shrq $0x21, %rax
movq 0xa0(%rsp), %rcx
movl (%rcx,%rax,8), %ecx
addl %ebp, %ecx
movl %ebp, %r8d
movq %r8, 0x28(%rsp)
cmpl %r13d, %r12d
je 0xb8a2e
cmpl %r12d, %ebp
ja 0xb8a2e
cmpl %r13d, %ecx
jae 0xb8a4b
cmpl %r13d, %r12d
jne 0xb8c65
cmpl %r12d, 0x28(%rsp)
jae 0xb8c65
cmpl %r13d, %ecx
jbe 0xb8c65
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xb0(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x90(%rsp)
leaq (%rax,%rax,2), %rax
movapd %xmm0, 0x60(%rsp)
movq %rcx, 0x70(%rsp)
movq 0x40(%rsp), %rcx
movl 0xc(%rcx,%rax,8), %r12d
testq %r12, %r12
je 0xb8b7b
movq 0x40(%rsp), %rcx
leaq (%rcx,%rax,8), %rax
movq (%rax), %r14
movq 0x50(%rsp), %rax
movq 0x20(%rax), %r13
movq 0x8(%rsp), %rax
shrq $0x20, %rax
movq %rax, 0x38(%rsp)
movl 0xc(%r14), %esi
cmpl $0x0, (%r13,%rsi,8)
movl %esi, %eax
js 0xb8b32
movl %eax, %eax
cmpl $0x0, (%r13,%rax,8)
js 0xb8b6e
testb $0x1, (%rbx,%rax)
je 0xb8b6e
movl 0xf8(%rsp), %eax
movl %eax, (%rsp)
movq 0x38(%rsp), %rdi
movq %r14, %rdx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl 0xf0(%rsp), %r9d
callq 0xb8701
cmpb $0x0, 0x20(%r14)
leaq 0x80(%rsp), %rcx
leaq 0x60(%rsp), %rdx
cmoveq %rdx, %rcx
movl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
addsd 0x16c745(%rip), %xmm0   # 0x225270
movsd %xmm0, (%rcx,%rax,8)
jmp 0xb8b6e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rsi, (%r15)
jbe 0xb8b54
movq 0x8(%r15), %rcx
movl (%rcx,%rsi,4), %ecx
cmpl $-0x1, %ecx
je 0xb8b54
movl %ecx, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shrl $0x1f, %ecx
jmp 0xb8b56
xorl %ecx, %ecx
cmpl $-0x1, %eax
sete %dl
orb %cl, %dl
jne 0xb8b6e
movl %eax, %ecx
cmpl $0x0, (%r13,%rcx,8)
jns 0xb8ac5
addq $0x28, %r14
decq %r12
jne 0xb8ab7
movsd 0x90(%rsp), %xmm0
movsd 0x80(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
addsd 0x88(%rsp), %xmm0
movsd 0x70(%rsp), %xmm2
movsd 0x60(%rsp), %xmm5
addsd %xmm2, %xmm5
movsd %xmm5, 0x60(%rsp)
addsd 0x68(%rsp), %xmm2
movsd %xmm0, 0x88(%rsp)
movsd %xmm2, 0x68(%rsp)
movapd %xmm1, %xmm3
addsd %xmm5, %xmm3
movapd %xmm5, %xmm4
divsd %xmm3, %xmm4
movapd %xmm0, %xmm6
addsd %xmm2, %xmm6
movapd %xmm2, %xmm3
divsd %xmm6, %xmm3
ucomisd %xmm3, %xmm4
jb 0xb8bf8
movapd %xmm5, %xmm2
movl 0x20(%rsp), %eax
xorps %xmm5, %xmm5
cvtsi2sd %rax, %xmm5
addl 0x8(%rsp), %ebp
movl 0xb0(%rsp), %eax
xorps %xmm6, %xmm6
cvtsi2sd %rax, %xmm6
addsd %xmm2, %xmm6
cmpnlesd %xmm4, %xmm3
andpd %xmm3, %xmm0
andnpd %xmm1, %xmm3
orpd %xmm0, %xmm3
addsd %xmm5, %xmm3
cvttsd2si %xmm6, %rdx
cvttsd2si %xmm3, %rax
movq 0xa8(%rsp), %rsi
incq %rsi
movq 0x58(%rsp), %r14
cmpq %r14, %rsi
movl 0xf0(%rsp), %r12d
movl 0xf8(%rsp), %r13d
jne 0xb89f0
jmp 0xb8c6a
movq 0x20(%rsp), %rax
movq %rax, %rcx
movl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addl %ecx, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd 0x48(%rsp), %xmm1
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
movl 0x14(%rsp), %edi
jb 0xb8cef
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl %edi, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movl 0x10(%rsp), %edx
subl %edx, %r12d
subl %r13d, %edx
addl (%rax,%rcx,8), %edx
cmpl %edx, %r12d
jbe 0xb8ce7
incl %edi
cmpl %r14d, %edi
jb 0xb8ceb
cmpl %r14d, %edi
jne 0xb8cdd
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
js 0xb8d19
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
jmp 0xb8cef
testl %edi, %edi
je 0xb8d05
movl %edi, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
jns 0xb8cef
jmp 0xb8ceb
movl $0x0, 0x8(%rsp)
jmp 0xb8cef