// File: /workspace/github_cpp_makefile_star2_O2/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
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 0x31152
movq %rcx, %rax
xorl %edx, %edx
movq 0x10(%rsp), %r8
divq %r8
leaq (%r8,%rcx), %r13
subq %rdx, %r13
testq %rdx, %rdx
cmoveq %rcx, %r13
incl %edi
cmpq %rdi, %r13
cmovaeq %rdi, %r13
testq %r13, %r13
je 0x31152
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 %r13, %rsi
movq %rsi, %rax
cqto
idivq %rcx
leaq 0x1(%rax), %rbx
movq %rdi, 0x20(%rsp)
cmpq %rbx, %rdi
jae 0x31152
movq %rax, %rbp
negq %rbp
movq 0x20(%rsp), %r14
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %r14, %rcx
shlq $0x5, %rcx
leaq 0x32(%rcx), %r8
movq %r14, %rax
incq %rax
pushq $0x1
popq %r11
movq %rax, %r10
cmpq %rbx, %r10
ja 0x31152
movq %r8, %rdi
movq %r10, %r14
movq %r11, %rsi
leaq (%r10,%rbp), %r8
movq 0x58(%r12), %rdx
cmpq $0x1, %r8
je 0x30f8f
leaq 0x20(%rdi), %r8
leaq 0x1(%r14), %r10
leaq 0x1(%rsi), %r11
cmpw $0x0, (%rdx,%rdi)
jns 0x30f5b
movzwl 0x12(%rdx,%rcx), %r8d
cmpq $0x1, %rsi
jbe 0x30fc9
leaq (%rdx,%rcx), %rsi
testw %r8w, %r8w
jns 0x30fd7
movslq 0x4(%rsi), %r8
incq %r8
movq %r8, (%rsp)
movl 0xc(%rsi), %esi
incl %esi
pushq $-0x1
popq %r8
cmovel %r8d, %esi
movslq %esi, %rsi
xorl %r10d, %r10d
pushq $-0x1
popq %r8
jmp 0x31007
testw %r8w, %r8w
js 0x30f43
leaq (%rdx,%rcx), %rsi
cmpl $-0x1, 0xc(%rdx,%rcx)
pushq $-0x1
popq %r8
je 0x30fea
movswq 0x14(%rdx,%rcx), %r10
jmp 0x31000
movl 0x4(%rdx,%rcx), %r10d
subl (%rsi), %r10d
incl %r10d
cmpl $0x1f, %r10d
pushq $0x1f
popq %rsi
cmovael %esi, %r10d
movq %r15, (%rsp)
movq %r8, %rsi
movq %r13, %r11
cmpq 0x20(%rsp), %r14
movq %rsi, 0x8(%rsp)
jbe 0x3102c
movq %r13, %r11
cmpq %rbx, %r14
jae 0x3102c
cmpw $0x0, (%rdx,%rdi)
movq %r13, %r11
js 0x31103
movq %r11, 0x18(%rsp)
cmpq $-0x1, %rsi
setne %dil
xorl %esi, %esi
movq %r8, %r11
testb %dil, %dil
je 0x31051
cmpq $-0x1, %r11
jne 0x31051
movq %r8, %r11
pushq $0x1
popq %rsi
jmp 0x31066
cmpq $-0x1, 0x8(%rsp)
sete %dil
andb %sil, %dil
movzbl %dil, %esi
xorq $0x3, %rsi
pushq $0x1f
popq %rdi
addq %rdx, %rcx
addq $0x34, %rcx
cmpq %r14, %rax
jae 0x3109b
cmpl $-0x1, -0x8(%rcx)
je 0x31081
movswq (%rcx), %rdx
jmp 0x3108f
movl -0x10(%rcx), %edx
subl -0x14(%rcx), %edx
incl %edx
cmpl $0x1f, %edx
cmovael %edi, %edx
addq %rdx, %r10
incq %rax
addq $0x20, %rcx
jmp 0x31070
movq %r12, %rdi
movq %r9, %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 %rsi
pushq %r10
pushq %r11
pushq 0x48(%rsp)
pushq 0x60(%rsp)
pushq 0x50(%rsp)
pushq 0xe0(%rsp)
callq 0x30a5f
movq 0x88(%rsp), %r9
addq $0x60, %rsp
jmp 0x30f43
movslq -0x2e(%rdx,%rdi), %r11
incq %r11
cmpl $-0x1, -0x6(%rdx,%rdi)
je 0x3102c
movq %r11, 0x18(%rsp)
movslq -0x26(%rdx,%rdi), %r11
cmpq $-0x1, %r11
je 0x31161
incq %r11
cmpl $-0x1, %r11d
setne %sil
cmpq $-0x1, 0x8(%rsp)
setne %dil
je 0x3103e
cmpl $-0x1, %r11d
je 0x3103e
xorl %esi, %esi
jmp 0x31066
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1b3596(%rip), %rdi    # 0x1e46fe
leaq 0x1b2534(%rip), %rsi    # 0x1e36a3
leaq 0x1b35a5(%rip), %rcx    # 0x1e471b
movl $0x395b, %edx           # imm = 0x395B
callq 0x2110