// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq 0x70(%rbp), %rax
movq 0x68(%rbp), %rax
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
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, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
movsd %xmm0, -0x58(%rbp)
cmpq $0x3, 0x60(%rbp)
jge 0x655ca
jmp 0x655e9
leaq 0x31e86b(%rip), %rdi    # 0x383e3c
leaq 0x31cd43(%rip), %rsi    # 0x38231b
movl $0x4278, %edx           # imm = 0x4278
leaq 0x31e861(%rip), %rcx    # 0x383e45
callq 0x2130
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xb0(%rbp)
movl $0x1, -0xcc(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jge 0x6563f
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jle 0x65666
movq -0x28(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x70(%rbp)
movq -0x28(%rbp), %rdi
movq -0x68(%rbp), %rax
cqto
idivq 0x10(%rbp)
movq %rax, %rsi
imulq 0x10(%rbp), %rsi
movq 0x10(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x9e2e0
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jge 0x656b0
movq -0x88(%rbp), %rax
movq %rax, -0x120(%rbp)
jmp 0x656c2
movq -0xb0(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rcx
xorl %eax, %eax
cmpq -0xb0(%rbp), %rcx
movb %al, -0x121(%rbp)
jge 0x65712
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x88(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
setg %al
movb %al, -0x121(%rbp)
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x6571e
jmp 0x65734
jmp 0x65720
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x656d0
movq -0x88(%rbp), %rax
cmpq -0xb0(%rbp), %rax
jge 0x65754
movq -0x88(%rbp), %rax
movq %rax, -0x130(%rbp)
jmp 0x65766
movq -0xb0(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x88(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x88(%rbp)
movb %al, -0x131(%rbp)
jl 0x657af
movq -0x38(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x88(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
setl %al
movb %al, -0x131(%rbp)
movb -0x131(%rbp), %al
testb $0x1, %al
jne 0x657bb
jmp 0x657d1
jmp 0x657bd
movq -0x88(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x65774
cmpq $0x0, -0x88(%rbp)
jge 0x657e6
movq $0x0, -0x88(%rbp)
cmpq $0x0, 0x60(%rbp)
je 0x657f4
cmpq $0x1, 0x60(%rbp)
jne 0x65816
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %esi
movq -0x48(%rbp), %rax
movl %eax, %edx
xorl %r8d, %r8d
leaq -0xcc(%rbp), %r9
movl %r8d, %ecx
callq 0xc11f0
movq -0x88(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
xorl %eax, %eax
cmpq -0xb0(%rbp), %rcx
movb %al, -0x132(%rbp)
jge 0x65859
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
cmpq -0x40(%rbp), %rax
setl %al
movb %al, -0x132(%rbp)
movb -0x132(%rbp), %al
testb $0x1, %al
jne 0x65868
jmp 0x65ab4
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $-0x1, 0xc(%rax)
jne 0x65886
jmp 0x65aa3
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x68(%rbp), %rax
jl 0x658d6
movq -0x38(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0x658e1
movq -0x68(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x65903
movq -0x40(%rbp), %rax
movq %rax, -0x148(%rbp)
jmp 0x6590e
movq -0x70(%rbp), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x65934
movq -0x80(%rbp), %rax
subq -0x78(%rbp), %rax
movq %rax, -0x150(%rbp)
jmp 0x6593f
xorl %eax, %eax
movq %rax, -0x150(%rbp)
jmp 0x6593f
movq -0x150(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jne 0x6595c
jmp 0x65aa3
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x659cd
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x60(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movq 0x28(%rbp), %rdx
movq 0x20(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x30(%rbp), %r9
movq 0x38(%rbp), %rax
movq (%rax), %rbx
movq 0x40(%rbp), %r11
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %r10d
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl %eax, 0x18(%rsp)
callq 0xa79c0
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x60(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x18(%rcx), %ecx
shlq %rcx
addq %rcx, %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movq %rax, -0xc0(%rbp)
movq %rax, -0xc8(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %edx
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
movl %eax, %r9d
movq -0x40(%rbp), %rax
movl %eax, %ebx
movq -0x30(%rbp), %r11
movq 0x10(%rbp), %r10
movq 0x58(%rbp), %rax
leaq -0xc8(%rbp), %rdi
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movl $0xa, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x64790
movl %eax, %eax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x65821
cmpq $0x0, 0x60(%rbp)
je 0x65ac2
cmpq $0x2, 0x60(%rbp)
jne 0x65aec
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rax
subq $0x1, %rax
movl %eax, %esi
movq -0x50(%rbp), %rax
subq $0x1, %rax
movl %eax, %edx
xorl %r8d, %r8d
leaq -0xcc(%rbp), %r9
movl %r8d, %ecx
callq 0xc11f0
cmpq $0x0, -0xa0(%rbp)
jne 0x65b12
movq -0x90(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq $0x0, -0x20(%rbp)
jmp 0x65e54
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x90(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xd8(%rbp)
movq -0x98(%rbp), %rax
subq -0x90(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq 0x70(%rbp), %rax
movl %eax, %edi
movq 0x58(%rbp), %rax
movl %eax, %esi
leaq -0xf0(%rbp), %rdx
leaq -0xf8(%rbp), %rcx
leaq -0x100(%rbp), %r8
leaq -0x110(%rbp), %r9
leaq -0x118(%rbp), %r10
leaq -0x108(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1aca80
movq $0x1388, -0x100(%rbp) # imm = 0x1388
movq -0xd8(%rbp), %rax
movq %rax, -0x1b0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movq -0x30(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x1a0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0x188(%rbp)
movsd -0x110(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
movsd -0x118(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x170(%rbp)
movq 0x48(%rbp), %rax
movq %rax, -0x168(%rbp)
movq 0x50(%rbp), %rax
movq %rax, -0x160(%rbp)
movb $0x1, %al
cmpq $0x0, 0x60(%rbp)
movb %al, -0x151(%rbp)
je 0x65c65
cmpq $0x1, 0x60(%rbp)
sete %al
movb %al, -0x151(%rbp)
movb -0x151(%rbp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
cltq
movq %rax, -0x1c0(%rbp)
movb $0x1, %al
cmpq $0x0, 0x60(%rbp)
movb %al, -0x1b1(%rbp)
je 0x65c9e
cmpq $0x2, 0x60(%rbp)
sete %al
movb %al, -0x1b1(%rbp)
movq -0x1c0(%rbp), %r10
movq -0x160(%rbp), %r11
movq -0x168(%rbp), %rbx
movsd -0x170(%rbp), %xmm2
movsd -0x178(%rbp), %xmm1
movsd -0x180(%rbp), %xmm0
movq -0x188(%rbp), %r9
movq -0x190(%rbp), %r8
movq -0x198(%rbp), %rcx
movq -0x1a0(%rbp), %rdx
movq -0x1a8(%rbp), %rsi
movq -0x1b0(%rbp), %rdi
movb -0x1b1(%rbp), %r15b
xorl %eax, %eax
movl $0x1, %r14d
testb $0x1, %r15b
cmovnel %r14d, %eax
cltq
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq $0x1, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x28e90
movq %rax, -0xe8(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0xe8(%rbp), %rax
jge 0x65ddc
movq -0xd8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x60(%rbp), %rdx
movq (%rax,%rdx,8), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq -0xd8(%rbp), %rax
movq -0x60(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movq -0xd8(%rbp), %rax
movq -0x60(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
cmpl $0x0, %eax
je 0x65dc9
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x65dcb
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x65d49
cmpq $0x0, -0xa0(%rbp)
jg 0x65df0
movq $0x0, -0x20(%rbp)
jmp 0x65e54
movq -0x30(%rbp), %rdi
movq -0xe8(%rbp), %rsi
movq 0x58(%rbp), %rdx
movq 0x60(%rbp), %rcx
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %r9
movq -0x48(%rbp), %r14
movq -0x50(%rbp), %rbx
movq -0xf0(%rbp), %r11
movq -0xf8(%rbp), %r10
movq 0x68(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x64bf0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)