// File: /workspace/github_cpp_makefile_star2_O3/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
movl %esi, 0x1c(%rsp)
cmpl $0x2, 0x10(%rdi)
jb 0xb9ed1
movq %rdi, %rbx
cmpl $0x0, 0xc(%rdi)
je 0xb9ed1
movq %r9, %r15
movq %r8, %r12
movq %rcx, %r13
movq %rdx, %r14
movq 0x88(%rsp), %rbp
movl 0x14(%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rbp, %rdi
xorl %esi, %esi
callq 0x20e0
cmpl $0x0, 0x10(%rbx)
movq %rbx, 0x8(%rsp)
je 0xb9d37
xorl %eax, %eax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%rbp,%rcx)
incq %rax
movl 0x10(%rbx), %ecx
cmpq %rcx, %rax
jb 0xb9c05
xorl %ebp, %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testl %ecx, %ecx
je 0xb9d4a
leaq 0x20(%rsp), %r11
xorl %ebx, %ebx
movq %r15, %r9
movq %r12, %r8
movq %r13, %rcx
movq 0x8(%rsp), %rsi
movq %r14, 0x10(%rsp)
movq 0x18(%rsi), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movq 0x8(%rsp), %rax
movl (%rax), %eax
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shrl $0x1f, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdx
movq %rcx, %r14
movq %r8, %r15
movq %r9, %r13
leaq 0x24(%rsp), %r12
pushq %r12
pushq %rax
pushq %r10
pushq %r11
pushq 0xb0(%rsp)
callq 0xb93e3
movq 0x38(%rsp), %rsi
movq %r14, %rcx
movq %r15, %r8
movq %r13, %r9
leaq 0x50(%rsp), %r11
addq $0x30, %rsp
incq %rbx
movl 0x10(%rsi), %eax
cmpq %rax, %rbx
jb 0xb9c56
testl %eax, %eax
je 0xb9d30
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x18(%rsi), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movq 0x8(%rsp), %rax
movl (%rax), %eax
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
shrl $0x1f, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdx
movq %r14, %rcx
movq %r15, %r8
movq %r13, %r9
pushq $0x0
pushq %rax
pushq %r10
pushq %r11
pushq 0xb0(%rsp)
callq 0xb93e3
movq 0x38(%rsp), %rsi
leaq 0x50(%rsp), %r11
addq $0x30, %rsp
incq %rbx
movl 0x10(%rsi), %eax
cmpq %rax, %rbx
jb 0xb9cce
movq 0x20(%rsp), %rbp
jmp 0xb9d4a
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
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 0x9dc69
movq %rbx, %r11
testq %rbp, %rbp
je 0xb9ec1
movl $0x1, %esi
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %r12d, %eax
movl %esi, %edx
movq (%r11,%rcx,8), %r12
movl %r12d, %esi
andl $0x1, %esi
cmpq $0x1, %rsi
movl $0x0, %r15d
sbbq %r15, %r15
shrq $0x2, %r12
movl %eax, %ecx
xorl %r12d, %ecx
xorl %esi, %edx
orl %ecx, %edx
je 0xb9e33
movq %rsi, 0x10(%rsp)
movl %eax, %edx
movl %r12d, %ecx
movl $0x1, %eax
xorl %r9d, %r9d
movq %r9, %r8
movq -0x8(%r14,%rax,8), %rdi
movq (%r14,%rax,8), %rsi
shrq $0x2, %rdi
shrq $0x2, %rsi
cmpq %rdx, %rdi
setbe %r9b
cmpq %rcx, %rsi
setae %r10b
andb %r9b, %r10b
movzbl %r10b, %r9d
addq %r8, %r9
cmpq %rdx, %rcx
jne 0xb9e1d
cmpq %rdx, %rdi
sete %dil
cmpq %rdx, %rsi
sete %sil
orb %dil, %sil
movzbl %sil, %esi
subq %rsi, %r9
leal 0x1(%rax), %esi
addq $0x2, %rax
cmpq %rsi, %rbp
ja 0xb9dd4
cmpq %r13, %r9
movq 0x10(%rsp), %rsi
jne 0xb9e4a
orq $0x1, %r15
addq %r15, %r13
incl %ebx
movq %rbx, %rcx
cmpq %rbx, %rbp
ja 0xb9d97
jmp 0xb9e76
movq 0x1db18f(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x17c9a4(%rip), %rsi    # 0x2367ff
movq %r13, %r8
xorl %eax, %eax
movq %r11, 0x40(%rsp)
callq 0x22b0
movq 0x10(%rsp), %rsi
movq 0x40(%rsp), %r11
jmp 0xb9e33
movq 0x8(%rsp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %r12d, %ecx
je 0xb9ec1
movl %r12d, %edx
movl $0x1, %eax
xorl %r9d, %r9d
movq -0x8(%r14,%rax,8), %rsi
shrq $0x2, %rsi
cmpq %rdx, %rsi
ja 0xb9eb0
movq (%r14,%rax,8), %rsi
shrq $0x2, %rsi
cmpq %rcx, %rsi
sbbq $-0x1, %r9
leal 0x1(%rax), %esi
addq $0x2, %rax
cmpq %rsi, %rbp
ja 0xb9e93
testq %r9, %r9
jne 0xb9ee0
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 0x1db0f9(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
leaq 0x17c90e(%rip), %rsi    # 0x2367ff
xorl %r8d, %r8d
xorl %eax, %eax
movq %r11, %rbx
callq 0x22b0
movq %rbx, %r11
jmp 0xb9ec1