// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: rechain_aln(overlap_region*, Candidates_list*, overlap_region*, long, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, double, long, long, long, long)
void rechain_aln(overlap_region *z, Candidates_list *cl, overlap_region *aux_o, int64_t aux_i, int64_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, int64_t tl, int64_t h_khit, int64_t rid)
{
    int64_t rcn = cl->length, ch_n, qs, qe, ts, te, mode, an0, an, todo; 
    k_mer_hit *ch_a; ul_ov_t idx; uint8_t q[2], t[2];
    ///[qs, qe) && [ts, te)
    qs = aux_o->w_list.a[aux_i].x_start; qe = aux_o->w_list.a[aux_i].x_end+1;
    ts = aux_o->w_list.a[aux_i].y_start; te = aux_o->w_list.a[aux_i].y_end+1;
    if(qe - qs < FORCE_SIN_L || te - ts < FORCE_SIN_L) return;
    mode = aux_o->w_list.a[aux_i].error_threshold;
    ch_n = gen_win_chain(z, cl, qs, qe, ts, te, wl, uref, hpc_g, rref, qstr, tu, exz, ql, tl, e_rate, h_khit, mode, rid, 0);
    ch_a = cl->list + rcn;
    if(ch_n) {
        idx.ts = idx.te = (uint32_t)-1; idx.qs = 0; idx.qe = ql; todo = 1;
        if(mode == 0) {//global
            idx.qn = 0; idx.tn = ch_n - 1;
            idx.qs = ch_a[idx.qn].self_offset;
            idx.ts = ch_a[idx.qn].offset;
            idx.qe = ch_a[idx.tn].self_offset;
            idx.te = ch_a[idx.tn].offset;
            assert(ch_a[0].self_offset == qs && ch_a[0].offset == ts);
            assert(ch_a[ch_n-1].self_offset == qe && ch_a[ch_n-1].offset == te);
            if(ch_n <= 2) todo = 0;
        } else if(mode == 1) {//forward ext
            idx.qn = 0; idx.tn = ch_n;
            idx.qs = ch_a[idx.qn].self_offset;
            idx.ts = ch_a[idx.qn].offset;
            idx.qe = ql; 
            assert(ch_a[0].self_offset == qs && ch_a[0].offset == ts);
            if(ch_n <= 1) todo = 0;
        } else if(mode == 2) {///backward ext
            idx.qn = (uint32_t)-1; idx.tn = ch_n-1;
            idx.qs = 0; 
            idx.qe = ch_a[idx.tn].self_offset;
            idx.te = ch_a[idx.tn].offset;
            assert(ch_a[ch_n-1].self_offset == qe && ch_a[ch_n-1].offset == te);
            if(ch_n <= 1) todo = 0;
        }
        if(todo) {
            an0 = aux_o->w_list.n;
            // if(z->x_id == 29033 && z->y_id == 21307) {
            //     fprintf(stderr, "[M::%s]\tan0::%ld\tq::[%u,\t%u)\tt::[%u,\t%u)\tlw::%u\trw::%u\n", __func__, an0,
            //     idx.qs, idx.qe, idx.ts, idx.te, idx.qn, idx.tn);
            // }
            ovlp_base_aln(z, ch_a, ch_n, &idx, wl, uref, hpc_g, rref, qstr, tu, exz, aux_o, e_rate, ql, tl, (uint64_t)-1);
            an = aux_o->w_list.n; q[0] = q[1] = t[0] = t[1] = 0; todo = 0;
            // if(z->x_id == 29033 && z->y_id == 21307) {
            //     fprintf(stderr, "[M::%s]\tan::%ld\n", __func__, an);
            // }
            // fprintf(stderr, "[M::%s::] awn0::%ld, awn::%lu\n", __func__, an0, an);
            ///old unaligned window could be replaced by the new aligned window
            if((an == (an0 + 1)) && (!(is_ualn_win(aux_o->w_list.a[an-1])))) {
                if(aux_o->w_list.a[aux_i].x_start == aux_o->w_list.a[an-1].x_start) q[0] = 1;
                if(aux_o->w_list.a[aux_i].x_end == aux_o->w_list.a[an-1].x_end) q[1] = 1;
                if(aux_o->w_list.a[aux_i].y_start == aux_o->w_list.a[an-1].y_start) t[0] = 1;
                if(aux_o->w_list.a[aux_i].y_end == aux_o->w_list.a[an-1].y_end) t[1] = 1;
                if((mode == 0) && q[0] && q[1] && t[0] && t[1]) todo = 1;
                if((mode == 1) && q[0] && t[0]) todo = 1;
                if((mode == 2) && q[1] && t[1]) todo = 1;
                if(todo) {
                    aux_o->w_list.a[aux_i] = aux_o->w_list.a[an-1]; aux_o->w_list.n--;
                }
            }
            // if(an > an0) {///should always > 0 as there are unmapped windows
            // }
            // aux_o->w_list.n = an0;
        }
    }
    cl->length = rcn;///must reset!!!!
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r15
movq 0x58(%rdx), %rax
shlq $0x5, %r15
movslq (%rax,%r15), %rbp
movslq 0x4(%rax,%r15), %r13
incq %r13
movq %r13, %rcx
subq %rbp, %rcx
cmpq $0x200, %rcx            # imm = 0x200
jl 0x69873
movslq 0x8(%rax,%r15), %r14
movslq 0xc(%rax,%r15), %r12
incq %r12
movq %r12, %rcx
subq %r14, %rcx
cmpq $0x200, %rcx            # imm = 0x200
jl 0x69873
movq %rdx, 0x20(%rsp)
movq 0xa0(%rsp), %rcx
movq 0x98(%rsp), %r11
movq 0x90(%rsp), %r10
movq 0x8(%rsi), %rdx
movq %rdx, 0x30(%rsp)
addq %r15, %rax
movswq 0x16(%rax), %rbx
subq $0x70, %rsp
movq %rbx, 0x50(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x10(%rsp)
movq %r9, 0xb8(%rsp)
movq %r9, 0x8(%rsp)
movq %r8, 0xb0(%rsp)
movq %r8, (%rsp)
movq $0x0, 0x60(%rsp)
movq %rdi, 0xa8(%rsp)
movq %rbp, %rdx
movq %r13, %rcx
movq %r14, %r8
movq %r12, %r9
movsd %xmm0, 0xc0(%rsp)
movq %rsi, 0x98(%rsp)
callq 0x68b84
movq 0x98(%rsp), %r11
addq $0x70, %rsp
testq %rax, %rax
je 0x6986a
movzwl %bx, %r10d
movq 0x30(%rsp), %rsi
shlq $0x4, %rsi
addq (%r11), %rsi
movq $-0x1, 0x14(%rsp)
movl $0x0, 0x8(%rsp)
movq 0xb8(%rsp), %rbx
movl %ebx, 0xc(%rsp)
testl %r10d, %r10d
je 0x69658
cmpl $0x2, %r10d
je 0x696e2
cmpl $0x1, %r10d
movq %rbx, %r12
jne 0x6973e
movl $0x0, 0x4(%rsp)
movl %eax, 0x10(%rsp)
movl 0x8(%rsi), %ecx
movl %ecx, 0x8(%rsp)
movl 0x4(%rsi), %edx
movl %edx, 0x14(%rsp)
movl %r12d, 0xc(%rsp)
cmpq %rbp, %rcx
jne 0x69639
movq %r12, %r8
movl $0x2, %ecx
cmpq %r14, %rdx
je 0x69732
leaq 0x1bdced(%rip), %rdi    # 0x22732d
leaq 0x1bbffc(%rip), %rsi    # 0x225643
leaq 0x1bdd11(%rip), %rcx    # 0x22735f
movl $0x4316, %edx           # imm = 0x4316
callq 0x2110
movl $0x0, 0x4(%rsp)
leaq -0x1(%rax), %rcx
movl %ecx, 0x10(%rsp)
movl 0x8(%rsi), %edi
movl %edi, 0x8(%rsp)
movl 0x4(%rsi), %edx
movl %edx, 0x14(%rsp)
movl %ecx, %r8d
shlq $0x4, %r8
movl 0x8(%rsi,%r8), %r9d
movl %r9d, 0xc(%rsp)
movl 0x4(%rsi,%r8), %r8d
movl %r8d, 0x18(%rsp)
cmpq %rbp, %rdi
jne 0x698a1
cmpq %r14, %rdx
jne 0x698a1
shlq $0x4, %rcx
movl 0x8(%rsi,%rcx), %edx
cmpq %r13, %rdx
jne 0x696c3
movq %rbx, %r8
addq %rsi, %rcx
movl 0x4(%rcx), %edx
movl $0x3, %ecx
cmpq %r12, %rdx
je 0x69732
leaq 0x1bdd64(%rip), %rdi    # 0x22742e
leaq 0x1bbf72(%rip), %rsi    # 0x225643
leaq 0x1bdc87(%rip), %rcx    # 0x22735f
movl $0x430f, %edx           # imm = 0x430F
callq 0x2110
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rcx, 0x4(%rsp)
leaq -0x1(%rax), %rcx
movl %ecx, 0x10(%rsp)
movl %ecx, %edx
shlq $0x4, %rdx
movl 0x8(%rsi,%rdx), %edi
movl %edi, 0xc(%rsp)
movl 0x4(%rsi,%rdx), %edx
movl %edx, 0x18(%rsp)
shlq $0x4, %rcx
movl 0x8(%rsi,%rcx), %edx
cmpq %r13, %rdx
jne 0x69882
movq %rbx, %r8
addq %rsi, %rcx
movl 0x4(%rcx), %edx
movl $0x2, %ecx
cmpq %r12, %rdx
jne 0x69882
cmpq %rcx, %rax
movq %r8, %r12
jl 0x6986a
movq 0x20(%rsp), %r14
movl %r10d, %ebp
movq 0x48(%r14), %rbx
subq $0x10, %rsp
leaq 0x14(%rsp), %rcx
movq 0x48(%rsp), %rdi
movq %rax, %rdx
movq 0x50(%rsp), %r8
movq 0x58(%rsp), %r9
movsd 0x60(%rsp), %xmm0
pushq 0xd0(%rsp)
pushq %r12
pushq %r14
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x67fda
movq 0x78(%rsp), %r11
addq $0x50, %rsp
movq 0x48(%r14), %rdx
incq %rbx
cmpq %rbx, %rdx
jne 0x6986a
movl %ebp, %ebx
movq 0x20(%rsp), %rax
movq 0x58(%rax), %rax
decq %rdx
shlq $0x5, %rdx
leaq (%rax,%rdx), %rcx
cmpw $0x7fff, 0x14(%rax,%rdx) # imm = 0x7FFF
jne 0x697e7
cmpl $0x0, 0x1c(%rcx)
jne 0x697e7
cmpw $0x0, 0x12(%rcx)
js 0x6986a
testl %ebx, %ebx
sete %sil
movl (%rax,%r15), %edi
cmpl (%rcx), %edi
movl 0x4(%rax,%r15), %edi
sete %r8b
cmpl 0x4(%rax,%rdx), %edi
sete %dil
movl 0x8(%rax,%r15), %r9d
cmpl 0x8(%rax,%rdx), %r9d
sete %r9b
movl 0xc(%rax,%r15), %r10d
cmpl 0xc(%rax,%rdx), %r10d
sete %dl
cmpl $0x1, %ebx
sete %r10b
cmpl $0x2, %ebx
sete %r11b
andb %dil, %r11b
testb %dl, %r11b
movq 0x28(%rsp), %r11
jne 0x69850
andb %r8b, %r10b
andb %r9b, %r10b
jne 0x69850
andb %r8b, %sil
andb %r9b, %dil
andb %dl, %dil
andb %sil, %dil
je 0x6986a
addq %r15, %rax
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
movq 0x20(%rsp), %rax
decq 0x48(%rax)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%r11)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bdba5(%rip), %rdi    # 0x22742e
leaq 0x1bbdb3(%rip), %rsi    # 0x225643
leaq 0x1bdac8(%rip), %rcx    # 0x22735f
movl $0x431d, %edx           # imm = 0x431D
callq 0x2110
leaq 0x1bda85(%rip), %rdi    # 0x22732d
leaq 0x1bbd94(%rip), %rsi    # 0x225643
leaq 0x1bdaa9(%rip), %rcx    # 0x22735f
movl $0x430e, %edx           # imm = 0x430E
callq 0x2110