// File: /workspace/github_cpp_makefile_star2_O1/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, 0x14(%rsp)
cmpl $0x2, 0x10(%rdi)
jb 0xb902c
cmpl $0x0, 0xc(%rdi)
je 0xb902c
movq %r9, %r14
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq 0x88(%rsp), %rbx
movq %rdi, 0x8(%rsp)
movl 0x14(%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x8(%rsp), %rdx
cmpl $0x0, 0x10(%rdx)
je 0xb8d9d
xorl %eax, %eax
movq 0x18(%rdx), %rcx
movq (%rcx,%rax,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%rbx,%rcx)
incq %rax
movl 0x10(%rdx), %ecx
cmpq %rcx, %rax
jb 0xb8d82
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
cmpl $0x0, 0x10(%rdx)
je 0xb8e2f
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x18(%rdx), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movl (%rdx), %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 %r14, %r9
movq %r13, %r11
movq %r12, %r13
movq %r15, %r12
movq %r14, %r15
leaq 0x1c(%rsp), %r14
pushq %r14
movq %r15, %r14
movq %r12, %r15
movq %r13, %r12
movq %r11, %r13
pushq %rax
pushq %r10
leaq 0x40(%rsp), %rax
pushq %rax
pushq 0xb0(%rsp)
callq 0xb84c6
movq 0x38(%rsp), %rdx
addq $0x30, %rsp
incq %rbx
movl 0x10(%rdx), %eax
cmpq %rax, %rbx
jb 0xb8db8
cmpl $0x0, 0x10(%rdx)
je 0xb8e93
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x18(%rdx), %rax
movq (%rax,%rbx,8), %rdi
movl %ebp, %esi
addl %edi, %ebp
shrq $0x20, %rdi
movl (%rdx), %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 %r14, %r9
pushq $0x0
pushq %rax
pushq %r10
leaq 0x40(%rsp), %rax
pushq %rax
pushq 0xb0(%rsp)
callq 0xb84c6
movq 0x38(%rsp), %rdx
addq $0x30, %rsp
incq %rbx
movl 0x10(%rdx), %eax
cmpq %rax, %rbx
jb 0xb8e39
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, (%rsp)
movq %rbx, %rdi
callq 0x9d53f
testq %rbp, %rbp
je 0xb8fc3
movl $0x1, %esi
xorl %ecx, %ecx
xorl %ebx, %ebx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%rsp), %rdi
movl %r12d, %eax
movl %esi, %edx
movq (%rdi,%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 0xb8f86
movq %rsi, 0x18(%rsp)
movl %eax, %edx
movl %r12d, %ecx
movl $0x1, %eax
xorl %r9d, %r9d
movq %r9, %r8
movq -0x8(%r14,%rax,8), %r11
movq (%r14,%rax,8), %rsi
shrq $0x2, %r11
shrq $0x2, %rsi
cmpq %rdx, %r11
setbe %r9b
cmpq %rcx, %rsi
setae %r10b
andb %r9b, %r10b
movzbl %r10b, %r9d
addq %r8, %r9
cmpq %rdx, %rcx
jne 0xb8f70
cmpq %rdx, %r11
sete %r8b
cmpq %rdx, %rsi
sete %sil
orb %r8b, %sil
movzbl %sil, %esi
subq %rsi, %r9
leal 0x1(%rax), %esi
addq $0x2, %rax
cmpq %rsi, %rbp
ja 0xb8f27
cmpq %r13, %r9
movq 0x18(%rsp), %rsi
jne 0xb8f9d
orq $0x1, %r15
addq %r15, %r13
incl %ebx
movq %rbx, %rcx
cmpq %rbx, %rbp
ja 0xb8eea
jmp 0xb8fca
movq 0x1d103c(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x171661(%rip), %rsi    # 0x22a60f
movq %r13, %r8
xorl %eax, %eax
callq 0x22b0
movq 0x18(%rsp), %rsi
movq (%rsp), %rdi
jmp 0xb8f86
xorl %r12d, %r12d
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movl (%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %r12d
je 0xb901f
movl %r12d, %edx
testq %rbp, %rbp
je 0xb9017
movl $0x1, %eax
xorl %r9d, %r9d
movq -0x8(%r14,%rax,8), %rsi
shrq $0x2, %rsi
cmpq %rdx, %rsi
ja 0xb9009
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 0xb8fec
jmp 0xb901a
xorl %r9d, %r9d
testq %r9, %r9
jne 0xb903b
callq 0x2220
movq %r14, %rdi
callq 0x2220
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1d0f9e(%rip), %rax    # 0x289fe0
movq (%rax), %rdi
leaq 0x1715c3(%rip), %rsi    # 0x22a60f
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x22b0
movq (%rsp), %rdi
jmp 0xb901f