// File: /workspace/github_cpp_makefile_star2_O1/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, %r12d
movq %rdi, %r14
movl 0x20(%rdx), %r9d
addl %r9d, %r12d
cmpq $0x0, (%rcx)
movq %rsi, 0x28(%rsp)
je 0x17d056
movq %rcx, %r13
movq (%rcx), %rcx
xorl %r15d, %r15d
testq %rcx, %rcx
je 0x17d056
movq %rdx, 0x30(%rsp)
leaq 0x18(%rsi), %rdi
movl $0x1, %ebx
movl $0x1, %edx
movl %r9d, %r11d
movq $0x0, (%rsp)
xorl %eax, %eax
movq %r12, 0x20(%rsp)
movl %r9d, 0xc(%rsp)
movq %rdi, 0x10(%rsp)
movl %eax, %ebp
cmpq %rdx, %rcx
jne 0x17cea7
cmpq %rbp, %rcx
jbe 0x17cef4
movq 0x10(%r13), %rcx
movl (%rcx,%rbp,4), %ecx
cmpl %r9d, %ecx
setae %dl
cmpl %r12d, %ecx
setb %cl
andb %dl, %cl
xorl %edx, %edx
jmp 0x17cef8
movq 0x10(%r13), %r10
movl (%r10,%rbp,4), %esi
cmpl %r9d, %esi
setae %r8b
cmpl %r12d, %esi
setb %cl
andb %r8b, %cl
movl (%r10,%rdx,4), %r10d
cmpl %r9d, %r10d
setae %dl
cmpl %r12d, %r10d
setb %r8b
andb %dl, %r8b
movl %ecx, %edx
xorb %r8b, %dl
xorb $0x1, %dl
cmpb $0x1, %cl
jne 0x17cef8
testb %r8b, %r8b
je 0x17cef8
subl %esi, %r10d
movl %ebx, %edx
subl %eax, %edx
cmpl %edx, %r10d
sete %dl
jmp 0x17cef8
xorl %edx, %edx
xorl %ecx, %ecx
testb %dl, %dl
jne 0x17d03f
testb %cl, %cl
je 0x17cfa3
movq 0x10(%r13), %rax
movl (%rax,%rbp,4), %ecx
subl %r11d, %ecx
jbe 0x17cf4f
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
movl $0x3, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl %r11d, %r12d
callq 0x18d881
movq 0x10(%rsp), %rdi
movl (%rdi), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rax
movl (%rax,%rbp,4), %eax
subl %r12d, %eax
addq %rax, (%rsp)
movq 0x10(%r13), %rax
leal -0x1(%rbx), %r12d
movl (%rax,%r12,4), %ecx
subl (%rax,%rbp,4), %ecx
incl %ecx
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
callq 0x18d881
movq 0x10(%rsp), %rdi
movl (%rdi), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r13), %rax
movl (%rax,%r12,4), %r11d
incl %r11d
movl %ebx, %eax
movq 0x20(%rsp), %r12
movl 0xc(%rsp), %r9d
jmp 0x17d03f
cmpl %eax, %ebx
jbe 0x17d03d
movl %r11d, 0x1c(%rsp)
movl %ebx, %r12d
movq %r15, 0x40(%rsp)
subl %eax, %r15d
movq %r15, 0x38(%rsp)
movq %r13, %r15
movq 0x30(%rsp), %r13
movq 0x10(%r13), %rax
movq 0x10(%r15), %rcx
movl (%rcx,%rbp,4), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %edx
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
andl $0x3, %edx
movl $0x2, %esi
movl $0x1, %ecx
callq 0x18d881
movq 0x10(%rsp), %rdi
movl (%rdi), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
incq %rbp
cmpq %rbp, %r12
jne 0x17cfc8
movq (%rsp), %rax
movq 0x38(%rsp), %rcx
addq %rcx, %rax
incq %rax
movq %rax, (%rsp)
movl %ebx, %eax
movq 0x20(%rsp), %r12
movl 0xc(%rsp), %r9d
movq %r15, %r13
movq 0x40(%rsp), %r15
movl 0x1c(%rsp), %r11d
jmp 0x17d03f
movl %ebx, %eax
incl %ebx
movq (%r13), %rcx
incl %r15d
movq %rbx, %rdx
cmpq %rbx, %rcx
jae 0x17ce82
jmp 0x17d061
movq $0x0, (%rsp)
movl %r9d, %r11d
subl %r11d, %r12d
jbe 0x17d0a1
movq 0x28(%rsp), %rbx
leaq 0x18(%rbx), %rdi
xorl %r8d, %r8d
cmpl $0x0, 0x1c(%r14)
setne %r8b
movl $0x3, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl %r12d, %ecx
callq 0x18d881
movl 0x18(%rbx), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq (%rsp), %rax
addq %r12, %rax
jmp 0x17d0a5
movq (%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq