// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: push_correct1(window_list*, window_list_alloc*, cns_gfa*, asg32_v*, unsigned int)
uint64_t push_correct1(window_list *idx, window_list_alloc *res, cns_gfa *cns, asg32_v *rc, uint32_t bl)
{
    // fprintf(stderr, "[M::%s]\t", __func__);
    uint64_t nec = 0; uint32_t k, l, i, ff, sl, sk, bs = cns->off, be = bl + cns->off, bend = cns->off;///[bs, be)
    if(rc->n) {///it is possible that rc->n == 0, which means there is a deletion
        for (k = 1, l = 0; k <= rc->n; ++k) {
            ff = 0; sl = sk = 0;
            if(k == rc->n) {
                if(l < rc->n) sl = ((rc->a[l] >= bs && rc->a[l] < be)?1:0); 
                ff = 1;
            } else {
                sl = ((rc->a[l] >= bs && rc->a[l] < be)?1:0); 
                sk = ((rc->a[k] >= bs && rc->a[k] < be)?1:0);
                if(sl != sk) {
                    ff = 1;
                } else if((sl == 1) && ((rc->a[k] - rc->a[l]) != (k - l))) {
                    ff = 1;
                }
            }

            if(!ff) continue;

            if(sl) { ///match
                if(rc->a[l] > bend) {///deltetion [bend, rc->a[l])
                    push_trace_bp(((asg16_v *)(&(res->c))), 3, (uint16_t)-1, rc->a[l] - bend, ((idx->clen>0)?1:0)); 
                    idx->clen = res->c.n - idx->cidx; nec += rc->a[l] - bend;
                    // fprintf(stderr, "%uD", rc->a[l] - bend);
                }

                ///push match
                push_trace_bp(((asg16_v *)(&(res->c))), 0, (uint16_t)-1, rc->a[k-1] + 1 - rc->a[l], ((idx->clen>0)?1:0)); 
                idx->clen = res->c.n - idx->cidx;

                // fprintf(stderr, "%uM", rc->a[k-1] + 1 - rc->a[l]);

                bend = rc->a[k-1] + 1;
            } else { ///unmatch
                for (i = l; i < k; i++) {
                    push_trace_bp(((asg16_v *)(&(res->c))), 2, cns->a[rc->a[i]].c, 1, ((idx->clen>0)?1:0)); 
                    idx->clen = res->c.n - idx->cidx; nec++;
                    // fprintf(stderr, "I");
                }
            }
            l = k;
        }
    } 
    
    ///push remaining deletion
    if(be > bend) {
        push_trace_bp(((asg16_v *)(&(res->c))), 3, (uint16_t)-1, be - bend, ((idx->clen>0)?1:0)); 
        idx->clen = res->c.n - idx->cidx; nec += be - bend;
        // fprintf(stderr, "%uD", be - bend);
    }
    // fprintf(stderr, "\n");
    return nec;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, %ebx
movq %rcx, %r13
movq %rsi, %r15
movq %rdi, %r14
movq %rdx, 0x30(%rsp)
movl 0x20(%rdx), %edx
addl %edx, %ebx
movq (%rcx), %rcx
xorl %eax, %eax
testq %rcx, %rcx
je 0x140298
movq %rbx, 0x20(%rsp)
movq %r15, 0x28(%rsp)
addq $0x18, %r15
pushq $0x1
popq %rsi
movl %esi, %ebp
xorl %r12d, %r12d
movl %edx, 0x1c(%rsp)
movl %edx, 0xc(%rsp)
movq %rbp, 0x10(%rsp)
movl %ebp, %ebp
cmpq %rbp, %rcx
jb 0x140288
movl %eax, %ebx
jne 0x14013c
cmpq %rbx, %rcx
jbe 0x14026a
movq 0x10(%r13), %rdx
movl (%rdx,%rbx,4), %eax
cmpl 0x1c(%rsp), %eax
setae %cl
cmpl 0x20(%rsp), %eax
setb %al
testb %al, %cl
je 0x14026a
jmp 0x1401a1
movq 0x10(%r13), %rdx
movl (%rdx,%rbx,4), %ecx
movl 0x1c(%rsp), %r9d
cmpl %r9d, %ecx
setae %dil
movq 0x20(%rsp), %r8
cmpl %r8d, %ecx
setb %sil
andb %dil, %sil
movl (%rdx,%rbp,4), %edi
cmpl %r9d, %edi
setae %r9b
cmpl %r8d, %edi
setb %r8b
andb %r9b, %r8b
cmpb $0x1, %sil
jne 0x14018f
testb %r8b, %r8b
je 0x14018f
subl %ecx, %edi
movq 0x10(%rsp), %rcx
subl %eax, %ecx
cmpl %ecx, %edi
jne 0x1401a1
jmp 0x140278
cmpb %r8b, %sil
je 0x140278
testb %sil, %sil
je 0x14026a
movl (%rdx,%rbx,4), %esi
movl %esi, %ecx
movl 0x1c(%r14), %eax
movl 0xc(%rsp), %ebp
subl %ebp, %ecx
jbe 0x1401e4
xorl %r8d, %r8d
testl %eax, %eax
setne %r8b
movq %r15, %rdi
pushq $0x3
popq %rsi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x14f3f8
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rdx
movl (%rdx,%rbx,4), %esi
movl %esi, %ecx
subl %ebp, %ecx
addq %rcx, %r12
movq 0x10(%rsp), %rbp
leal -0x1(%rbp), %ebx
movl (%rdx,%rbx,4), %ecx
subl %esi, %ecx
incl %ecx
xorl %r8d, %r8d
testl %eax, %eax
setne %r8b
movq %r15, %rdi
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x14f3f8
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rax
movl (%rax,%rbx,4), %eax
incl %eax
movl %eax, 0xc(%rsp)
jmp 0x140274
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%r13), %rcx
movl (%rcx,%rbx,4), %ecx
imulq $0x28, %rcx, %rcx
movl (%rax,%rcx), %edx
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
andl $0x3, %edx
movq %r15, %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rcx
callq 0x14f3f8
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
incq %r12
incq %rbx
cmpq %rbp, %rbx
jb 0x140225
movq 0x10(%rsp), %rbp
movl %ebp, %eax
jmp 0x14027d
movq 0x10(%rsp), %rbp
incl %ebp
movq (%r13), %rcx
jmp 0x140100
movl 0xc(%rsp), %edx
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r15
jmp 0x14029b
xorl %r12d, %r12d
subl %edx, %ebx
jbe 0x1402cd
leaq 0x18(%r15), %rdi
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
pushq $0x3
popq %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl %ebx, %ecx
callq 0x14f3f8
movl 0x18(%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
addq %rbx, %r12
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq