// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_win_chain(overlap_region*, Candidates_list*, long, long, long, long, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, long, long, double, long, long, long, long)
int64_t gen_win_chain(overlap_region *z, Candidates_list *cl, int64_t qs, int64_t qe, int64_t ts, int64_t te, 
int64_t wl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, UC_Read *tu, bit_extz_t *exz, 
int64_t ql, int64_t tl, double e_rate, int64_t h_khit, int64_t mode, int64_t rid, int64_t is_accurate)
{
    assert(mode < 3);
    int64_t k, ws, we, os, oe, wsk, rcn = cl->length, ncn, occ = 0, ovlp, wn = z->w_list.n; asg16_v ez; uint32_t w = 1;
    ws = qs; if(ws < z->x_pos_s) ws = z->x_pos_s; 
    we = qe-1; if(we > z->x_pos_e) we = z->x_pos_e; 
    wsk = get_win_id_by_s(z, ((ws/wl)*wl), wl, NULL); 
    // if(rid == 7) {
    //     fprintf(stderr, "[M::%s::]\tutg%.6u%c\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\tq::[%ld,%ld)\tw::[%ld,%ld]\twn::%ld\twsk::%ld\n", __func__, 
	// 			z->x_id+1, "lc"[uref->ug->u.a[z->x_id].circ], 
	// 			z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand],
	// 			z->y_id+1, "lc"[uref->ug->u.a[z->y_id].circ], 
	// 			z->y_pos_s, z->y_pos_e+1, qs, qe, ws, we, wn, wsk);
    // }
    // assert((ws>=z->w_list.a[wsk].x_start) && (ws<=z->w_list.a[wsk].x_end));
    // wek = get_win_id_by_e(z, ((we/wl)*wl), wl, NULL);
    // assert((we>=z->w_list.a[wek].x_start) && (we<=z->w_list.a[wek].x_end));
    for(wsk=((wsk<wn)?(wsk):(wn-1)); wsk<wn && qs>z->w_list.a[wsk].x_end; wsk++);
    for(wsk=((wsk<wn)?(wsk):(wn-1)); wsk>=0 && qs<z->w_list.a[wsk].x_start; wsk--);
    if(wsk < 0) wsk = 0; ///qs >= z->w_list.a[wsk].x_start && qs <= z->w_list.a[wsk].x_end  
    ///global or forward
    if(mode == 0 || mode == 1) push_khit(cl, qs, ts, 0, 0, &w);
    //[ws, we] && [wsk, wek]; [qs, qe) && [ts, te)
    for (k = wsk; k<wn && z->w_list.a[k].x_start<qe; k++) {
        if(z->w_list.a[k].y_end == -1) continue;
        ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end+1;
        os = MAX(qs, ws); oe = MIN(qe, we); ovlp = ((oe>os)? (oe-os):0);
        if(!ovlp) continue;
        if(!(z->w_list.a[k].clen)) {
            gen_backtrace_adv_exz(&(z->w_list.a[k]), z, rref, hpc_g, uref, qstr, tu->seq, exz, z->y_pos_strand, z->y_id);
        }
        ez.a = z->w_list.c.a + z->w_list.a[k].cidx; 
        ez.n = ez.m = z->w_list.a[k].clen; 
        occ += extract_exact_cigar(&ez, z->w_list.a[k].y_start, z->w_list.a[k].x_start, ts, te, qs, qe, cl, 10, wl, h_khit);
    }
    ///global or backward
    if(mode == 0 || mode == 2) push_khit(cl, qe-1, te-1, 0, 0, &w);
    // fprintf(stderr, "[M::%s::] rcn::%ld, cl->length::%lld\n", __func__, rcn, cl->length);
    if(!occ) {
        cl->length = rcn; return 0;
    }
    ncn = cl->length; cl->length = rcn;
    k_mer_hit *ch_a = cl->list + rcn; int64_t ch_n0 = ncn - rcn, ch_n;
    int64_t max_skip, max_iter, max_dis, quick_check; double chn_pen_gap, chn_pen_skip;
	set_lchain_dp_op(is_accurate, h_khit, &max_skip, &max_iter, &max_dis, &chn_pen_gap, &chn_pen_skip, &quick_check);
    max_dis = MAX_SIN_L>>1;
    // for (k = 0; k < ch_n0; k++) {
    //     assert(debug_k_mer_hit_retrive(&(ch_a[k]), hpc_g, rref, uref, qstr, tu, z->y_id, z->y_pos_strand));
    // }
    ch_n = lchain_qdp_fix(ch_a, ch_n0, &(cl->chainDP), max_skip, max_iter, max_dis, chn_pen_gap, chn_pen_skip, 
                e_rate, ql, tl, 1, ((mode==0)||(mode==1))?1:0,  ((mode==0)||(mode==2))?1:0);
    for (k = occ = 0; k < ch_n; k++) {
        ch_a[k] = ch_a[cl->chainDP.tmp[k]];
        if((ch_a[k].cnt&(0xffu))) occ++;
        // assert(debug_k_mer_hit_retrive(&(ch_a[k]), hpc_g, rref, uref, qstr, tu, z->y_id, z->y_pos_strand));
    }
    // fprintf(stderr, "[M::%s::] ch_n0::%ld, ch_n::%ld, mode::%ld, ql::%ld, tl::%ld, occ::%ld\n", 
    // __func__, ch_n0, ch_n, mode, qe-qs, te-ts, occ);
    if(occ <= 0) return 0;
    ch_n = gen_single_khit(cl, ch_n, h_khit, mode, qs, qe, ts, te, max_skip, max_iter, rid);
    return ch_n;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm0, 0x38(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x20(%rsp)
cmpq $0x3, 0x100(%rsp)
jge 0x68cc9
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0xb0(%rsp), %r8
movq 0x8(%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x48(%rdi), %rbx
movl 0x4(%rdi), %ecx
cmpq %rdx, %rcx
movq %rdx, %rsi
cmovgq %rcx, %rsi
movq %rsi, %rax
cqto
idivq %r8
movq %rdx, %rdi
movq %rcx, %rax
xorl %edx, %edx
idivq %r8
addq %rcx, %rdi
subq %rdi, %rdx
addq %rdx, %rsi
movq %rsi, %rax
cqto
idivq %r8
leaq -0x1(%rbx), %rdx
cmpq %rbx, %rax
cmovgeq %rdx, %rax
movq 0x58(%r14), %rcx
movq %rax, %rsi
shlq $0x5, %rsi
addq %rcx, %rsi
addq $0x4, %rsi
movslq (%rsi), %rdi
cmpq %r12, %rdi
jge 0x68912
incq %rax
addq $0x20, %rsi
cmpq %rbx, %rax
jl 0x688fb
movq %rdx, %rax
testq %rax, %rax
js 0x68940
movq %rax, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rdx
cmpq %r12, %rdx
jle 0x68940
leaq -0x1(%rax), %rdx
addq $-0x20, %rcx
testq %rax, %rax
movq %rdx, %rax
jg 0x68921
movq $-0x1, %rax
xorl %ebp, %ebp
testq %rax, %rax
cmovgq %rax, %rbp
cmpq $0x1, 0x100(%rsp)
ja 0x689b9
movq (%r13), %rax
movq 0x10(%rsp), %rdx
cmpq 0x10(%r13), %rdx
jne 0x6898d
leaq (%rdx,%rdx), %rcx
testq %rdx, %rdx
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rcx
jmp 0x68990
movq %rdx, %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
shlq $0x4, %rcx
movl $0x0, (%rax,%rcx)
movl %r12d, 0x8(%rax,%rcx)
movq 0x20(%rsp), %rdx
movl %edx, 0x4(%rax,%rcx)
movl $0x100, 0xc(%rax,%rcx)  # imm = 0x100
movb $0x1, %sil
subq %rbp, %rbx
jle 0x68adc
movq %r13, 0x8(%rsp)
shlq $0x5, %rbp
xorl %r13d, %r13d
movq 0x58(%r14), %rdi
movl (%rdi,%rbp), %edx
movslq %edx, %rax
cmpq %r15, %rax
jge 0x68ad0
cmpl $-0x1, 0xc(%rdi,%rbp)
je 0x68ac3
movslq 0x4(%rdi,%rbp), %rcx
incq %rcx
cmpq %r12, %rax
cmovleq %r12, %rax
cmpq %r15, %rcx
cmovgeq %r15, %rcx
cmpq %rax, %rcx
jle 0x68ac3
cmpl $0x0, 0x1c(%rdi,%rbp)
jne 0x68a67
addq %rbp, %rdi
movl 0x10(%r14), %eax
movl 0x1c(%r14), %r10d
movq %r14, %rsi
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
movq 0xb8(%rsp), %r8
movq 0xd0(%rsp), %r9
pushq %rax
pushq %r10
pushq 0xf0(%rsp)
movq 0xf0(%rsp), %rax
pushq (%rax)
callq 0x8a9ca
addq $0x20, %rsp
movq 0x58(%r14), %rdi
movl (%rdi,%rbp), %edx
movl 0x18(%rdi,%rbp), %eax
addq %rax, %rax
addq 0x70(%r14), %rax
movq %rax, 0x68(%rsp)
movl 0x1c(%rdi,%rbp), %eax
movq %rax, 0x60(%rsp)
movq %rax, 0x58(%rsp)
movl 0x8(%rdi,%rbp), %esi
subq $0x8, %rsp
leaq 0x60(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r8
movl %r12d, %r9d
pushq 0x100(%rsp)
pushq 0xc0(%rsp)
pushq $0xa
pushq 0x28(%rsp)
pushq %r15
callq 0x67fd2
addq $0x30, %rsp
movl %eax, %eax
addq %rax, %r13
addq $0x20, %rbp
decq %rbx
jne 0x689d1
testq %r13, %r13
sete %sil
movq 0x8(%rsp), %r13
testq $-0x3, 0x100(%rsp)
jne 0x68b55
leal -0x1(%r15), %ebx
movq 0x18(%rsp), %rax
leal -0x1(%rax), %ebp
movq (%r13), %rax
movq 0x8(%r13), %rcx
cmpq 0x10(%r13), %rcx
jne 0x68b32
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl %esi, %r14d
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x10(%r13)
shlq $0x4, %rsi
movq %rax, %rdi
callq 0x2370
movl %r14d, %esi
movq %rax, (%r13)
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
shlq $0x4, %rcx
movl $0x0, (%rax,%rcx)
movl %ebx, 0x8(%rax,%rcx)
movl %ebp, 0x4(%rax,%rcx)
movl $0x100, 0xc(%rax,%rcx)  # imm = 0x100
testb %sil, %sil
je 0x68b68
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
jmp 0x68cb8
xorl %ecx, %ecx
movq 0x100(%rsp), %rax
testq $-0x3, %rax
sete %cl
movq %rcx, 0x8(%rsp)
xorl %r14d, %r14d
cmpq $0x2, %rax
setb %r14b
movq 0x8(%r13), %rbx
movq 0x10(%rsp), %rbp
movq %rbp, 0x8(%r13)
subq %rbp, %rbx
shlq $0x4, %rbp
addq (%r13), %rbp
leaq 0x70(%rsp), %rax
leaq 0x40(%rsp), %r10
leaq 0x30(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x50(%rsp), %r8
leaq 0x48(%rsp), %r9
movq 0x110(%rsp), %rdi
movq 0xf8(%rsp), %rsi
pushq %rax
pushq %r10
callq 0x12fb4d
addq $0x10, %rsp
leaq 0x50(%rsp), %rax
movq $0x1388, (%rax)         # imm = 0x1388
leaq 0x18(%r13), %rdx
leaq 0x30(%rsp), %rax
movq (%rax), %rcx
leaq 0x28(%rsp), %rax
movq (%rax), %r8
leaq 0x48(%rsp), %rax
movsd (%rax), %xmm0
leaq 0x40(%rsp), %rax
movsd (%rax), %xmm1
subq $0x8, %rsp
movl $0x1388, %r9d           # imm = 0x1388
movq %rbp, %rdi
movq %rbx, %rsi
movsd 0x40(%rsp), %xmm2
pushq 0x10(%rsp)
pushq %r14
pushq $0x1
pushq 0x110(%rsp)
pushq 0x110(%rsp)
callq 0x43030
addq $0x30, %rsp
testq %rax, %rax
jle 0x68cb8
xorl %ecx, %ecx
movq %rbp, %rdx
xorl %esi, %esi
movq 0x40(%r13), %rdi
movq (%rdi,%rcx,8), %rdi
shlq $0x4, %rdi
movups (%rbp,%rdi), %xmm0
movups %xmm0, (%rdx)
cmpb $0x1, 0xc(%rdx)
sbbq $-0x1, %rsi
incq %rcx
addq $0x10, %rdx
cmpq %rcx, %rax
jne 0x68c50
testq %rsi, %rsi
je 0x68cb8
subq $0x10, %rsp
movq %r13, %rdi
movq %rax, %rsi
movq 0x108(%rsp), %rdx
movq 0x110(%rsp), %rcx
movq %r12, %r8
movq %r15, %r9
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x683b6
addq $0x30, %rsp
jmp 0x68cba
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ca5a4(%rip), %rdi    # 0x233274
leaq 0x1c8a7c(%rip), %rsi    # 0x231753
leaq 0x1ca59f(%rip), %rcx    # 0x23327d
movl $0x4278, %edx           # imm = 0x4278
callq 0x2110