// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0x70(%rbp)
movl $0x0, -0x6c(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf2f0b
movl -0x5c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x10(%rbp), %rdx
movq 0x18(%rdx), %rdx
movl -0x44(%rbp), %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
addq (%rcx,%rdx,8), %rax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl 0x10(%rbp), %eax
cmpl 0x18(%rbp), %eax
je 0xf2da4
movl -0x60(%rbp), %eax
cmpl 0x10(%rbp), %eax
ja 0xf2da4
movl -0x64(%rbp), %eax
cmpl 0x18(%rbp), %eax
jae 0xf2dc8
movl 0x10(%rbp), %eax
cmpl 0x18(%rbp), %eax
jne 0xf2ef1
movl -0x60(%rbp), %eax
cmpl 0x10(%rbp), %eax
jae 0xf2ef1
movl -0x64(%rbp), %eax
cmpl 0x18(%rbp), %eax
jbe 0xf2ef1
cmpl $-0x1, -0x54(%rbp)
jne 0xf2dda
movl -0x44(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0xc8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0xf2eef
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf2e91
movq -0x38(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq -0x4c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x4c(%rbp)
je 0xf2e8d
cmpl $0x1, -0x50(%rbp)
je 0xf2e8d
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf2e8f
jmp 0xf2ee1
jmp 0xf2e91
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf2ea9
jmp 0xf2ee1
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xf2ebe
jmp 0xf2ee1
movq -0xd0(%rbp), %rax
cmpb $0x0, 0x20(%rax)
je 0xf2ed6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xf2edf
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xf2ee1
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xf2e06
jmp 0xf2efb
cmpl $-0x1, -0x54(%rbp)
je 0xf2ef9
jmp 0xf2f0b
jmp 0xf2efb
jmp 0xf2efd
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf2d3c
cmpl $-0x1, -0x54(%rbp)
jne 0xf2f1d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xf33d4
movl -0x6c(%rbp), %eax
movl %eax, %ecx
movl %ecx, %eax
cvtsi2sd %rcx, %xmm1
movl -0x70(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
mulsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0xf2f43
jmp 0xf3327
movl $0x0, -0x70(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf32f8
movl -0x5c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x10(%rbp), %rdx
movq 0x18(%rdx), %rdx
movl -0x44(%rbp), %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
addq (%rcx,%rdx,8), %rax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl 0x10(%rbp), %eax
cmpl 0x18(%rbp), %eax
je 0xf2fc5
movl -0x60(%rbp), %eax
cmpl 0x10(%rbp), %eax
ja 0xf2fc5
movl -0x64(%rbp), %eax
cmpl 0x18(%rbp), %eax
jae 0xf2fe9
movl 0x10(%rbp), %eax
cmpl 0x18(%rbp), %eax
jne 0xf32de
movl -0x60(%rbp), %eax
cmpl 0x10(%rbp), %eax
jae 0xf32de
movl -0x64(%rbp), %eax
cmpl 0x18(%rbp), %eax
jbe 0xf32de
xorps %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x90(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xa0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xa8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movq -0xc8(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0xf31b6
movq -0xc8(%rbp), %rax
movq (%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf30e2
movq -0x38(%rbp), %rdi
movl -0x4c(%rbp), %esi
leaq -0x4c(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0xd3d50
cmpl $-0x1, -0x4c(%rbp)
je 0xf30db
cmpl $0x1, -0x50(%rbp)
je 0xf30db
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf30e0
jmp 0xf31a8
jmp 0xf30e2
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0xf30fd
jmp 0xf31a8
movq -0x20(%rbp), %rax
movl -0x4c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0xf3115
jmp 0xf31a8
movq -0xd0(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %edi
movl -0x4c(%rbp), %esi
movq -0xd0(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl -0x60(%rbp), %eax
movl %eax, %r8d
movl 0x10(%rbp), %r9d
movl 0x18(%rbp), %eax
movl %eax, (%rsp)
callq 0xf2ad0
movl %eax, -0x68(%rbp)
movq -0xd0(%rbp), %rax
cmpb $0x0, 0x20(%rax)
je 0xf3189
movl -0x68(%rbp), %eax
movsd 0x28ee8b(%rip), %xmm0   # 0x382000
addsd -0x90(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x90(%rbp,%rax,8)
jmp 0xf31a6
movl -0x68(%rbp), %eax
movsd 0x28ee6c(%rip), %xmm0   # 0x382000
addsd -0xb0(%rbp,%rax,8), %xmm0
movsd %xmm0, -0xb0(%rbp,%rax,8)
jmp 0xf31a8
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xf3054
movsd -0x80(%rbp), %xmm0
addsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
movsd -0x80(%rbp), %xmm0
addsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0xa0(%rbp), %xmm0
addsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xa0(%rbp), %xmm0
addsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xb0(%rbp), %xmm0
movsd -0xb0(%rbp), %xmm1
addsd -0x90(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm1
addsd -0x88(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb8(%rbp)
movsd -0xc0(%rbp), %xmm0
ucomisd -0xb8(%rbp), %xmm0
jb 0xf32a4
movsd -0x90(%rbp), %xmm1
movl -0x6c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x6c(%rbp)
movsd -0xb0(%rbp), %xmm1
movl -0x70(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x70(%rbp)
jmp 0xf32dc
movsd -0x88(%rbp), %xmm1
movl -0x6c(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x6c(%rbp)
movsd -0xa8(%rbp), %xmm1
movl -0x70(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movl %eax, -0x70(%rbp)
jmp 0xf32e8
cmpl $-0x1, -0x54(%rbp)
je 0xf32e6
jmp 0xf32f8
jmp 0xf32e8
jmp 0xf32ea
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf2f5d
movl -0x6c(%rbp), %eax
movl %eax, %ecx
movl %ecx, %eax
cvtsi2sd %rcx, %xmm1
movl -0x70(%rbp), %ecx
addl %ecx, %eax
cvtsi2sd %rax, %xmm0
mulsd -0x40(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0xf331b
jmp 0xf3327
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xf33d4
movl -0x54(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq -0x10(%rbp), %rdx
movq 0x18(%rdx), %rdx
movl -0x44(%rbp), %esi
movq (%rdx,%rsi,8), %rdx
shrq $0x21, %rdx
addq (%rcx,%rdx,8), %rax
movl %eax, -0x64(%rbp)
movl 0x10(%rbp), %eax
subl -0x60(%rbp), %eax
movl -0x64(%rbp), %ecx
subl 0x18(%rbp), %ecx
cmpl %ecx, %eax
ja 0xf338d
cmpl $0x0, -0x44(%rbp)
jne 0xf3385
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0xf3385
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xf33d4
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xf33d4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf33a7
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0xf33d4
movl -0x44(%rbp), %eax
addl $0x1, %eax
movq -0x10(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jne 0xf33cd
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xf33cd
movl $0x0, -0x4(%rbp)
jmp 0xf33d4
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq