// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movl %esi, -0x1c(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x10(%rax)
jb 0xf3421
movq -0x18(%rbp), %rax
cmpl $0x0, 0xc(%rax)
jne 0xf3426
jmp 0xf3a1a
movq 0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x2100
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf347a
movq 0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x44(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf3448
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf356d
movl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %edi
movl -0x4c(%rbp), %eax
movl %eax, %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %r9
movq 0x18(%rbp), %r14
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r11d
movq -0x18(%rbp), %rax
movl (%rax), %r10d
shrl $0x1f, %r10d
leaq -0x70(%rbp), %rbx
leaq -0x1c(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0xf2680
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf34c6
movl $0x0, -0x48(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xf3622
movl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, %edi
movl -0x4c(%rbp), %eax
movl %eax, %esi
movq -0x28(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %r9
movq 0x18(%rbp), %rbx
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %r10d
movq -0x18(%rbp), %rax
movl (%rax), %eax
shrl $0x1f, %eax
leaq -0x70(%rbp), %r11
xorl %r14d, %r14d
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movl %eax, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0xf2680
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf357b
movq $0x0, -0x90(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2270
movq %rax, -0x80(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rdx
shlq $0x3, %rdx
callq 0x2180
movq -0x60(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x70(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0xc90e0
movq $0x0, -0x98(%rbp)
movl $0x0, -0xa8(%rbp)
movl $0x1, -0xb0(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpq -0x70(%rbp), %rax
jae 0xf38d9
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
cmpq $0x0, %rax
je 0xf36eb
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0xf36fd
movq -0x98(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
andq $0x1, %rax
movl %eax, -0xac(%rbp)
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x2, %rax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
subl -0xa8(%rbp), %eax
cmpl $0x0, %eax
ja 0xf375c
movl -0xa4(%rbp), %eax
cmpl -0xa8(%rbp), %eax
jne 0xf38b3
movl -0xb0(%rbp), %eax
cmpl -0xac(%rbp), %eax
je 0xf38b3
movl -0xa8(%rbp), %eax
movq %rax, -0xc0(%rbp)
movl -0xa4(%rbp), %eax
movq %rax, -0xc8(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq $0x0, -0xe8(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpq -0x90(%rbp), %rax
jae 0xf386d
movq -0x80(%rbp), %rax
movl -0xb4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x2, %rax
movq %rax, -0xd8(%rbp)
movq -0x80(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x2, %rax
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
ja 0xf3815
movq -0xe0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jb 0xf3815
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jne 0xf3857
movq -0xd8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
je 0xf3845
movq -0xe0(%rbp), %rax
cmpq -0xc0(%rbp), %rax
jne 0xf3857
movq -0xe8(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xf3859
movl -0xb4(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xb4(%rbp)
jmp 0xf3799
movq -0xe8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
je 0xf38b1
movq 0x2d075c(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0xc0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
movq -0xd0(%rbp), %r8
movq -0xe8(%rbp), %r9
leaq 0x293983(%rip), %rsi    # 0x38722d
movb $0x0, %al
callq 0x22a0
jmp 0xf38b3
movl -0xa4(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xf36a7
movl -0xa8(%rbp), %eax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
je 0xf3a08
movl -0xa8(%rbp), %eax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq %rax, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movq $0x0, -0xe8(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpq -0x90(%rbp), %rax
jae 0xf39c2
movq -0x80(%rbp), %rax
movl -0xb4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x2, %rax
movq %rax, -0xd8(%rbp)
movq -0x80(%rbp), %rax
movl -0xb4(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x2, %rax
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
ja 0xf39ac
movq -0xe0(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jb 0xf39ac
movq -0xe8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe8(%rbp)
jmp 0xf39ae
movl -0xb4(%rbp), %eax
addl $0x2, %eax
movl %eax, -0xb4(%rbp)
jmp 0xf3934
movq -0xe8(%rbp), %rax
cmpq -0xd0(%rbp), %rax
je 0xf3a06
movq 0x2d0607(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
movq -0xc0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
movq -0xd0(%rbp), %r8
movq -0xe8(%rbp), %r9
leaq 0x29382e(%rip), %rsi    # 0x38722d
movb $0x0, %al
callq 0x22a0
jmp 0xf3a08
movq -0x60(%rbp), %rdi
callq 0x2210
movq -0x80(%rbp), %rdi
callq 0x2210
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r14
popq %rbp
retq
nopw %cs:(%rax,%rax)