// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: sub_ciagar_gen(overlap_region*, Candidates_list*, unsigned long, unsigned long, unsigned long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, double, long, unsigned long)
void sub_ciagar_gen(overlap_region *z, Candidates_list *cl, uint64_t s, uint64_t e, uint64_t wl, 
const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, UC_Read *tu, bit_extz_t *exz, double e_rate,
int64_t ql, uint64_t rid)
{
    uint64_t qs, qe, sid, eid, k, l, m, tot_e, c_e; int64_t q[2], t[2], o[2], mode, kocc;
    qs = (s/wl)*wl; if(qs < z->x_pos_s) qs = z->x_pos_s; if(qs > z->x_pos_e) return;
    qe = (e/wl)*wl; if(qe < e) qe += wl; if(qe > z->x_pos_e+1) qe = z->x_pos_e+1;
    if(qe <= 0) return;
    sid = get_win_id_by_s(z, qs, wl, NULL);
    eid = get_win_id_by_e(z, qe-1, wl, NULL) + 1;///must qe-1 instead of qe!!!!!!
    if(sid >= eid) return;
    // fprintf(stderr, "\n***[M::%s::rid->%lu] s::%lu, e::%lu, n_qs::%lu, n_qe::%lu, z::[%u, %u), sid::%lu, eid::%lu, w_list.n::%lu\n", 
    // __func__, rid, s, e, qs, qe, z->x_pos_s, z->x_pos_e+1, sid, eid, (uint64_t)z->w_list.n);
    for (k = sid+1, l = sid; k <= eid; k++) {//[sid, eid)
        if(k == eid || z->w_list.a[k].extra_end < 0) {
            if(k - l > 1 || z->w_list.a[l].extra_end >= 0) {
                q[0] = q[1] = t[0] = t[1] = -1; 
                o[0] = o[1] = -1; mode = -1; tot_e = 0;
                if(z->w_list.a[l].extra_end < 0) {
                    q[0] = z->w_list.a[l].x_end+1; 
                    if(z->w_list.a[l].y_end != -1) {
                        t[0] = z->w_list.a[l].y_end+1; 
                    }
                    if(z->w_list.a[l].extra_end != INT16_MIN) {
                        o[0] = -z->w_list.a[l].extra_end;
                    }
                } else {///first window
                    q[0] = qs; 
                    if(z->w_list.a[l].y_end != -1) {
                        c_e = z->w_list.a[l].error;
                    } else {
                        c_e = z->w_list.a[l].x_end + 1 - z->w_list.a[l].x_start;
                        if(c_e > THRESHOLD_MAX_SIZE) c_e = THRESHOLD_MAX_SIZE;
                    }
                    tot_e += c_e;
                }

                if(k > sid && k < eid && z->w_list.a[k].extra_end < 0) {
                    q[1] = z->w_list.a[k-1].x_end+1; 
                    if(z->w_list.a[k].y_end != -1) {
                        // if(z->w_list.a[k-1].y_end == -1) {
                        //     fprintf(stderr, "[M::%s::rid::%lu] k::%lu, sid::%lu, eid::%lu, k_y_end::%d, k-1_y_end::%d, xk[%d, %d)\n", 
                        //     __func__, rid, k, sid, eid, z->w_list.a[k].y_end, z->w_list.a[k-1].y_end,
                        //     z->w_list.a[k].x_start, z->w_list.a[k].x_end+1);
                        // }
                        assert(z->w_list.a[k-1].y_end != -1);
                        t[1] = z->w_list.a[k-1].y_end+1;
                    }
                    if(z->w_list.a[k].extra_end != INT16_MIN) {
                        o[1] = -z->w_list.a[k].extra_end;
                    }
                } else {///last window
                    q[1] = qe;
                }

                if((t[0] != -1) && (t[1] != -1)) {
                    mode = 0;//global
                } else if((t[0] != -1) && (t[1] == -1)) {
                    /**t[1] = z->y_pos_e+1;**/ mode = 1;///forward extension
                } else if((t[0] == -1) && (t[1] != -1)) {
                    /**t[0] = z->y_pos_s;**/ mode = 2;///backward extension
                } else {
                    mode = 3;//semi-global
                }

                for (m = l+1; m < k; m++) {
                    if(z->w_list.a[m].y_end != -1) {
                        c_e = z->w_list.a[m].error;
                    } else {
                        c_e = z->w_list.a[m].x_end + 1 - z->w_list.a[m].x_start;
                        if(c_e > THRESHOLD_MAX_SIZE) c_e = THRESHOLD_MAX_SIZE;
                    }
                    tot_e += c_e;
                }
                // if(q[1] < q[0]) {
                //     fprintf(stderr, "[M::%s::ql::%lu] qs::%lu, qe::%lu, ts::%lu, te::%lu, mode::%ld, tot_e::%lu\n", 
                //                                         __func__, q[1]-q[0], q[0], q[1], t[0], t[1], mode, tot_e);
                // }
                kocc = MAX(o[0], o[1]); if(kocc < 0) kocc = 1;
                hc_aln_exz(z, cl, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], tot_e, mode, wl, exz, ql, e_rate, MAX_CNS_L, MAX_CNS_E); 
                
            }
            l = k;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %rdx, %r15
movq %rdi, %r12
movq %rdx, %rax
xorl %edx, %edx
movq %r8, 0x10(%rsp)
divq %r8
subq %rdx, %r15
movl 0x8(%rdi), %edi
movl 0x4(%r12), %esi
cmpq %rsi, %r15
cmovbeq %rsi, %r15
cmpq %rdi, %r15
ja 0x619ac
movq %rcx, %rax
xorl %edx, %edx
movq 0x10(%rsp), %r8
divq %r8
leaq (%r8,%rcx), %rax
subq %rdx, %rax
testq %rdx, %rdx
cmoveq %rcx, %rax
incl %edi
cmpq %rdi, %rax
cmovaeq %rdi, %rax
movq %rax, (%rsp)
testq %rax, %rax
je 0x619ac
movq %rsi, %rax
xorl %edx, %edx
movq 0x10(%rsp), %rcx
idivq %rcx
subq %rdx, %rsi
movq %r15, %rax
subq %rsi, %rax
cqto
idivq %rcx
movq %rax, %rdi
notq %rsi
addq (%rsp), %rsi
movq %rsi, %rax
cqto
idivq %rcx
leaq 0x1(%rax), %r9
movq %rdi, 0x8(%rsp)
cmpq %r9, %rdi
jae 0x619ac
movq %rax, %rsi
movq 0x8(%rsp), %rax
leaq 0x1(%rax), %rcx
cmpq %r9, %rcx
jbe 0x619bb
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r13
movq %r13, %rax
movq %rsi, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movq %r13, %rdi
movq %rcx, %r13
cmpq %rsi, %rdi
je 0x619f4
movq 0x58(%r12), %rcx
movq %r13, %rdx
shlq $0x5, %rdx
cmpw $0x0, 0x12(%rcx,%rdx)
js 0x619f4
movq %rax, %rdx
jmp 0x61b96
movq %r13, %rcx
subq %rax, %rcx
cmpq $0x1, %rcx
ja 0x61a1b
movq 0x58(%r12), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
cmpw $0x0, 0x12(%rcx,%rdx)
movq %r13, %rdx
js 0x61b96
movq 0x58(%r12), %rcx
movq %rax, %rdx
shlq $0x5, %rdx
leaq (%rcx,%rdx), %r8
cmpw $0x0, 0x12(%rcx,%rdx)
js 0x61a41
cmpl $-0x1, 0xc(%r8)
je 0x61a61
movswq 0x14(%r8), %r10
jmp 0x61a78
movslq 0x4(%r8), %rsi
incq %rsi
movl 0xc(%r8), %r8d
incl %r8d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmovel %r10d, %r8d
movslq %r8d, %r11
xorl %r10d, %r10d
jmp 0x61a82
movl 0x4(%r8), %r10d
subl (%r8), %r10d
incl %r10d
cmpl $0x1f, %r10d
movl $0x1f, %esi
cmovael %esi, %r10d
movq $-0x1, %r11
movq %r15, %rsi
movq $-0x1, %r14
cmpq 0x8(%rsp), %r13
jbe 0x61ab5
cmpq %r9, %r13
jae 0x61ab5
movq %r13, %r8
shlq $0x5, %r8
cmpw $0x0, 0x12(%rcx,%r8)
movl $0x1f, %r9d
js 0x61bab
movq (%rsp), %rbx
jmp 0x61abf
movq (%rsp), %rbx
movl $0x1f, %r9d
cmpq $-0x1, %r11
je 0x61acf
cmpq $-0x1, %r14
je 0x61acf
xorl %ebp, %ebp
jmp 0x61afb
cmpq $-0x1, %r11
je 0x61ae0
movl $0x1, %ebp
cmpq $-0x1, %r14
je 0x61afb
cmpq $-0x1, %r14
setne %dil
cmpq $-0x1, %r11
sete %r8b
andb %dil, %r8b
movzbl %r8b, %ebp
xorq $0x3, %rbp
incq %rax
cmpq %r13, %rax
jae 0x61b34
addq %rdx, %rcx
addq $0x34, %rcx
cmpl $-0x1, -0x8(%rcx)
je 0x61b16
movswq (%rcx), %rdx
jmp 0x61b25
movl -0x10(%rcx), %edx
subl -0x14(%rcx), %edx
incl %edx
cmpl $0x1f, %edx
cmovael %r9d, %edx
addq %rdx, %r10
incq %rax
addq $0x20, %rcx
cmpq %r13, %rax
jb 0x61b0a
movq %r12, %rdi
movq 0x28(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r8
movq 0x80(%rsp), %r9
movsd 0x30(%rsp), %xmm0
pushq $0x3ff                  # imm = 0x3FF
pushq $0xc00                  # imm = 0xC00
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
pushq 0x30(%rsp)
pushq %rbp
pushq %r10
pushq %r14
pushq %r11
pushq %rbx
pushq %rsi
pushq 0xe0(%rsp)
callq 0x61505
addq $0x60, %rsp
movq %r13, %rdx
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r9
leaq 0x1(%r13), %rcx
movq %rdx, %rax
cmpq %r9, %rcx
jbe 0x619cd
jmp 0x619ac
addq %rcx, %r8
shlq $0x5, %rdi
movslq 0x4(%rcx,%rdi), %rbx
incq %rbx
cmpl $-0x1, 0xc(%r8)
je 0x61abf
addq %rcx, %rdi
movslq 0xc(%rdi), %r14
cmpq $-0x1, %r14
je 0x61bda
incq %r14
jmp 0x61abf
leaq 0x1c4abd(%rip), %rdi    # 0x22669e
leaq 0x1c3a5b(%rip), %rsi    # 0x225643
leaq 0x1c4acc(%rip), %rcx    # 0x2266bb
movl $0x395b, %edx           # imm = 0x395B
callq 0x2110