// File: /workspace/github_cpp_makefile_star2_O3/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 $0xc8, %rsp
movq %r8, 0x60(%rsp)
movq %rcx, 0x40(%rsp)
movl 0x10(%rdi), %r14d
testq %r14, %r14
je 0xb9b6c
movq %r9, %r15
movq %rdx, %rbx
movl 0x108(%rsp), %r12d
movl 0x100(%rsp), %r13d
movq %rsi, 0x68(%rsp)
movq 0x18(%rsi), %rax
movq %rax, 0x38(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x18(%rdi), %rax
movq %rax, 0x30(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %edi, %edi
xorl %eax, %eax
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq %r14, 0x58(%rsp)
movq 0x40(%rsp), %rsi
movl %r10d, %edx
movq 0x30(%rsp), %r9
movq (%r9,%rdi,8), %r9
movq %r9, %r11
shrq $0x21, %r11
movq 0x38(%rsp), %r10
movl (%r10,%r11,8), %r10d
addl %r8d, %r10d
cmpl %r12d, %r13d
je 0xb974a
cmpl %r13d, %r8d
ja 0xb974a
cmpl %r12d, %r10d
jae 0xb9765
cmpl %r12d, %r13d
jne 0xb9811
cmpl %r13d, %r8d
jae 0xb9811
cmpl %r12d, %r10d
jbe 0xb9811
cmpl $-0x1, %edx
movl %edi, %r10d
cmovnel %edx, %r10d
cmovel %r8d, %ebp
leaq (%r11,%r11,2), %r11
movl 0xc(%rsi,%r11,8), %r12d
testq %r12, %r12
je 0xb9807
movq %rsi, %rdx
movl %ebp, %esi
leaq (%rdx,%r11,8), %r11
movq (%r11), %r11
movq 0x60(%rsp), %rdx
movq 0x20(%rdx), %r14
shlq $0x3, %r12
leaq (%r12,%r12,4), %r12
xorl %r13d, %r13d
movl 0xc(%r11,%r13), %ebp
cmpl $0x0, (%r14,%rbp,8)
js 0xb97c3
testb $0x1, (%rbx,%rbp)
je 0xb97e0
cmpb $0x0, 0x20(%r11,%r13)
je 0xb97de
incl %ecx
jmp 0xb97e0
cmpq %rbp, (%r15)
jbe 0xb97e0
movq 0x8(%r15), %rdx
movslq (%rdx,%rbp,4), %rbp
testq %rbp, %rbp
js 0xb97e0
cmpl $0x0, (%r14,%rbp,8)
jns 0xb97b1
jmp 0xb97e0
incl %eax
addq $0x28, %r13
cmpq %r13, %r12
jne 0xb97a5
movq 0x58(%rsp), %r14
movl 0x100(%rsp), %r13d
movl 0x108(%rsp), %r12d
movl %esi, %ebp
movq 0x40(%rsp), %rsi
jmp 0xb981c
movl 0x108(%rsp), %r12d
jmp 0xb981c
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpl $-0x1, %edx
jne 0xb9838
addl %r9d, %r8d
incq %rdi
cmpq %r14, %rdi
jne 0xb971c
movl %r10d, %edx
cmpl $-0x1, %r10d
je 0xb9b6c
movl %edx, 0x14(%rsp)
movl %ecx, %edx
movapd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
addl %ecx, %eax
cvtsi2sd %rax, %xmm1
movsd %xmm2, 0xa8(%rsp)
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
movq 0x68(%rsp), %rcx
jae 0xb9b2d
movq $0x0, 0x20(%rsp)
movl $0x0, %eax
movq %rax, 0x28(%rsp)
cmpl %r14d, 0x14(%rsp)
jae 0xb9afb
movq 0x18(%rcx), %rax
movq %rax, 0xb8(%rsp)
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0xb0(%rsp)
movl 0x14(%rsp), %eax
movq $0x0, 0x28(%rsp)
movq $0x0, 0x20(%rsp)
movl %ebp, %edi
movl %ebp, 0x4c(%rsp)
movq %rax, %rdx
movq 0xb0(%rsp), %rax
movq %rdx, 0xc0(%rsp)
movq (%rax,%rdx,8), %rax
movq %rax, 0x50(%rsp)
shrq $0x21, %rax
movq 0xb8(%rsp), %rdx
movl (%rdx,%rax,8), %edx
addl %edi, %edx
movl %edi, 0x30(%rsp)
movl %edi, %edi
movq %rdi, 0x38(%rsp)
cmpl %r12d, %r13d
je 0xb990d
cmpl %r13d, 0x30(%rsp)
ja 0xb990d
cmpl %r12d, %edx
jae 0xb992a
cmpl %r12d, %r13d
jne 0xb9afb
cmpl %r13d, 0x38(%rsp)
jae 0xb9afb
cmpl %r12d, %edx
jbe 0xb9afb
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
xorl %edx, %edx
movq %rdx, 0xa0(%rsp)
leaq (%rax,%rax,2), %rax
movapd %xmm0, 0x70(%rsp)
movq %rdx, 0x80(%rsp)
movl 0xc(%rsi,%rax,8), %ebp
testq %rbp, %rbp
je 0xb9a3e
leaq (%rsi,%rax,8), %rax
movq (%rax), %r14
movq 0x60(%rsp), %rax
movq 0x20(%rax), %r12
movq 0x50(%rsp), %r13
shrq $0x20, %r13
movl 0xc(%r14), %esi
cmpl $0x0, (%r12,%rsi,8)
movq %rsi, %rax
js 0xb99e0
testb $0x1, (%rbx,%rax)
je 0xb99f9
movl 0x108(%rsp), %eax
movl %eax, (%rsp)
movl %r13d, %edi
movq %r14, %rdx
movq 0x38(%rsp), %r8
movl 0x100(%rsp), %r9d
callq 0xb95ec
movq 0x68(%rsp), %rcx
cmpb $0x0, 0x20(%r14)
leaq 0x90(%rsp), %rdx
leaq 0x70(%rsp), %rsi
cmoveq %rsi, %rdx
movl %eax, %eax
movsd (%rdx,%rax,8), %xmm0
addsd 0x177937(%rip), %xmm0   # 0x231310
movsd %xmm0, (%rdx,%rax,8)
jmp 0xb99f9
cmpq %rsi, (%r15)
jbe 0xb99f9
movq 0x8(%r15), %rax
movslq (%rax,%rsi,4), %rax
testq %rax, %rax
js 0xb99f9
cmpl $0x0, (%r12,%rax,8)
jns 0xb9987
addq $0x28, %r14
decq %rbp
jne 0xb9979
movsd 0xa0(%rsp), %xmm0
movapd 0x90(%rsp), %xmm2
movsd 0x80(%rsp), %xmm1
movapd 0x70(%rsp), %xmm3
movq 0x58(%rsp), %r14
movl 0x100(%rsp), %r13d
movl 0x108(%rsp), %r12d
jmp 0xb9a4e
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
xorpd %xmm3, %xmm3
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
addpd %xmm2, %xmm0
movapd %xmm0, 0x90(%rsp)
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
addpd %xmm3, %xmm1
movapd %xmm1, 0x70(%rsp)
movapd %xmm0, %xmm2
addpd %xmm1, %xmm2
movapd %xmm1, %xmm3
divpd %xmm2, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
ucomisd %xmm2, %xmm3
movl 0x4c(%rsp), %ebp
movl 0x30(%rsp), %edi
jae 0xb9a97
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addl 0x50(%rsp), %edi
movl 0x20(%rsp), %eax
cvtsi2sd %rax, %xmm4
movl 0x28(%rsp), %eax
cvtsi2sd %rax, %xmm5
addsd %xmm1, %xmm5
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
cmpnlesd %xmm3, %xmm2
andpd %xmm2, %xmm1
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
addsd %xmm4, %xmm2
cvttsd2si %xmm5, %rax
movq %rax, 0x28(%rsp)
cvttsd2si %xmm2, %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
incq %rax
cmpq %r14, %rax
movq 0x40(%rsp), %rsi
jne 0xb98c4
movq 0x20(%rsp), %rdx
movl %edx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movq 0x28(%rsp), %rax
addl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0xa8(%rsp), %xmm1
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
ucomisd %xmm0, %xmm1
jb 0xb9b79
movq 0x18(%rcx), %rax
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rsp), %esi
movl %esi, %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
subl %ebp, %r13d
subl %r12d, %ebp
addl (%rax,%rcx,8), %ebp
cmpl %ebp, %r13d
jbe 0xb9b73
incl %esi
cmpl %r14d, %esi
jb 0xb9b77
cmpl %r14d, %esi
jne 0xb9b6c
movq 0x18(%rsp), %rax
cmpl $0x0, (%rax)
js 0xb9ba0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0xb9b79
testl %esi, %esi
je 0xb9b8b
movl %esi, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rcx
cmpl $0x0, (%rcx)
jns 0xb9b79
movl 0x14(%rsp), %eax
jmp 0xb9b79
xorl %eax, %eax
jmp 0xb9b79