// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq -0x40(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x50(%rbp)
imulq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jae 0x596cd
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x596e2
jmp 0x59ddd
movq -0x48(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x50(%rbp)
imulq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x5970d
movq -0x50(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x8(%rcx), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x59732
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
ja 0x5973e
jmp 0x59ddd
movq -0x30(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x78(%rbp)
movq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rsi
subq $0x1, %rsi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e250
addq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x80(%rbp), %rax
jb 0x59787
jmp 0x59ddd
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
ja 0x59ddd
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
je 0x597e2
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x59dc4
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
cmpq $0x1, %rax
ja 0x59819
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jl 0x59db6
movq $-0x1, -0xc8(%rbp)
movq $-0x1, -0xd0(%rbp)
movq $-0x1, -0xb8(%rbp)
movq $-0x1, -0xc0(%rbp)
movq $-0x1, -0xd8(%rbp)
movq $-0x1, -0xe0(%rbp)
movq $-0x1, -0xe8(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x59947
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x598fa
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xd0(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0xffff8000, %eax       # imm = 0xFFFF8000
je 0x59942
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %ecx
xorl %eax, %eax
subl %ecx, %eax
cltq
movq %rax, -0xe0(%rbp)
jmp 0x599fb
movq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x59992
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswq 0x14(%rax), %rax
movq %rax, -0xa8(%rbp)
jmp 0x599e6
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x90(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
movq %rax, -0xa8(%rbp)
cmpq $0x1f, -0xa8(%rbp)
jbe 0x599e4
movq $0x1f, -0xa8(%rbp)
jmp 0x599e6
movq -0xa8(%rbp), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
cmpq -0x78(%rbp), %rax
jbe 0x59b39
movq -0x88(%rbp), %rax
cmpq -0x80(%rbp), %rax
jae 0x59b39
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0x0, %eax
jge 0x59b39
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x59aef
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x59aa7
jmp 0x59ac6
leaq 0x3298c8(%rip), %rdi    # 0x383376
leaq 0x328866(%rip), %rsi    # 0x38231b
movl $0x395b, %edx           # imm = 0x395B
leaq 0x3298d2(%rip), %rcx    # 0x383393
callq 0x2130
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
subq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xc8(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %eax
cmpl $0xffff8000, %eax       # imm = 0xFFFF8000
je 0x59b37
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x88(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswl 0x12(%rax), %ecx
xorl %eax, %eax
subl %ecx, %eax
cltq
movq %rax, -0xd8(%rbp)
jmp 0x59b44
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $-0x1, -0xd0(%rbp)
je 0x59b65
cmpq $-0x1, -0xc8(%rbp)
je 0x59b65
movq $0x0, -0xe8(%rbp)
jmp 0x59bb6
cmpq $-0x1, -0xd0(%rbp)
je 0x59b86
cmpq $-0x1, -0xc8(%rbp)
jne 0x59b86
movq $0x1, -0xe8(%rbp)
jmp 0x59bb4
cmpq $-0x1, -0xd0(%rbp)
jne 0x59ba7
cmpq $-0x1, -0xc8(%rbp)
je 0x59ba7
movq $0x2, -0xe8(%rbp)
jmp 0x59bb2
movq $0x3, -0xe8(%rbp)
jmp 0x59bb4
jmp 0x59bb6
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x59c9c
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
je 0x59c1c
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswq 0x14(%rax), %rax
movq %rax, -0xa8(%rbp)
jmp 0x59c70
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x98(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x98(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
movq %rax, -0xa8(%rbp)
cmpq $0x1f, -0xa8(%rbp)
jbe 0x59c6e
movq $0x1f, -0xa8(%rbp)
jmp 0x59c70
movq -0xa8(%rbp), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x59bc8
movq -0xe0(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jl 0x59cbc
movq -0xe0(%rbp), %rax
movq %rax, -0xf8(%rbp)
jmp 0x59cca
movq -0xd8(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xf0(%rbp)
cmpq $0x0, -0xf0(%rbp)
jge 0x59ced
movq $0x1, -0xf0(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movq 0x28(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0xb8(%rbp), %r13
movq -0xd0(%rbp), %r12
movq -0xc8(%rbp), %r15
movq -0xa0(%rbp), %r14
movq -0xe8(%rbp), %rbx
movq -0x50(%rbp), %r11
movq 0x30(%rbp), %r10
movq 0x38(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x110(%rbp), %rax
movsd -0x60(%rbp), %xmm0
movq %rax, (%rsp)
movq -0x108(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x100(%rbp), %rax
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r11, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq $0xc00, 0x50(%rsp)      # imm = 0xC00
movq $0x3ff, 0x58(%rsp)      # imm = 0x3FF
callq 0x59190
movq -0x88(%rbp), %rax
movq %rax, -0x90(%rbp)
jmp 0x59dc6
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x597a1
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop