// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %r9, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x10(%rsp)
movq 0x110(%rsp), %r9
cmpq $0x3, %r9
jge 0x378c2
movq %rdx, %r12
movq %rdi, %r14
movq 0x8(%rsi), %r10
movq 0x48(%rdi), %rbx
movl $0x1, 0x1c(%rsp)
movl 0x4(%rdi), %ecx
cmpq %rdx, %rcx
movq %rdx, %r8
cmovgq %rcx, %r8
movq %r8, %rax
cqto
movq 0xc0(%rsp), %r11
idivq %r11
movq %rdx, %rdi
addq %rcx, %rdi
movq %rcx, %rax
xorl %edx, %edx
idivq %r11
subq %rdi, %rdx
addq %rdx, %r8
movq %r8, %rax
cqto
idivq %r11
leaq -0x1(%rbx), %rcx
cmpq %rbx, %rax
cmovgeq %rcx, %rax
movq %rax, %rdx
shlq $0x5, %rdx
orq $0x4, %rdx
cmpq %rbx, %rax
jge 0x3757e
movq 0x58(%r14), %rdi
movslq (%rdi,%rdx), %rdi
cmpq %r12, %rdi
jge 0x3757b
incq %rax
addq $0x20, %rdx
jmp 0x37560
movq %rax, %rcx
cmpq $-0x1, %rcx
pushq $-0x1
popq %rax
cmovlq %rcx, %rax
movq %rcx, %rdx
shlq $0x5, %rdx
testq %rcx, %rcx
js 0x375ae
movq 0x58(%r14), %rdi
movslq (%rdi,%rdx), %rdi
cmpq %r12, %rdi
jle 0x375ab
decq %rcx
addq $-0x20, %rdx
jmp 0x37590
movq %rcx, %rax
xorl %r13d, %r13d
testq %rax, %rax
cmovgq %rax, %r13
cmpq $0x2, %r9
movq %rsi, 0x8(%rsp)
movq %r10, 0x30(%rsp)
movsd %xmm0, 0x48(%rsp)
jae 0x375e8
leaq 0x1c(%rsp), %r9
movq %rsi, %rdi
movl %r12d, %esi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6293f
movq 0x10(%rsp), %r10
movq %r13, %rbp
shlq $0x5, %rbp
xorl %r15d, %r15d
cmpq %rbx, %r13
jge 0x37708
movq 0x58(%r14), %rdi
movl (%rdi,%rbp), %edx
movslq %edx, %rax
cmpq %r10, %rax
jge 0x37708
cmpl $-0x1, 0xc(%rdi,%rbp)
je 0x376fc
movslq 0x4(%rdi,%rbp), %rcx
incq %rcx
cmpq %r12, %rax
cmovleq %r12, %rax
cmpq %r10, %rcx
cmovgeq %r10, %rcx
cmpq %rax, %rcx
jle 0x376fc
cmpl $0x0, 0x1c(%rdi,%rbp)
jne 0x3769b
addq %rbp, %rdi
movl 0x10(%r14), %eax
movl 0x1c(%r14), %r10d
movq %r14, %rsi
movq 0xd8(%rsp), %rdx
movq 0xd0(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0xe0(%rsp), %r9
pushq %rax
pushq %r10
pushq 0x100(%rsp)
movq 0x100(%rsp), %rax
pushq (%rax)
callq 0x570cf
movq 0x30(%rsp), %r10
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, 0x70(%rsp)
movl 0x1c(%rdi,%rbp), %eax
movq %rax, 0x68(%rsp)
movq %rax, 0x60(%rsp)
movl 0x8(%rdi,%rbp), %esi
subq $0x8, %rsp
leaq 0x68(%rsp), %rdi
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
movl %r12d, %r9d
pushq 0x110(%rsp)
pushq 0xd0(%rsp)
pushq $0xa
pushq 0x28(%rsp)
pushq %r10
callq 0x36d69
movq 0x40(%rsp), %r10
addq $0x30, %rsp
movl %eax, %eax
addq %rax, %r15
incq %r13
addq $0x20, %rbp
jmp 0x375f7
movq 0x110(%rsp), %r14
testq $-0x3, %r14
movq 0x8(%rsp), %rbx
jne 0x3773c
leal -0x1(%r10), %esi
movq 0x20(%rsp), %rax
leal -0x1(%rax), %edx
leaq 0x1c(%rsp), %r9
movq %rbx, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x6293f
testq %r15, %r15
je 0x378a5
xorl %r13d, %r13d
testq $-0x3, %r14
sete %r13b
xorl %ebp, %ebp
cmpq $0x2, %r14
setb %bpl
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rbx
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %r14
movq %r14, 0x8(%rax)
subq %r14, %rbx
shlq $0x4, %r14
movq 0x8(%rsp), %rax
addq (%rax), %r14
leaq 0x80(%rsp), %rax
leaq 0x78(%rsp), %r15
leaq 0x40(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x50(%rsp), %r9
movq 0x120(%rsp), %rdi
movq 0x108(%rsp), %rsi
pushq %rax
pushq %r15
callq 0xea117
popq %rax
popq %rcx
leaq 0x58(%rsp), %rax
movq $0x1388, (%rax)         # imm = 0x1388
movq 0x8(%rsp), %rax
leaq 0x18(%rax), %rdx
leaq 0x40(%rsp), %rax
movq (%rax), %rcx
leaq 0x38(%rsp), %rax
movq (%rax), %r8
leaq 0x50(%rsp), %rax
movsd (%rax), %xmm0
movsd (%r15), %xmm1
subq $0x8, %rsp
movl $0x1388, %r9d           # imm = 0x1388
movq %r14, %rdi
movq %rbx, %rsi
movsd 0x50(%rsp), %xmm2
pushq %r13
pushq %rbp
movq 0x20(%rsp), %rbx
pushq $0x1
pushq 0x120(%rsp)
pushq 0x120(%rsp)
callq 0x16da5
movq 0x40(%rsp), %r9
addq $0x30, %rsp
xorl %ecx, %ecx
testq %rax, %rax
movq %rax, %rdx
cmovleq %rcx, %rdx
movq %r14, %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x37868
movq 0x40(%rbx), %r8
movq (%r8,%rdi,8), %r8
shlq $0x4, %r8
movups (%r14,%r8), %xmm0
movups %xmm0, (%rsi)
cmpb $0x1, 0xc(%rsi)
sbbq $-0x1, %rcx
incq %rdi
addq $0x10, %rsi
jmp 0x3783e
testq %rcx, %rcx
je 0x378ae
subq $0x10, %rsp
movq %rbx, %rdi
movq %rax, %rsi
movq 0x118(%rsp), %rdx
movq 0x120(%rsp), %rcx
movq %r12, %r8
pushq 0x48(%rsp)
pushq 0x58(%rsp)
pushq 0x40(%rsp)
pushq 0x50(%rsp)
callq 0x3704e
addq $0x30, %rsp
jmp 0x378b0
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rbx)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ad8fb(%rip), %rdi    # 0x1e51c4
leaq 0x1abdd3(%rip), %rsi    # 0x1e36a3
leaq 0x1ad8f6(%rip), %rcx    # 0x1e51cd
movl $0x4278, %edx           # imm = 0x4278
callq 0x2110