// File: /workspace/github_cpp_makefile_star2_O1/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 0x68fe9
movq %rcx, %r10
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), %rcx
cmpq %rbx, %rax
cmovgeq %rcx, %rax
movq 0x58(%r14), %rdx
movq %rax, %rsi
shlq $0x5, %rsi
addq %rsi, %rdx
addq $0x4, %rdx
movslq (%rdx), %rsi
cmpq %r12, %rsi
jge 0x68c37
incq %rax
addq $0x20, %rdx
cmpq %rbx, %rax
jl 0x68c20
movq %rcx, %rax
testq %rax, %rax
js 0x68c66
movq %rax, %rcx
shlq $0x5, %rcx
addq 0x58(%r14), %rcx
movslq (%rcx), %rdx
cmpq %r12, %rdx
jle 0x68c66
leaq -0x1(%rax), %rdx
addq $-0x20, %rcx
testq %rax, %rax
movq %rdx, %rax
jg 0x68c47
movq $-0x1, %rax
xorl %ebp, %ebp
testq %rax, %rax
cmovgq %rax, %rbp
cmpq $0x1, 0x100(%rsp)
movq %r10, 0x8(%rsp)
ja 0x68ce5
movq 0x10(%r13), %rax
cmpq %rax, 0x10(%rsp)
jne 0x68cb4
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%r13)
movq (%r13), %rdi
shlq $0x4, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq %rax, (%r13)
movq (%r13), %rax
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%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, %r15b
subq %rbp, %rbx
jle 0x68e04
shlq $0x5, %rbp
xorl %r15d, %r15d
movq 0x58(%r14), %rdi
movslq (%rdi,%rbp), %rax
cmpq %r10, %rax
jge 0x68dfd
cmpl $-0x1, 0xc(%rdi,%rbp)
je 0x68df0
movslq 0x4(%rdi,%rbp), %rcx
incq %rcx
cmpq %r12, %rax
cmovleq %r12, %rax
cmpq %r10, %rcx
cmovgeq %r10, %rcx
cmpq %rax, %rcx
jle 0x68df0
cmpl $0x0, 0x1c(%rdi,%rbp)
jne 0x68d8a
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 0x8b5b0
movq 0x28(%rsp), %r10
addq $0x20, %rsp
movq 0x58(%r14), %rax
movl 0x18(%rax,%rbp), %ecx
addq %rcx, %rcx
addq 0x70(%r14), %rcx
movq %rcx, 0x68(%rsp)
movl 0x1c(%rax,%rbp), %ecx
movq %rcx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movl (%rax,%rbp), %edx
movl 0x8(%rax,%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 %r13
pushq %r10
callq 0x68341
movq 0x38(%rsp), %r10
addq $0x30, %rsp
movl %eax, %eax
addq %rax, %r15
addq $0x20, %rbp
decq %rbx
jne 0x68cf8
testq %r15, %r15
sete %r15b
testq $-0x3, 0x100(%rsp)
jne 0x68e78
leal -0x1(%r10), %ebx
movq 0x18(%rsp), %rax
leal -0x1(%rax), %ebp
movq 0x10(%r13), %rax
cmpq %rax, 0x8(%r13)
jne 0x68e4d
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x10(%r13)
movq (%r13), %rdi
shlq $0x4, %rsi
callq 0x2370
movq %rax, (%r13)
movq (%r13), %rax
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
shlq $0x4, %rcx
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%rcx)
movl %ebx, 0x8(%rax,%rcx)
movl %ebp, 0x4(%rax,%rcx)
movl $0x100, 0xc(%rax,%rcx)  # imm = 0x100
testb %r15b, %r15b
je 0x68e8b
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r13)
jmp 0x68fd8
xorl %r15d, %r15d
movq 0x100(%rsp), %rax
testq $-0x3, %rax
sete %r15b
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 0x129a8a
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 %r15
pushq %r14
pushq $0x1
pushq 0x110(%rsp)
pushq 0x110(%rsp)
callq 0x438ae
addq $0x30, %rsp
testq %rax, %rax
jle 0x68fd8
xorl %ecx, %ecx
movq %rbp, %rdx
xorl %esi, %esi
movq 0x8(%rsp), %r9
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 0x68f73
testq %rsi, %rsi
je 0x68fd8
subq $0x10, %rsp
movq %r13, %rdi
movq %rax, %rsi
movq 0x108(%rsp), %rdx
movq 0x110(%rsp), %rcx
movq %r12, %r8
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x6870b
addq $0x30, %rsp
jmp 0x68fda
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1be174(%rip), %rdi    # 0x227164
leaq 0x1bc64c(%rip), %rsi    # 0x225643
leaq 0x1be16f(%rip), %rcx    # 0x22716d
movl $0x4278, %edx           # imm = 0x4278
callq 0x2110