// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/ecovlp.cpp
// Function: push_correct1_fhc(window_list*, window_list_alloc*, cns_gfa*, char*, UC_Read*, bit_extz_t*, asg32_v*, unsigned int, unsigned int)
uint64_t push_correct1_fhc(window_list *idx, window_list_alloc *res, cns_gfa *cns, char* qstr, UC_Read* tu, bit_extz_t *exz, asg32_v *rc, uint32_t bl, uint32_t rid)
{
    // fprintf(stderr, "[M::%s]\trc->n::%u\tbl::%u\n", __func__, (uint32_t)rc->n, bl);
    uint64_t nec = 0; uint32_t k, l, i, ff, sl, sk, bs = cns->off, be = bl + cns->off, bend = cns->off, is_i = 0, sc0 = res->c.n;///[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])
                    if(is_i && exz) {
                        nec += push_correct1_fhc_indel_exz(((asg16_v *)(&(res->c))), sc0, idx, cns, qstr, tu, exz, cns->off, 3, rc->a[l] - bend, bend-cns->off);
                    } else {
                        for (i = bend; i < rc->a[l]; i++) {
                            push_trace_bp_f(((asg16_v *)(&(res->c))), 3, cns->a[i].c, (uint16_t)-1,  1, ((idx->clen>0)?1:0));
                            idx->clen = res->c.n - idx->cidx; nec++;
                        }
                    }
                }

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

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %r11
movq %r8, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r14
movq 0xa0(%rsp), %r10
movl 0x20(%rdx), %r12d
movl 0xa8(%rsp), %r9d
addl %r12d, %r9d
movl 0x18(%rsi), %eax
movq %rax, 0x28(%rsp)
addq $0x18, %r15
cmpq $0x0, (%r10)
je 0x17de81
movq (%r10), %rcx
xorl %edx, %edx
testq %rcx, %rcx
je 0x17de83
movq %r11, 0x40(%rsp)
movq %rbp, 0x18(%rsp)
movl $0x1, %r13d
movl $0x1, %edx
movq $0x0, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl %r12d, %r11d
xorl %eax, %eax
movq %r12, 0x48(%rsp)
movl %r9d, 0x14(%rsp)
xorl %ebp, %ebp
movl %eax, %ebx
cmpq %rdx, %rcx
jne 0x17dba8
cmpq %rbx, %rcx
jbe 0x17dbf2
movq 0x10(%r10), %rcx
movl (%rcx,%rbx,4), %ecx
cmpl %r12d, %ecx
setae %dl
cmpl %r9d, %ecx
setb %cl
andb %dl, %cl
xorl %edx, %edx
jmp 0x17dbf6
movq 0x10(%r10), %rdi
movl (%rdi,%rbx,4), %esi
cmpl %r12d, %esi
setae %r8b
cmpl %r9d, %esi
setb %cl
andb %r8b, %cl
movl (%rdi,%rdx,4), %edi
cmpl %r12d, %edi
setae %dl
cmpl %r9d, %edi
setb %r8b
andb %dl, %r8b
movl %ecx, %edx
xorb %r8b, %dl
xorb $0x1, %dl
cmpb $0x1, %cl
jne 0x17dbf6
testb %r8b, %r8b
je 0x17dbf6
subl %esi, %edi
movl %r13d, %edx
subl %eax, %edx
cmpl %edx, %edi
sete %dl
jmp 0x17dbf6
xorl %edx, %edx
xorl %ecx, %ecx
testb %dl, %dl
jne 0x17ddc6
testb %cl, %cl
je 0x17dc90
movq 0x10(%r10), %rax
movl (%rax,%rbx,4), %eax
subl %r11d, %eax
movq %rbp, 0x60(%rsp)
movq %r13, 0x20(%rsp)
jbe 0x17dd41
movq %r11, %rsi
movq 0x40(%rsp), %r11
testq %r11, %r11
setne %cl
cmpl $0x0, 0x10(%rsp)
setne %dl
testb %dl, %cl
je 0x17dddf
movq 0x18(%rsp), %rcx
movl 0x20(%rcx), %r10d
movq %rsi, %r12
subl %r10d, %r12d
subq $0x8, %rsp
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq %r14, %rdx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
pushq %r12
pushq %rax
pushq $0x3
pushq %r10
pushq %r11
callq 0x17d0b4
movq 0xd0(%rsp), %r10
addq $0x30, %rsp
movq %r15, %rbp
movq 0x8(%rsp), %r15
addq %rax, %r15
jmp 0x17dd49
movl $0x1, 0x10(%rsp)
cmpl %eax, %r13d
jbe 0x17ddc3
movq %r11, 0x58(%rsp)
movq %r13, 0x20(%rsp)
movl %r13d, %r12d
movl %ebp, %ecx
subl %eax, %ecx
movq %rcx, 0x50(%rsp)
movq 0x18(%rsp), %r13
movq 0x10(%r13), %rax
movq 0x10(%r10), %rcx
movl (%rcx,%rbx,4), %ecx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %ecx
xorl %r9d, %r9d
cmpl $0x0, 0x1c(%r14)
setne %r9b
andl $0x3, %ecx
movq %r15, %rdi
movl $0x2, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0x1, %r8d
callq 0x18d9ca
movq 0xa0(%rsp), %r10
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
incq %rbx
cmpq %rbx, %r12
jne 0x17dcbc
movq 0x8(%rsp), %rax
movq 0x50(%rsp), %rcx
addq %rcx, %rax
incq %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %r13
movl %r13d, %eax
movq 0x48(%rsp), %r12
movl 0x14(%rsp), %r9d
movq 0x58(%rsp), %r11
jmp 0x17ddc6
movq %r15, %rbp
movq 0x8(%rsp), %r15
movq %r15, 0x8(%rsp)
movq 0x10(%r10), %rax
movq 0x20(%rsp), %r13
leal -0x1(%r13), %r12d
movl (%rax,%r12,4), %r8d
subl (%rax,%rbx,4), %r8d
incl %r8d
xorl %r9d, %r9d
cmpl $0x0, 0x1c(%r14)
setne %r9b
movl $0x0, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movl $0xffff, %edx           # imm = 0xFFFF
movl $0xffff, %ecx           # imm = 0xFFFF
callq 0x18d9ca
movq 0xa0(%rsp), %r10
movl (%rbp), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
movq 0x10(%r10), %rax
movl (%rax,%r12,4), %r11d
incl %r11d
movl %r13d, %eax
movq %rbp, %r15
movq 0x48(%rsp), %r12
movl 0x14(%rsp), %r9d
movq 0x60(%rsp), %rbp
jmp 0x17ddc6
movl %r13d, %eax
incl %r13d
movq (%r10), %rcx
incl %ebp
movq %r13, %rdx
cmpq %r13, %rcx
jae 0x17db83
jmp 0x17de65
movq %r15, %rbp
movq 0x10(%r10), %rax
cmpl (%rax,%rbx,4), %esi
jae 0x17dd44
movl %esi, %r12d
leaq (,%r12,8), %rax
leaq (%rax,%rax,4), %r13
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax,%r13), %edx
xorl %r9d, %r9d
cmpl $0x0, 0x1c(%r14)
setne %r9b
andl $0x3, %edx
movq %rbp, %rdi
movl $0x3, %esi
movl $0xffff, %ecx           # imm = 0xFFFF
movl $0x1, %r8d
callq 0x18d9ca
movq 0xa0(%rsp), %r10
movl (%rbp), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
incq %r15
incq %r12
movq 0x10(%r10), %rax
movl (%rax,%rbx,4), %eax
addq $0x28, %r13
cmpq %rax, %r12
jb 0x17de03
jmp 0x17dd49
cmpl $0x0, 0x10(%rsp)
setne %dl
movl %r11d, %r12d
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %r11
jmp 0x17de85
xorl %edx, %edx
xorl %ecx, %ecx
movl %r9d, %eax
subl %r12d, %eax
jbe 0x17df51
movq %rcx, 0x8(%rsp)
testq %r11, %r11
setne %cl
testb %dl, %cl
je 0x17dedf
movl 0x20(%rbp), %r10d
subl %r10d, %r12d
subq $0x8, %rsp
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
pushq %r12
pushq %rax
pushq $0x3
pushq %r10
pushq %r11
callq 0x17d0b4
addq $0x30, %rsp
movq 0x8(%rsp), %rcx
addq %rax, %rcx
jmp 0x17df51
cmpl %r12d, %r9d
jbe 0x17df4c
movl %r12d, %ebx
movl %r9d, %eax
leaq (,%rbx,8), %rcx
leaq (%rcx,%rcx,4), %r12
subq %rax, %rbx
xorl %r13d, %r13d
movq 0x10(%rbp), %rax
movl (%rax,%r12), %edx
xorl %r9d, %r9d
cmpl $0x0, 0x1c(%r14)
setne %r9b
andl $0x3, %edx
movq %r15, %rdi
movl $0x3, %esi
movl $0xffff, %ecx           # imm = 0xFFFF
movl $0x1, %r8d
callq 0x18d9ca
movl (%r15), %eax
subl 0x18(%r14), %eax
movl %eax, 0x1c(%r14)
addq $0x28, %r12
decq %r13
cmpq %r13, %rbx
jne 0x17defc
movq 0x8(%rsp), %rcx
subq %r13, %rcx
jmp 0x17df51
movq 0x8(%rsp), %rcx
movq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq