// File: /workspace/github_cpp_makefile_star2_O3/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 %rdx, %rax
xorl %edx, %edx
movq %r8, 0x8(%rsp)
divq %r8
subq %rdx, %r15
movl 0x8(%rdi), %r9d
movq %rdi, 0x10(%rsp)
movl 0x4(%rdi), %esi
cmpq %rsi, %r15
cmovbeq %rsi, %r15
cmpq %r9, %r15
ja 0x613d3
movq %rcx, %rax
xorl %edx, %edx
movq 0x8(%rsp), %r8
divq %r8
leaq (%r8,%rcx), %r14
subq %rdx, %r14
testq %rdx, %rdx
cmoveq %rcx, %r14
incl %r9d
cmpq %r9, %r14
cmovaeq %r9, %r14
testq %r14, %r14
je 0x613d3
movq %rsi, %rax
xorl %edx, %edx
movq 0x8(%rsp), %rcx
idivq %rcx
subq %rdx, %rsi
movq %r15, %rax
subq %rsi, %rax
cqto
idivq %rcx
movq %rax, %rdi
notq %rsi
addq %r14, %rsi
movq %rsi, %rax
cqto
idivq %rcx
leaq 0x1(%rax), %rbp
movq %rdi, (%rsp)
cmpq %rbp, %rdi
jae 0x613d3
movq %rax, %r9
movq (%rsp), %rax
leaq 0x1(%rax), %rcx
cmpq %rbp, %rcx
jbe 0x613e2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %r13
movq %r13, %rax
movq %r9, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %r13, %rdi
movq %rcx, %r13
movq 0x10(%rsp), %rcx
movq 0x58(%rcx), %rcx
cmpq %r9, %rdi
je 0x6141e
movq %r13, %rdx
shlq $0x5, %rdx
cmpw $0x0, 0x12(%rcx,%rdx)
js 0x6141e
movq %rax, %rsi
jmp 0x615a9
movq %r13, %rsi
subq %rax, %rsi
movq %rax, %rdx
shlq $0x5, %rdx
movzwl 0x12(%rcx,%rdx), %r8d
cmpq $0x1, %rsi
jbe 0x61465
leaq (%rcx,%rdx), %rsi
testw %r8w, %r8w
jns 0x61476
movslq 0x4(%rsi), %r12
incq %r12
movl 0xc(%rsi), %esi
incl %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmovel %r8d, %esi
movslq %esi, %r11
xorl %r10d, %r10d
movl $0x1f, %r8d
jmp 0x614a8
movq %r13, %rsi
testw %r8w, %r8w
js 0x615a9
leaq (%rcx,%rdx), %rsi
movl $0x1f, %r8d
cmpl $-0x1, 0xc(%rcx,%rdx)
je 0x6148b
movswq 0x14(%rcx,%rdx), %r10
jmp 0x6149e
movl 0x4(%rcx,%rdx), %r10d
subl (%rsi), %r10d
incl %r10d
cmpl $0x1f, %r10d
cmovael %r8d, %r10d
movq $-0x1, %r11
movq %r15, %r12
movq %r14, %rbx
cmpq (%rsp), %r13
jbe 0x614cf
movq %r14, %rbx
cmpq %rbp, %r13
jae 0x614cf
movq %r13, %rsi
shlq $0x5, %rsi
cmpw $0x0, 0x12(%rcx,%rsi)
movq %r14, %rbx
js 0x615be
cmpq $-0x1, %r11
setne %dil
movq $-0x1, %rbp
xorl %esi, %esi
testb %dil, %dil
je 0x614f9
cmpq $-0x1, %rbp
jne 0x614f9
movl $0x1, %esi
movq $-0x1, %rbp
jmp 0x6150c
cmpq $-0x1, %r11
sete %dil
andb %sil, %dil
movzbl %dil, %esi
xorq $0x3, %rsi
incq %rax
cmpq %r13, %rax
jae 0x61545
addq %rdx, %rcx
addq $0x34, %rcx
cmpl $-0x1, -0x8(%rcx)
je 0x61527
movswq (%rcx), %rdx
jmp 0x61536
movl -0x10(%rcx), %edx
subl -0x14(%rcx), %edx
incl %edx
cmpl $0x1f, %edx
cmovael %r8d, %edx
addq %rdx, %r10
incq %rax
addq $0x20, %rcx
cmpq %r13, %rax
jb 0x6151b
movq 0x10(%rsp), %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 0x28(%rsp)
pushq %rsi
pushq %r10
pushq %rbp
pushq %r11
pushq %rbx
pushq %r12
pushq 0xe0(%rsp)
callq 0x60eef
addq $0x60, %rsp
movq %r13, %rsi
movq 0x20(%rsp), %r9
movq 0x18(%rsp), %rbp
leaq 0x1(%r13), %rcx
movq %rsi, %rax
cmpq %rbp, %rcx
jbe 0x613f3
jmp 0x613d3
addq %rcx, %rsi
shlq $0x5, %rdi
movslq 0x4(%rcx,%rdi), %rbx
incq %rbx
cmpl $-0x1, 0xc(%rsi)
je 0x614cf
addq %rcx, %rdi
movslq 0xc(%rdi), %rbp
cmpq $-0x1, %rbp
je 0x6160c
incq %rbp
cmpl $-0x1, %ebp
setne %sil
cmpq $-0x1, %r11
setne %dil
je 0x614e0
cmpl $-0x1, %ebp
je 0x614e0
xorl %esi, %esi
jmp 0x6150c
leaq 0x1d119b(%rip), %rdi    # 0x2327ae
leaq 0x1d0139(%rip), %rsi    # 0x231753
leaq 0x1d11aa(%rip), %rcx    # 0x2327cb
movl $0x395b, %edx           # imm = 0x395B
callq 0x2110