// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: debug_break_point_advance(ma_utg_t*, unsigned int, asg_t*, All_reads*, ma_hit_t_alloc*, R_to_U*, kvec_asg_arc_t_warp*, unsigned char*)
void debug_break_point_advance(ma_utg_t *u, uint32_t uID, asg_t* read_g, All_reads *RNF, ma_hit_t_alloc* sources, 
R_to_U* ruIndex, kvec_asg_arc_t_warp* edge, uint8_t* r_flag)
{
    if(u->n < 2 || u->m == 0) return;
    uint32_t k, l, c_beg;
    memset(r_flag, 0, read_g->n_seq);
    for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 1;
    kvec_t_u64_warp d; kv_init(d.a);
    kvec_t_u64_warp b_d; kv_init(b_d.a);

    d.a.n = 0;
    for (k = l = 0; k < u->n; k++)
    {
        c_beg = l;
        l += (uint32_t)u->a[k];
        get_break_point_cov_advance((uint64_t)(u->a[k])>>32, c_beg, read_g, RNF, sources, ruIndex, r_flag, &d, u->len, u->circ, &uID);
    }
    
    for (k = l = 0; k < u->n; k++)
    {
        c_beg = l;
        l += (uint32_t)u->a[k];
        get_break_point_cov_advance((uint64_t)(u->a[k])>>32, c_beg, read_g, RNF, sources, ruIndex, r_flag, &d, u->len, u->circ, NULL);
    }

    kv_malloc(b_d.a, d.a.n); b_d.a.n = d.a.n;
    memcpy(b_d.a.a, d.a.a, d.a.n*sizeof(uint64_t));

    radix_sort_arch64(d.a.a, d.a.a + d.a.n);

    long long dp, o_dp;
    uint32_t idx, o_idx, dir, o_dir, i;
    uint64_t k_beg, k_end, k_dp, b_beg, b_end, occ;
    dp = 0; o_idx = 0; o_dir = 1;///means it is a beg 
    for (k = 0; k < d.a.n; k++)
    {
        o_dp = dp;
        ///if start = end, we should meet end first, otherwise it will have a bug
        if(d.a.a[k]&1) ++dp;
        else --dp;

        dir = d.a.a[k]&1; idx = d.a.a[k]>>2;
        if((idx - o_idx > 0) || (idx == o_idx && o_dir != dir))
        {
            k_beg = o_idx; k_end = idx; k_dp = o_dp;

            for (i = occ = 0; i < b_d.a.n; i += 2)
            {
                b_beg = b_d.a.a[i]>>2;
                b_end = b_d.a.a[i+1]>>2;
                if(b_beg <= k_beg && b_end >= k_end) occ++;
                if((k_beg == k_end) && (b_beg == k_beg || b_end == k_beg)) occ--;
            }
            if(occ != k_dp)
            {
                fprintf(stderr, "k_beg: %lu, k_end: %lu, k_dp: %lu, occ: %lu\n", k_beg, k_end, k_dp, occ);
            } 
        } 
        o_idx = idx;
        o_dir = dir;
    }

    if(o_idx != u->len)
    {
        k_beg = o_idx; k_end = u->len; k_dp = 0;

        for (i = occ = 0; i < b_d.a.n; i += 2)
        {
            b_beg = b_d.a.a[i]>>2;
            b_end = b_d.a.a[i+1]>>2;
            if(b_beg <= k_beg && b_end >= k_end) occ++;
        }
        if(occ != k_dp)
        {
            fprintf(stderr, "k_beg: %lu, k_end: %lu, k_dp: %lu, occ: %lu\n", k_beg, k_end, k_dp, occ);
        } 
    }

    kv_destroy(d.a); kv_destroy(b_d.a);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, (%rsp)
movl %esi, 0x14(%rsp)
cmpl $0x2, 0x10(%rdi)
jb 0x7ed88
movq %rdi, %rbp
cmpl $0x0, 0xc(%rdi)
je 0x7ed88
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq 0x88(%rsp), %rdi
movl 0x14(%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
xorl %esi, %esi
callq 0x20e0
movl 0x10(%rbp), %eax
cmpq %rax, %rbx
jae 0x7eb2d
movq 0x18(%rbp), %rax
movq (%rax,%rbx,8), %rax
shrq $0x21, %rax
movq 0x88(%rsp), %rcx
movb $0x1, (%rcx,%rax)
incq %rbx
jmp 0x7eb08
movq %rbp, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movaps %xmm0, (%rcx)
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movl %eax, %ecx
cmpq %rcx, %rbx
jae 0x7ebad
movq 0x18(%r14), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movl (%r14), %eax
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shrl $0x1f, %eax
subq $0x8, %rsp
movq %r13, %rdx
movq %r12, %rcx
movq %r15, %r8
movq 0x8(%rsp), %r9
leaq 0x1c(%rsp), %r11
pushq %r11
pushq %rax
pushq %r10
leaq 0x40(%rsp), %rax
pushq %rax
pushq 0xb0(%rsp)
callq 0x7e364
addq $0x30, %rsp
incq %rbx
movl 0x10(%r14), %eax
jmp 0x7eb4b
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movl %eax, %eax
cmpq %rax, %rbx
jae 0x7ec13
movq 0x18(%r14), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movl (%r14), %eax
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shrl $0x1f, %eax
subq $0x8, %rsp
movq %r13, %rdx
movq %r12, %rcx
movq %r15, %r8
movq 0x8(%rsp), %r9
pushq $0x0
pushq %rax
pushq %r10
leaq 0x40(%rsp), %rax
pushq %rax
pushq 0xb0(%rsp)
callq 0x7e364
addq $0x30, %rsp
incq %rbx
movl 0x10(%r14), %eax
jmp 0x7ebb6
movq 0x20(%rsp), %rbp
leaq (,%rbp,8), %r12
movq %r12, %rdi
callq 0x2280
movq %rax, %r14
movq 0x30(%rsp), %rbx
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2160
leaq (%rbx,%rbp,8), %rsi
movq %rbx, %rdi
callq 0x66c25
movq %rbx, %r11
pushq $0x1
popq %rcx
xorl %esi, %esi
xorl %eax, %eax
xorl %r12d, %r12d
movl %r12d, %r12d
movl %eax, %edx
cmpq %r12, %rbp
jbe 0x7ed33
movq (%r11,%r12,8), %r15
movl %r15d, %r13d
andl $0x1, %r13d
cmpq $0x1, %r13
movl $0x0, %ebx
sbbq %rbx, %rbx
shrq $0x2, %r15
xorl %r15d, %eax
xorl %r13d, %ecx
orl %eax, %ecx
je 0x7ecf2
movq %rsi, (%rsp)
movl %r15d, %ecx
xorl %eax, %eax
xorl %r9d, %r9d
movl %eax, %esi
cmpq %rsi, %rbp
jbe 0x7ece9
movq (%r14,%rsi,8), %rsi
shrq $0x2, %rsi
movq 0x8(%r14,%rax,8), %rdi
shrq $0x2, %rdi
cmpq %rdx, %rsi
setbe %r8b
cmpq %rcx, %rdi
setae %r10b
andb %r8b, %r10b
movzbl %r10b, %r8d
addq %r8, %r9
cmpq %rdx, %rcx
jne 0x7ece3
cmpq %rdx, %rsi
sete %sil
cmpq %rdx, %rdi
sete %dil
orb %sil, %dil
movzbl %dil, %esi
subq %rsi, %r9
addq $0x2, %rax
jmp 0x7ec96
movq (%rsp), %rsi
cmpq %rsi, %r9
jne 0x7ed07
orq $0x1, %rbx
addq %rbx, %rsi
incl %r12d
movl %r15d, %eax
movl %r13d, %ecx
jmp 0x7ec57
movq 0x1d42d2(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x1699e7(%rip), %rsi    # 0x1e86ff
movq (%rsp), %r8
xorl %eax, %eax
movq %r11, 0x18(%rsp)
callq 0x22b0
movq (%rsp), %rsi
movq 0x18(%rsp), %r11
jmp 0x7ecf2
movq 0x8(%rsp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
je 0x7ed78
xorl %eax, %eax
xorl %r9d, %r9d
movl %eax, %esi
cmpq %rsi, %rbp
jbe 0x7ed73
movq (%r14,%rsi,8), %rsi
shrq $0x2, %rsi
cmpq %rdx, %rsi
ja 0x7ed6d
movq 0x8(%r14,%rax,8), %rsi
shrq $0x2, %rsi
cmpq %rcx, %rsi
sbbq $-0x1, %r9
addq $0x2, %rax
jmp 0x7ed49
testq %r9, %r9
jne 0x7ed97
movq %r11, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d4242(%rip), %rax    # 0x252fe0
movq (%rax), %rdi
leaq 0x169957(%rip), %rsi    # 0x1e86ff
xorl %r8d, %r8d
xorl %eax, %eax
movq %r11, %rbx
callq 0x22b0
movq %rbx, %r11
jmp 0x7ed78