// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movl %r8d, %ebp
movq %rcx, %r13
movq %rdi, %r14
movl 0x20(%rdx), %r10d
addl %r10d, %ebp
movq (%rcx), %rcx
xorl %r12d, %r12d
testq %rcx, %rcx
movq %rsi, 0x20(%rsp)
je 0x183f53
movq %rdx, 0x28(%rsp)
leaq 0x18(%rsi), %r15
movl $0x1, %r11d
movl $0x1, %esi
movl %r10d, 0x4(%rsp)
movq $0x0, 0x8(%rsp)
xorl %eax, %eax
movl %r10d, 0x14(%rsp)
movq %rbp, 0x18(%rsp)
cmpq %rsi, %rcx
jne 0x183d94
movl %eax, %esi
cmpq %rsi, %rcx
jbe 0x183e97
movq 0x10(%r13), %rdx
movl (%rdx,%rsi,4), %ecx
cmpl %r10d, %ecx
setae %sil
cmpl %ebp, %ecx
setb %cl
testb %cl, %sil
jne 0x183ded
jmp 0x183e97
movq 0x10(%r13), %rdx
movl %eax, %ecx
movl (%rdx,%rcx,4), %ecx
cmpl %r10d, %ecx
setae %r8b
cmpl %ebp, %ecx
setb %dil
andb %r8b, %dil
movl (%rdx,%rsi,4), %esi
cmpl %r10d, %esi
setae %r9b
cmpl %ebp, %esi
setb %r8b
andb %r9b, %r8b
cmpb $0x1, %dil
jne 0x183ddb
testb %r8b, %r8b
je 0x183ddb
subl %ecx, %esi
movl %r11d, %ecx
subl %eax, %ecx
cmpl %ecx, %esi
jne 0x183ded
jmp 0x183f3b
cmpb %r8b, %dil
je 0x183f3b
testb %dil, %dil
je 0x183e97
movl %eax, %ebx
movl (%rdx,%rbx,4), %esi
movl %esi, %ecx
movl 0x1c(%r14), %eax
movl 0x4(%rsp), %r9d
subl %r9d, %ecx
jbe 0x183e45
xorl %r8d, %r8d
testl %eax, %eax
setne %r8b
movq %r15, %rdi
movl $0x3, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movq %r11, %rbp
movl %r9d, 0x4(%rsp)
callq 0x196694
movq %rbp, %r11
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rdx
movl (%rdx,%rbx,4), %esi
movl %esi, %ecx
subl 0x4(%rsp), %ecx
addq %rcx, 0x8(%rsp)
leal -0x1(%r11), %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
movq %r11, %rbp
callq 0x196694
movq %rbp, %r11
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rax
movl (%rax,%rbx,4), %eax
incl %eax
movl %eax, 0x4(%rsp)
movl %ebp, %eax
movl 0x14(%rsp), %r10d
movq 0x18(%rsp), %rbp
jmp 0x183f3b
cmpl %eax, %r11d
jbe 0x183f38
movq %r11, 0x38(%rsp)
movl %r11d, %ebx
movl %eax, %ebp
movq %r12, 0x40(%rsp)
subl %eax, %r12d
movq %r12, 0x30(%rsp)
movl 0x1c(%r14), %eax
movq %r13, %r12
movq 0x28(%rsp), %r13
movq 0x10(%r13), %rcx
movq 0x10(%r12), %rdx
movl (%rdx,%rbp,4), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %edx
xorl %r8d, %r8d
testl %eax, %eax
setne %r8b
andl $0x3, %edx
movq %r15, %rdi
movl $0x2, %esi
movl $0x1, %ecx
callq 0x196694
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
incq %rbp
cmpq %rbp, %rbx
jne 0x183ec3
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %rcx, %rax
incq %rax
movq %rax, 0x8(%rsp)
movq 0x38(%rsp), %r11
movl %r11d, %eax
movq 0x18(%rsp), %rbp
movl 0x14(%rsp), %r10d
movq %r12, %r13
movq 0x40(%rsp), %r12
jmp 0x183f3b
movl %r11d, %eax
incl %r11d
movq (%r13), %rcx
incl %r12d
movq %r11, %rsi
cmpq %r11, %rcx
jae 0x183d67
jmp 0x183f61
movq $0x0, 0x8(%rsp)
movl %r10d, 0x4(%rsp)
subl 0x4(%rsp), %ebp
jbe 0x183fa2
movq 0x20(%rsp), %rbx
leaq 0x18(%rbx), %rdi
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
movl $0x3, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl %ebp, %ecx
callq 0x196694
movl 0x18(%rbx), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x8(%rsp), %rax
addq %rbp, %rax
jmp 0x183fa7
movq 0x8(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq