// File: chhylp123[P]hifiasm/ecovlp.cpp
// Function: push_cns_anchor(overlap_region*, All_reads*, unsigned long, unsigned long, char*, unsigned long, UC_Read*, bit_extz_t*, cc_idx_t*, overlap_region*, unsigned long, unsigned long, double, asg32_v*, cns_gfa*, unsigned int)
uint64_t push_cns_anchor(overlap_region* ol, All_reads *rref, uint64_t s, uint64_t e, char* qstr, uint64_t ql, UC_Read* tu, bit_extz_t *exz, cc_idx_t *idx, overlap_region *aux_o, uint64_t is_tail, uint64_t occ_tot, double occ_max, asg32_v* b32, cns_gfa *cns, uint32_t rid)
{
    if((!is_tail) && (s >= e)) return 0;//if s >= e && is_tail = 1, gen the cns of the last a few bases -> s == e == ql
    // fprintf(stderr, "\n****************[M::%s::M] [%lu, %lu)****************\n", __func__, s, e);
    window_list *p = NULL; uint64_t e0 = 0, nec = 0; uint32_t rc;
    if(aux_o->w_list.n > 0) {
        p = &(aux_o->w_list.a[aux_o->w_list.n-1]);
        e0 = p->x_end+1;
        ///make sure e > s
    }
    assert(s >= e0);
    if((s == e) && (is_tail == 1) && (s == e0)) return 0;///in this case, s == e == ql

    if(aux_o->w_list.n == 0) {
        kv_pushp(window_list, aux_o->w_list, &p);
        p->x_start = -1; p->x_end = -1;
        p->clen = p->cidx = 0;
    }

    if(((!is_tail) && (s > 0)) || ((is_tail) && (s > e0))) {
        // fprintf(stderr, ">>>>>>[M::%s::M] [%lu, %lu), ec::%u\n", __func__, e0, s, cal_cigar_xlen_fhc(aux_o));
        // fprintf(stderr, ">>>>>>[M::%s::M] [%lu, %lu)\n", __func__, e0, s);
        if (cns_gen0(ol, rref, e0, s, ql, tu, idx, occ_tot, occ_max, b32, &rc)) {
            if(p->x_start == -1 || p->x_end == -1) {///hasn't neem set
                p->x_start = e0; p->x_end = s-1;
            }
            // nec += push_correct0(p, &(aux_o->w_list), (uint32_t)-1, rc);
            nec += push_correct0_fhc(p, &(aux_o->w_list), (uint32_t)-1, rc, qstr + e0);
            // fprintf(stderr, "-0-[M::%s::M]\n", __func__);
        } else {
            nec += cns_gen_full(ol, rref, e0, s, cns->cns_g_wl, qstr, tu, exz, idx, occ_tot, occ_max, b32, cns, ql, p, &(aux_o->w_list), rid);
            // output_cns_g(cns, e0, s); exit(1);
            // fprintf(stderr, "-1-[M::%s::M]\n", __func__);
        }
        p->x_end = s-1;

        // if((uint32_t)p->x_end + 1 != cal_cigar_xlen_fhc(aux_o)) {
        //     fprintf(stderr, "sb1::%u\n", cal_cigar_xlen_fhc(aux_o));
        // }
    }

    
    // fprintf(stderr, "------[M::%s::M] [%lu, %lu), ec::%u\n", __func__, s, e, cal_cigar_xlen_fhc(aux_o));
    // fprintf(stderr, "------[M::%s::M] [%lu, %lu)\n", __func__, s, e);
    if(p->x_start == -1 || p->x_end == -1) {///hasn't neem set
        p->x_start = s; p->x_end = e-1;
    }
    // nec += push_correct0(p, &(aux_o->w_list), e-s, (uint32_t)-1);    
    nec += push_correct0_fhc(p, &(aux_o->w_list), e-s, (uint32_t)-1, NULL);    
    p->x_end = e-1;
    // if((uint32_t)p->x_end + 1 != cal_cigar_xlen_fhc(aux_o)) {
    //     fprintf(stderr, "ta2::%u\n", cal_cigar_xlen_fhc(aux_o));
    // }
    return nec;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl 0x50(%rbp), %eax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movsd %xmm0, -0x68(%rbp)
cmpq $0x0, 0x30(%rbp)
jne 0x2536c2
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jb 0x2536c2
movq $0x0, -0x30(%rbp)
jmp 0x253a4b
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq 0x28(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jbe 0x253714
movq 0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x28(%rbp), %rcx
movq 0x48(%rcx), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x78(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x78(%rbp), %rax
jb 0x253720
jmp 0x25373f
leaq 0x13b970(%rip), %rdi    # 0x38f097
leaq 0x13b4dd(%rip), %rsi    # 0x38ec0b
movl $0x847, %edx            # imm = 0x847
leaq 0x13b965(%rip), %rcx    # 0x38f09f
callq 0x2130
movq -0x48(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x253767
cmpq $0x1, 0x30(%rbp)
jne 0x253767
movq -0x48(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x253767
movq $0x0, -0x30(%rbp)
jmp 0x253a4b
movq 0x28(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x25383b
jmp 0x253778
movq 0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x28(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jne 0x2537ea
movq 0x28(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x2537a9
movq 0x28(%rbp), %rax
movq 0x50(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x2537b7
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x2537b7
movq -0x90(%rbp), %rcx
movq 0x28(%rbp), %rax
movq %rcx, 0x50(%rax)
movq 0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x28(%rbp), %rax
movq 0x50(%rax), %rsi
shlq $0x5, %rsi
callq 0x2360
movq %rax, %rcx
movq 0x28(%rbp), %rax
movq %rcx, 0x58(%rax)
movq 0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x28(%rbp), %rdx
movq 0x48(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x48(%rdx)
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
movq -0x70(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x70(%rbp), %rax
movl $0x0, 0x1c(%rax)
cmpq $0x0, 0x30(%rbp)
jne 0x253849
cmpq $0x0, -0x48(%rbp)
ja 0x253862
cmpq $0x0, 0x30(%rbp)
je 0x2539d5
movq -0x48(%rbp), %rax
cmpq -0x78(%rbp), %rax
jbe 0x2539d5
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x60(%rbp), %r8
movq 0x10(%rbp), %r9
movq 0x20(%rbp), %rbx
movq 0x38(%rbp), %r11
movsd -0x68(%rbp), %xmm0
movq 0x40(%rbp), %r10
leaq -0x84(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x24ee70
cmpq $0x0, %rax
je 0x253911
movq -0x70(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x2538c3
movq -0x70(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
jne 0x2538e0
movq -0x78(%rbp), %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x70(%rbp), %rdi
movq 0x28(%rbp), %rsi
addq $0x48, %rsi
movl -0x84(%rbp), %ecx
movq -0x58(%rbp), %r8
addq -0x78(%rbp), %r8
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x252df0
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x2539c4
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq 0x48(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, %r8d
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x20(%rbp), %r13
movq 0x38(%rbp), %r12
movsd -0x68(%rbp), %xmm0
movq 0x40(%rbp), %r15
movq 0x48(%rbp), %r14
movq -0x60(%rbp), %rbx
movq -0x70(%rbp), %r11
movq 0x28(%rbp), %r10
addq $0x48, %r10
movl 0x50(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, (%rsp)
movq -0xa0(%rbp), %rax
movq %rax, 0x8(%rsp)
movl -0x94(%rbp), %eax
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r11, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movl %eax, 0x48(%rsp)
callq 0x2529d0
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x70(%rbp), %rax
cmpl $-0x1, (%rax)
je 0x2539e8
movq -0x70(%rbp), %rax
cmpl $-0x1, 0x4(%rax)
jne 0x253a05
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, (%rax)
movq -0x50(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x70(%rbp), %rdi
movq 0x28(%rbp), %rsi
addq $0x48, %rsi
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
movl %eax, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %r8d
callq 0x252df0
addq -0x80(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x50(%rbp), %rax
subq $0x1, %rax
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x80(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)