// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: h_ec_lchain_re(ha_abuf_s*, unsigned int, char*, unsigned long, UC_Read*, unsigned long, unsigned long, All_reads*, overlap_region_alloc*, Candidates_list*, bit_extz_t*, asg16_v*, double, int, kvec_t_u8_warp*, kvec_t_u64_warp*, st_mt_t*, unsigned int*, unsigned int*, unsigned int, unsigned int, long, double, unsigned int, ma_hit_t_alloc*, ma_hit_t_alloc*)
overlap_region* h_ec_lchain_re(ha_abuf_t *ab, uint32_t rid, char* rs, uint64_t rl, UC_Read *tu, uint64_t mz_w, uint64_t mz_k, All_reads *rref, overlap_region_alloc *ol, Candidates_list *cl, bit_extz_t *exz, asg16_v* buf, double bw_thres, 
								 int apend_be, kvec_t_u8_warp* k_flag, kvec_t_u64_warp* dbg_ct, st_mt_t *sp, uint32_t *high_occ, uint32_t *low_occ, uint32_t is_accurate, uint32_t gen_off, int64_t enable_mcopy, double mcopy_rate, uint32_t mcopy_khit_cut, ma_hit_t_alloc *in0, ma_hit_t_alloc *in1)
{
    // fprintf(stderr, "-mm-[M::%s]\tchain_cutoff::%u\n", __func__, chain_cutoff);
    uint64_t on = 0, k, ol0, wl = (asm_opt.is_ont)?(WINDOW_OHC):(WINDOW_HC), m; ma_hit_t *oa = NULL; overlap_region *z = NULL, *aux_o = NULL, t; Window_Pool w; double err = asm_opt.max_ov_diff_ec;

    int64_t max_skip, max_iter, max_dis, quick_check; double chn_pen_gap, chn_pen_skip; 
	set_lchain_dp_op(is_accurate, mz_k, &max_skip, &max_iter, &max_dis, &chn_pen_gap, &chn_pen_skip, &quick_check);

    init_Window_Pool(&w, rl, wl, (int)(1.0/err));

    on = in0->length + in1->length + 1;
    clear_overlap_region_alloc(ol); 
    clear_Candidates_list(cl); 
    if(on > ol->size) {
        REALLOC(ol->list, on);
        memset(ol->list+ol->size, 0, sizeof(overlap_region)*(on-ol->size));
        ol->size = on;   
    }
    on = in0->length + in1->length; aux_o = &(ol->list[on]);
    ol->length = 0; ol->mapped_overlaps_length = 0; m = 0;
    

    // get the list of anchors
    get_mz1(rs, rl, mz_w, mz_k, 0, !(asm_opt.flag & HA_F_NO_HPC), ab, ha_flt_tab, ha_idx, asm_opt.mz_sample_dist, k_flag, dbg_ct, NULL, -1, asm_opt.dp_min_len, -1, sp, asm_opt.mz_rewin, 0, NULL, 0);

    oa = in0->buffer; on = in0->length;
    for (k = 0; k < on; k++) {
        ol0 = ol->length;
        if(oa[k].el) {
            z = &(ol->list[ol->length++]);
            z->x_id = rid; z->y_id = oa[k].tn;
            z->x_pos_strand = 0; z->y_pos_strand = oa[k].rev;
            z->x_pos_s = (uint32_t)oa[k].qns;
            z->x_pos_e = oa[k].qe - 1;
            z->y_pos_s = oa[k].ts;
            z->y_pos_e = oa[k].te - 1;

            z->is_match = 1;
            z->align_length = z->overlapLen = z->shared_seed = z->x_pos_e + 1 - z->x_pos_s;
            z->non_homopolymer_errors = z->strong = 0;

            set_exact_exz(exz, z->x_pos_s, z->x_pos_e + 1, z->y_pos_s, z->y_pos_e + 1); push_alnw(z, exz);

            // if(oa[k].tn == 15382) fprintf(stderr, "-em-[M::%s]\tqn::%u\ttn::%u\terr::%u\n", __func__, rid, oa[k].tn, ol->list[ol->length-1].non_homopolymer_errors);
        } else {
            if(oa[k].rev) recover_UC_Read_RC(tu, rref, oa[k].tn);
            else recover_UC_Read(tu, rref, oa[k].tn);

            get_pi_ec_chain(ab, rid, rl, oa[k].tn, tu->seq, tu->length, mz_w, mz_k, ol, cl, bw_thres, apend_be, k_flag, dbg_ct, sp, high_occ, low_occ, gen_off, enable_mcopy, mcopy_rate, mcopy_khit_cut, max_skip, max_iter, max_dis, quick_check, chn_pen_gap, chn_pen_skip);
            assert(ol->length - ol0 <= 1);
            if((ol->length > ol0) && (gen_hc_r_alin_re(&(ol->list[ol0]), cl, rs, rl, tu->seq, tu->length, exz, aux_o, asm_opt.max_ov_diff_ec, w.window_length, rid, E_KHIT, 1, buf))) {
                ol->list[ol0].y_pos_strand = oa[k].rev;
                // if(oa[k].tn == 15382) fprintf(stderr, "-mm-[M::%s]\tqn::%u\ttn::%u\terr::%u\n", __func__, rid, oa[k].tn, ol->list[ol->length-1].non_homopolymer_errors);
            } 
        }

        if((ol->length <= ol0) || (ol->list[ol0].is_match != 1)) continue;

        if(m != ol0) {
            t = ol->list[m];
            ol->list[m] = ol->list[ol0];
            ol->list[ol0] = t;
        }

        m++;
    }


    oa = in1->buffer; on = in1->length;
    for (k = 0; k < on; k++) {
        ol0 = ol->length;

        if(oa[k].rev) recover_UC_Read_RC(tu, rref, oa[k].tn);
        else recover_UC_Read(tu, rref, oa[k].tn);

        get_pi_ec_chain(ab, rid, rl, oa[k].tn, tu->seq, tu->length, mz_w, mz_k, ol, cl, bw_thres, apend_be, k_flag, dbg_ct, sp, high_occ, low_occ, gen_off, enable_mcopy, mcopy_rate, mcopy_khit_cut, max_skip, max_iter, max_dis, quick_check, chn_pen_gap, chn_pen_skip);
        assert(ol->length - ol0 <= 1);
        if((ol->length > ol0) && (gen_hc_r_alin_re(&(ol->list[ol0]), cl, rs, rl, tu->seq, tu->length, exz, aux_o, asm_opt.max_ov_diff_ec, w.window_length, rid, E_KHIT, 1, buf))) {
            ol->list[ol0].y_pos_strand = oa[k].rev;
            // if(oa[k].tn == 15382) fprintf(stderr, "-mm-[M::%s]\tqn::%u\ttn::%u\terr::%u\n", __func__, rid, oa[k].tn, ol->list[ol->length-1].non_homopolymer_errors);
        } 

        if((ol->length <= ol0) || (ol->list[ol0].is_match != 1)) continue;

        if(m != ol0) {
            t = ol->list[m];
            ol->list[m] = ol->list[ol0];
            ol->list[ol0] = t;
        }
        
        m++;
    }

    ol->length = m;
    
    // fprintf(stderr, "[M::%s]\tnew_n::%lu\told_n::%u\n", __func__, ol->length, in0->length + in1->length);
    assert(ol->length <= (in0->length + in1->length));


    // dbg_overlap_region_cigar(ol->list, ol->length, rs, rref, tu);


    return aux_o;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movq %r9, 0x48(%rsp)
movq %r8, %r14
movq %rcx, %r13
movq %rdx, 0x30(%rsp)
movl %esi, 0x14(%rsp)
movq %rdi, 0x40(%rsp)
movq 0x208(%rsp), %rbp
movq 0x200(%rsp), %r15
leaq 0x10a272(%rip), %rax    # 0x295758
cmpq $0x0, 0x2a8(%rax)
movl $0x307, %eax            # imm = 0x307
movl $0x177, %ecx            # imm = 0x177
cmoveq %rax, %rcx
movq %rcx, 0x50(%rsp)
movq 0x198(%rsp), %rbx
movq 0x190(%rsp), %r12
movq 0x180(%rsp), %rsi
movl 0x1e0(%rsp), %edi
leaq 0x78(%rsp), %rax
leaq 0x68(%rsp), %r10
leaq 0x90(%rsp), %rdx
leaq 0x88(%rsp), %rcx
leaq 0x80(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %rax
pushq %r10
callq 0x12fb4d
addq $0x10, %rsp
movl 0xc(%r15), %eax
movl 0xc(%rbp), %ecx
leal (%rax,%rcx), %r15d
incl %r15d
movq %r12, %rdi
callq 0x3d018
movq %rbx, %rdi
callq 0x3f1bd
movq (%r12), %rbx
cmpq %r15, 0x8(%r12)
jae 0x18b5ba
imulq $0xa8, %r15, %rsi
movq %rbx, %rdi
callq 0x2370
movq %rax, %rbx
movq %rax, (%r12)
movq 0x8(%r12), %rax
imulq $0xa8, %rax, %rdi
addq %rbx, %rdi
movq %r15, %rcx
subq %rax, %rcx
imulq $0xa8, %rcx, %rdx
xorl %esi, %esi
callq 0x20e0
movq %r15, 0x8(%r12)
movl 0xc(%rbp), %eax
movq 0x200(%rsp), %r15
addl 0xc(%r15), %eax
imulq $0xa8, %rax, %rax
addq %rbx, %rax
movq %rax, 0x28(%rsp)
leaq 0x10(%r12), %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r12)
leaq 0x10a166(%rip), %rdi    # 0x295758
movl (%rdi), %r9d
movl 0xa0(%rdi), %eax
notl %r9d
andl $0x1, %r9d
leaq 0x10ae27(%rip), %rcx    # 0x296430
movq (%rcx), %rcx
leaq 0x10ae25(%rip), %rdx    # 0x296438
movq (%rdx), %rdx
movl 0xa8(%rdi), %esi
movl 0x1f8(%rdi), %edi
subq $0x70, %rsp
movups %xmm0, 0x60(%rsp)
movl %eax, 0x50(%rsp)
movq 0x238(%rsp), %rax
movq %rax, 0x48(%rsp)
movl %edi, 0x40(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x228(%rsp), %rax
movq %rax, 0x20(%rsp)
movl %esi, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, (%rsp)
movl $0x0, 0x58(%rsp)
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
movq $0x0, 0x30(%rsp)
movss 0xaf51b(%rip), %xmm0    # 0x23abb0
movq 0xa0(%rsp), %rdi
movl %r13d, %esi
movq 0xb8(%rsp), %rdx
movq 0x1f0(%rsp), %rcx
xorl %r8d, %r8d
callq 0x13444e
addq $0x70, %rsp
movl 0xc(%r15), %eax
movl 0x14(%rsp), %ecx
movq %rcx, 0x20(%rsp)
testq %rax, %rax
movq %r13, 0x18(%rsp)
movq %r12, %rbx
je 0x18ba05
movq (%r15), %r12
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
movq %rax, 0x98(%rsp)
xorl %ebp, %ebp
movq $0x0, (%rsp)
movq 0x8(%rsp), %rax
movq (%rax), %r15
cmpb $0x0, 0x20(%r12,%rbp)
je 0x18b7cb
movq (%rbx), %rax
leaq 0x1(%r15), %rcx
movq %rcx, 0x10(%rbx)
imulq $0xa8, %r15, %rdi
leaq (%rax,%rdi), %rcx
movq %rcx, 0x38(%rsp)
movl 0x14(%rsp), %ecx
movl %ecx, (%rax,%rdi)
movl 0xc(%r12,%rbp), %ecx
movl %ecx, 0x10(%rax,%rdi)
xorl %r11d, %r11d
movl %r11d, 0xc(%rax,%rdi)
movl 0x18(%r12,%rbp), %ecx
shrl $0x1f, %ecx
movl %ecx, 0x1c(%rax,%rdi)
movq (%r12,%rbp), %r9
movl %r9d, %esi
movl %r9d, 0x4(%rax,%rdi)
movl 0x8(%r12,%rbp), %edx
leal -0x1(%rdx), %ecx
movl %ecx, 0x8(%rax,%rdi)
movl 0x10(%r12,%rbp), %ecx
movl %ecx, 0x14(%rax,%rdi)
movl 0x14(%r12,%rbp), %r8d
leal -0x1(%r8), %r10d
movl %r10d, 0x18(%rax,%rdi)
movb $0x1, 0x2c(%rax,%rdi)
movl %edx, %r10d
subl %r9d, %r10d
movl %r10d, 0x24(%rax,%rdi)
movl %r10d, 0x20(%rax,%rdi)
movl %r10d, 0x28(%rax,%rdi)
movb $0x0, 0x2e(%rax,%rdi)
movl %r11d, 0x30(%rax,%rdi)
movq %r14, %rbx
movq %r13, %r14
movq 0x1a0(%rsp), %r13
movq %r13, %rdi
callq 0x663fa
movq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %r14, %r13
movq %rbx, %r14
callq 0x65a71
jmp 0x18b943
testb $-0x80, 0x1b(%r12,%rbp)
movl 0xc(%r12,%rbp), %edx
movq %r14, %rdi
movq 0x188(%rsp), %rsi
jne 0x18b7ea
callq 0x333e0
jmp 0x18b7ef
callq 0x334a6
movl 0xc(%r12,%rbp), %ecx
movq (%r14), %r8
movq 0x8(%r14), %r9
movsd 0x70(%rsp), %xmm2
movsd 0x68(%rsp), %xmm3
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
pushq 0x80(%rsp)
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
pushq 0xb0(%rsp)
movl 0x220(%rsp), %eax
pushq %rax
pushq 0x220(%rsp)
movl 0x220(%rsp), %eax
pushq %rax
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
movl 0x218(%rsp), %eax
pushq %rax
pushq 0x208(%rsp)
pushq %rbx
pushq 0x200(%rsp)
pushq 0xd0(%rsp)
callq 0x1345a7
addq $0x90, %rsp
movq 0x10(%rbx), %rax
movq %rax, %rcx
subq %r15, %rcx
cmpq $0x2, %rcx
jae 0x18bcbe
cmpq %r15, %rax
jbe 0x18b943
imulq $0xa8, %r15, %rbx
movq 0x190(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movq (%r14), %r8
movq 0x8(%r14), %r9
leaq 0x109e74(%rip), %rax    # 0x295758
movsd 0xc0(%rax), %xmm0
subq $0x8, %rsp
movq 0x1a0(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rcx
pushq 0x1b0(%rsp)
pushq $0x1
pushq $0x1f
pushq 0x40(%rsp)
pushq 0x78(%rsp)
pushq 0x58(%rsp)
pushq 0x1d8(%rsp)
callq 0x84832
addq $0x40, %rsp
testq %rax, %rax
je 0x18b943
movl 0x18(%r12,%rbp), %eax
shrl $0x1f, %eax
movq 0x190(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, 0x1c(%rcx,%rbx)
movq 0x8(%rsp), %rax
cmpq %r15, (%rax)
jbe 0x18b9e1
movq 0x190(%rsp), %rax
movq (%rax), %r13
imulq $0xa8, %r15, %rbx
cmpb $0x1, 0x2c(%r13,%rbx)
jne 0x18b9dc
movq (%rsp), %rax
cmpq %r15, %rax
je 0x18b9ca
imulq $0xa8, %rax, %r15
addq %r13, %r15
movl $0xa8, %edx
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %rbx, %r13
movl $0xa8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x190(%rsp), %rax
addq (%rax), %rbx
movl $0xa8, %edx
movq %rbx, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2160
movq (%rsp), %rax
incq %rax
movq 0x18(%rsp), %r13
movq 0x190(%rsp), %rbx
jmp 0x18b9ed
movq 0x18(%rsp), %r13
movq 0x190(%rsp), %rbx
movq (%rsp), %rax
movq %rax, (%rsp)
addq $0x28, %rbp
cmpq %rbp, 0x98(%rsp)
jne 0x18b6f7
jmp 0x18ba0d
movq $0x0, (%rsp)
movq 0x208(%rsp), %rcx
movl 0xc(%rcx), %eax
testq %rax, %rax
je 0x18bc6a
movq (%rcx), %r12
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq (%rax), %r15
testb $-0x80, 0x1b(%r12,%rbp)
movl 0xc(%r12,%rbp), %edx
movq %r14, %rdi
movq 0x188(%rsp), %rsi
jne 0x18ba5a
callq 0x333e0
jmp 0x18ba5f
callq 0x334a6
movl 0xc(%r12,%rbp), %ecx
movq (%r14), %r8
movq 0x8(%r14), %r9
movsd 0x70(%rsp), %xmm2
movsd 0x68(%rsp), %xmm3
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r13, %rdx
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
pushq 0x80(%rsp)
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
pushq 0xb0(%rsp)
movl 0x220(%rsp), %eax
pushq %rax
pushq 0x220(%rsp)
movl 0x220(%rsp), %eax
pushq %rax
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
pushq 0x218(%rsp)
movl 0x218(%rsp), %eax
pushq %rax
pushq 0x208(%rsp)
pushq %rbx
pushq 0x200(%rsp)
pushq 0xd0(%rsp)
callq 0x1345a7
addq $0x90, %rsp
movq 0x10(%rbx), %rax
movq %rax, %rcx
subq %r15, %rcx
cmpq $0x2, %rcx
jae 0x18bc9f
cmpq %r15, %rax
jbe 0x18bbb3
imulq $0xa8, %r15, %rbx
movq 0x190(%rsp), %rax
movq (%rax), %rdi
addq %rbx, %rdi
movq (%r14), %r8
movq 0x8(%r14), %r9
leaq 0x109c04(%rip), %rax    # 0x295758
movsd 0xc0(%rax), %xmm0
subq $0x8, %rsp
movq 0x1a0(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rcx
pushq 0x1b0(%rsp)
pushq $0x1
pushq $0x1f
pushq 0x40(%rsp)
pushq 0x78(%rsp)
pushq 0x58(%rsp)
pushq 0x1d8(%rsp)
callq 0x84832
addq $0x40, %rsp
testq %rax, %rax
je 0x18bbb3
movl 0x18(%r12,%rbp), %eax
shrl $0x1f, %eax
movq 0x190(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, 0x1c(%rcx,%rbx)
movq 0x8(%rsp), %rax
cmpq %r15, (%rax)
jbe 0x18bc46
movq 0x190(%rsp), %rax
movq (%rax), %r13
imulq $0xa8, %r15, %rbx
cmpb $0x1, 0x2c(%r13,%rbx)
jne 0x18bc41
movq (%rsp), %rax
cmpq %r15, %rax
je 0x18bc3a
imulq $0xa8, %rax, %r15
addq %r13, %r15
movl $0xa8, %edx
leaq 0xa0(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %rbx, %r13
movl $0xa8, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2160
movq 0x190(%rsp), %rax
addq (%rax), %rbx
movl $0xa8, %edx
movq %rbx, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2160
movq (%rsp), %rax
incq %rax
movq %rax, (%rsp)
movq 0x18(%rsp), %r13
movq 0x190(%rsp), %rbx
addq $0x28, %rbp
cmpq %rbp, 0x38(%rsp)
jne 0x18ba33
movq 0x208(%rsp), %rax
movl 0xc(%rax), %edx
jmp 0x18bc6c
xorl %edx, %edx
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
movq 0x200(%rsp), %rax
addl 0xc(%rax), %edx
cmpq %rdx, %rcx
ja 0x18bcdd
movq 0x28(%rsp), %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb2cf9(%rip), %rdi     # 0x23e99f
leaq 0xb238e(%rip), %rsi     # 0x23e03b
leaq 0xb2d01(%rip), %rcx     # 0x23e9b5
movl $0x1195, %edx           # imm = 0x1195
callq 0x2110
leaq 0xb2cda(%rip), %rdi     # 0x23e99f
leaq 0xb236f(%rip), %rsi     # 0x23e03b
leaq 0xb2ce2(%rip), %rcx     # 0x23e9b5
movl $0x117a, %edx           # imm = 0x117A
callq 0x2110
leaq 0xb2e31(%rip), %rdi     # 0x23eb15
leaq 0xb2350(%rip), %rsi     # 0x23e03b
leaq 0xb2cc3(%rip), %rcx     # 0x23e9b5
movl $0x11a9, %edx           # imm = 0x11A9
callq 0x2110