// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: worker_hap_ec(void*, long, int)
static void worker_hap_ec(void *data, long i, int tid)
{
	ec_ovec_buf_t0 *b = &(((ec_ovec_buf_t*)data)->a[tid]);
    uint32_t high_occ = asm_opt.hom_cov * (2.0 - HA_KMER_GOOD_RATIO);
    uint32_t low_occ = asm_opt.hom_cov * HA_KMER_GOOD_RATIO;
    overlap_region *aux_o = NULL; asg64_v buf0; uint32_t qlen = 0;

    /**
    if((i != 1129685) && (i != 1137865) && (i != 1137917) && (i != 1140647) && (i != 1144740) && (i != 1148936) && (i != 1149134) && (i != 1151224) && (i != 1151386) && (i != 1152960) && (i != 1154846) && (i != 1154881) && (i != 1155112) && 
        (i != 1156823) && (i != 1157099) && (i != 1157393) && (i != 1158300) && (i != 1158368) && (i != 1160411) && (i != 1160659) && (i != 1161458) && (i != 1163595) && (i != 1164084) && (i != 1164230) && (i != 1165050) && (i != 1168249) && 
        (i != 1168304) && (i != 1168514) && (i != 1170447) && (i != 1171377) && (i != 1171387) && (i != 1172376) && (i != 1173566) && (i != 1174275) && (i != 1174434) && (i != 1174511) && (i != 1174860) && (i != 1175306) && (i != 1175314) && 
        (i != 1177101) && (i != 1178196) && (i != 1178470) && (i != 1179327) && (i != 1179626) && (i != 1180357) && (i != 1181347) && (i != 1181422) && (i != 1181725) && (i != 1183135) && (i != 1183734) && (i != 1185569) && (i != 1185604) && 
        (i != 1186192) && (i != 1188441) && (i != 1188487) && (i != 1189865) && (i != 1189943) && (i != 1192819) && (i != 1193133) && (i != 1196908) && (i != 1197590) && (i != 1200549) && (i != 1200757) && (i != 1205500)) return;

    fprintf(stderr, "%ld\t+++\n", i);
    **/
    // if(i < 1100000 || i > 1400000) return;
    // if(i % 100000 == 0) fprintf(stderr, "-a-[M::%s-beg] rid->%ld\n", __func__, i);
    // if (memcmp("c42804f3-0e13-43a0-8a71-b91b40accf9a", Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) {
    // if (memcmp("b2e68ecf-381a-439c-b676-c1e6831d6acf", Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) {
    // if (memcmp("e3f3f43a-e200-4cac-8acd-3f85428f3811", Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) {
    // if (memcmp("b4bd5ccb-2fe3-447e-b7b8-7a7b26fa0f7a", Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) {
    //     fprintf(stderr, "-a-[M::%s-beg] rid->%ld\n", __func__, i);
    // } else {
    //     return;
    // }

    // if(i != 4080965) return;
    // if(i != 4325346) return;
    // if(i != 4378784) return;
    ///for debug indel
    // if(i != 1238) return;
    // if(i != 2410) return;
    // if(i != 4198005) return;
    // if(i != 682) return;

    // debug_retrive_bqual(D, &b->v8t, i, 256); return;

    recover_UC_Read(&b->self_read, &R_INF, i); qlen = b->self_read.length; 

    h_ec_lchain(b->ab, i, b->self_read.seq, b->self_read.length, asm_opt.mz_win, asm_opt.k_mer_length, &R_INF, &b->olist, &b->clist, ((asm_opt.is_ont)?(0.05):(0.02)), asm_opt.max_n_chain, 1, NULL, NULL, &(b->sp), &high_occ, &low_occ, 1, 1, 3, 0.7, 2, 32, COV_W);///ONT high error

    // b->num_read_base += b->olist.length;
    b->cnt[0] += b->self_read.length;

    aux_o = fetch_aux_ovlp(&b->olist);///must be here

    // stderr_phase_ovlp(&b->olist);

    ///debug for memory
    // snprintf(NULL, 0, "dwn::%u\tdcn::%u", (uint32_t)aux_o->w_list.n, (uint32_t)aux_o->w_list.c.n);
    ///mz1_ha_sketch(rs, rl, mz_w, mz_k, 0, !(asm_opt.flag & HA_F_NO_HPC), &ab->mz, ha_flt_tab, asm_opt.mz_sample_dist, k_flag, dbg_ct, NULL, -1, asm_opt.dp_min_len, -1, sp, asm_opt.mz_rewin, 0, NULL);
    // if((asm_opt.is_ont) && (b->olist.length)) get_mz1(qu->seq, qu->length, RES_W, RES_K, 0, !(asm_opt.flag & HA_F_NO_HPC), b->ab, NULL, NULL, asm_opt.mz_sample_dist, NULL, NULL, NULL, -1, asm_opt.dp_min_len, -1, &(b->sp), asm_opt.mz_rewin, 0, NULL, 0);

    gen_hc_r_alin_ea(&b->olist, &b->clist, &R_INF, &b->self_read, &b->ovlp_read, &b->exz, aux_o, asm_opt.max_ov_diff_ec, (asm_opt.is_ont)?(WINDOW_OHC):(WINDOW_HC), i, E_KHIT/**asm_opt.k_mer_length**/, 1, &b->v16, &b->v64, &(R_INF.paf[i]), asm_opt.is_ont, (asm_opt.is_ont)?(0.006):(-1), (asm_opt.is_ont)?(64):(-1));
    ///for debug indel
    // prt_ovlp_sam(&b->olist, &b->ovlp_read, b->self_read.seq, b->self_read.length);


    // fprintf(stderr, "\n[M::%s] rid::%ld\t%.*s\tlen::%lld\tocc::%lu\n", __func__, i, (int)Get_NAME_LENGTH(R_INF, i), Get_NAME(R_INF, i), b->self_read.length, b->olist.length);

    // fprintf(stderr, "[M::%s] rid::%ld\n", __func__, i);
    // debug_mm_exact_cigar(&b->olist, i, &b->self_read, &b->ovlp_read);

    // b->num_correct_base += b->olist.length;

    copy_asg_arr(buf0, b->sp); 
    rphase_hc(&b->olist, &R_INF, &b->hap, &b->self_read, &b->ovlp_read, &b->pidx, &b->v64, &buf0, 0, WINDOW_MAX_SIZE, b->self_read.length, 1/**, 0**/, i, (asm_opt.is_ont)?HPC_PL:0, asm_opt.is_ont, ((asm_opt.is_ont)?&(b->clist.chainDP):NULL), ((asm_opt.is_sc)?&(b->v8q):NULL), ((asm_opt.is_sc)?&(b->v8t):NULL), (asm_opt.is_ont)?1:0);
    copy_asg_arr(b->sp, buf0);
    ///for debug indel
    // stderr_phase_ovlp(&b->olist);

    dedup_chains(&b->olist);

    copy_asg_arr(buf0, b->sp);
    b->cnt[1] += wcns_gen(&b->olist, &R_INF, &b->self_read, &b->ovlp_read, &b->exz, &b->pidx, &b->v64, &buf0, 0, 512, b->self_read.length, 3, 0.500001, aux_o, &b->v32, &b->cns, 256, i);
    copy_asg_arr(b->sp, buf0);

    push_nec_re(aux_o, &(scc.a[i]));
    push_nec_re(aux_o, &(scb.a[i]));

    // if((asm_opt.is_ont) && is_chemical_r_qual(&b->olist, &b->v64, qlen, 1, 16, &(b->v8q), i)/**(is_uncorrected_read(&b->olist, &b->v64, qlen, 1600))**/) {
    //     // b->olist.length = 0;
    //     fprintf(stderr, "[M::%s] rid::%ld\t%.*s\n\n", __func__, i, (int)Get_NAME_LENGTH(R_INF, i), Get_NAME(R_INF, i));
    // }

    push_ne_ovlp(&(R_INF.paf[i]), &b->olist, 1, &R_INF, &(scc.a[i])/**, i, &b->self_read, &b->ovlp_read**/);
    push_ne_ovlp(&(R_INF.reverse_paf[i]), &b->olist, 2, &R_INF, NULL/**, i, NULL, NULL**/);


    check_well_cal(&(scc.a[i]), &b->v64, &(R_INF.paf[i].is_fully_corrected), &(R_INF.paf[i].is_abnormal), qlen, (MIN_COVERAGE_THRESHOLD*2), &(R_INF.paf[i]));
    R_INF.trio_flag[i] = AMBIGU;

    // uint32_t k;
    // for (k = 0; k < b->olist.length; k++) {
    //     if(b->olist.list[k].is_match == 1) b->num_recorrect_base++;
    // }
    ///for debug indel
    // exit(1);
    
    

    // prt_chain(&b->olist);

    // ul_map_lchain(b->abl, (uint32_t)-1, s->seq[i], s->len[i], s->opt->w, s->opt->k, s->uu, &b->olist, &b->clist, s->opt->bw_thres, 
    //         s->opt->max_n_chain, 1, NULL, &(b->tmp_region), NULL, &(b->sp), &high_occ, NULL, 0, 1, 0.2/**0.75**/, 2, 3);

    /**
	int fully_cov, abnormal;
    // if(i != 12578) return;
    // fprintf(stderr, "[M::%s-beg] rid->%ld\n", __func__, i);
    // if (memcmp("7897e875-76e5-42c8-bc37-94b370c4cc8d", Get_NAME((R_INF), i), Get_NAME_LENGTH((R_INF),i)) == 0) {
    //     fprintf(stderr, "[M::%s-beg] rid->%ld\n", __func__, i);
    // } else {
    //     return;
    // }

    ha_get_candidates_interface(b->ab, i, &b->self_read, &b->olist, &b->olist_hp, &b->clist, 
    0.02, asm_opt.max_n_chain, 1, NULL, &b->r_buf, &(R_INF.paf[i]), &(R_INF.reverse_paf[i]), &(b->tmp_region), NULL, &(b->sp));

	clear_Cigar_record(&b->cigar1);
	clear_Round2_alignment(&b->round2);

	correct_overlap(&b->olist, &R_INF, &b->self_read, &b->correct, &b->ovlp_read, &b->POA_Graph, &b->DAGCon,
			&b->cigar1, &b->hap, &b->round2, &b->r_buf, &(b->tmp_region.w_list), 0, 1, &fully_cov, &abnormal);

	b->num_read_base += b->self_read.length;
	b->num_correct_base += b->correct.corrected_base;
	b->num_recorrect_base += b->round2.dumy.corrected_base;

	push_cigar(R_INF.cigars, i, &b->cigar1);
	push_cigar(R_INF.second_round_cigar, i, &b->round2.cigar);

	R_INF.paf[i].is_fully_corrected = 0;
	if (fully_cov) {
		if (get_cigar_errors(&b->cigar1) == 0 && get_cigar_errors(&b->round2.cigar) == 0)
			R_INF.paf[i].is_fully_corrected = 1;
	}
	R_INF.paf[i].is_abnormal = abnormal;

    R_INF.trio_flag[i] = AMBIGU;
    
    ///need to be fixed in r305
    // if(ha_idx_hp == NULL)
    // {
    //     R_INF.trio_flag[i] += collect_hp_regions(&b->olist, &R_INF, &(b->k_flag), RESEED_HP_RATE, Get_READ_LENGTH(R_INF, i), NULL);
    // }

    if (R_INF.trio_flag[i] != AMBIGU || b->save_ov) {
		int is_rev = (asm_opt.number_of_round % 2 == 0);
		push_overlaps(&(R_INF.paf[i]), &b->olist, 1, &R_INF, is_rev);
		push_overlaps(&(R_INF.reverse_paf[i]), &b->olist, 2, &R_INF, is_rev);
	}

    if(het_cnt) het_cnt[i] = get_het_cnt(&b->hap);
    // fprintf(stderr, "[M::%s-end] rid->%ld\n", __func__, i);
    **/
    // exit(1);
    refresh_ec_ovec_buf_t0(b, REFRESH_N);

    /**
    fprintf(stderr, "%ld\t---\n", i);
    **/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r15
movq (%rdi), %r12
movslq %edx, %rax
imulq $0x5c0, %rax, %rbx      # imm = 0x5C0
leaq (%r12,%rbx), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x107996(%rip), %r13    # 0x253758
cvtsi2sdl 0xd0(%r13), %xmm0
movsd 0xa0cbd(%rip), %xmm1    # 0x1eca90
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
leaq 0x24(%rsp), %rdx
movl %eax, (%rdx)
mulsd 0x9f7d5(%rip), %xmm0    # 0x1eb5c0
cvttsd2si %xmm0, %rax
leaq 0x6c(%rsp), %rbp
movl %eax, (%rbp)
leaq 0x108431(%rip), %rsi    # 0x254230
movq %r15, %rdx
callq 0x715e
leaq (%r12,%rbx), %rax
addq $0x98, %rax
movq -0x98(%rax), %rdx
movq -0x90(%rax), %rcx
movq %rcx, 0x60(%rsp)
movq 0x20(%rax), %rdi
movq %rax, %r14
movslq 0x94(%r13), %r8
movslq 0x80(%r13), %r9
leaq (%r12,%rbx), %r11
addq $0x40, %r11
xorl %eax, %eax
cmpq $0x0, 0x2a8(%r13)
sete %al
leaq 0xa3fcb(%rip), %rsi     # 0x1efe20
movsd (%rsi,%rax,8), %xmm0
movl 0xe8(%r13), %eax
leaq (%r12,%rbx), %rsi
addq $0x568, %rsi            # imm = 0x568
subq $0x80, %rsp
pushq $0x3
popq %r10
movq %r10, 0x60(%rsp)
pushq $0x1
popq %r10
movl %r10d, 0x58(%rsp)
movl %r10d, 0x50(%rsp)
movq %rbp, 0x48(%rsp)
leaq 0xa4(%rsp), %r13
movq %r13, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
xorpd %xmm1, %xmm1
movupd %xmm1, 0x28(%rsp)
movl %r10d, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq %r11, %r13
movq %r11, 0x90(%rsp)
movq %r11, 0x10(%rsp)
movq %r14, 0x8(%rsp)
leaq 0x108360(%rip), %rax    # 0x254230
movq %rax, (%rsp)
movq $0xc00, 0x78(%rsp)      # imm = 0xC00
movl $0x20, 0x70(%rsp)
movl $0x2, 0x68(%rsp)
movsd 0x975fb(%rip), %xmm1    # 0x1e34f0
movq %r15, %rbp
movl %ebp, %esi
callq 0xea30c
addq $0x80, %rsp
movq -0x38(%r13), %rax
movq %r14, %r13
addq %rax, 0x28(%r14)
movq %r14, %rdi
movq %r14, 0x38(%rsp)
callq 0x13d428
movq %rax, 0x40(%rsp)
movq %r12, 0x28(%rsp)
leaq (%r12,%rbx), %r8
addq $0x20, %r8
leaq 0x3e0(%r12,%rbx), %r9
movq %r9, 0x50(%rsp)
leaq 0x107814(%rip), %rax    # 0x253758
movsd 0xc0(%rax), %xmm0
movq 0x2a8(%rax), %rcx
xorl %r10d, %r10d
testq %rcx, %rcx
sete %r10b
movl $0x307, %edx            # imm = 0x307
movl $0x177, %eax            # imm = 0x177
cmoveq %rdx, %rax
leaq (%r12,%rbx), %r15
addq $0x500, %r15            # imm = 0x500
leaq (%r12,%rbx), %rdx
addq $0x4d0, %rdx            # imm = 0x4D0
movq %rdx, 0x18(%rsp)
imulq $0x18, %rbp, %rsi
movq %rsi, 0x58(%rsp)
movq %rbp, 0x30(%rsp)
leaq 0xa3e95(%rip), %rdx     # 0x1efe30
movsd (%rdx,%r10,8), %xmm1
leaq 0x108288(%rip), %rdx    # 0x254230
movq 0x78(%rdx), %r11
addq %rsi, %r11
negq %r10
orq $0x40, %r10
movzbl %cl, %r14d
movq %r13, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rcx
movq %r8, %r12
pushq %r10
pushq %r14
pushq %r11
pushq 0x30(%rsp)
pushq %r15
pushq $0x1
pushq $0x1f
pushq %rbp
pushq %rax
pushq 0x88(%rsp)
callq 0x143a42
addq $0x50, %rsp
movq 0x78(%r15), %rax
leaq 0x70(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x68(%r15), %xmm0
movaps %xmm0, (%rcx)
movq 0x28(%rsp), %rcx
leaq 0xf8(%rcx,%rbx), %rdx
leaq 0x107743(%rip), %rsi    # 0x253758
movq 0x2a8(%rsi), %rax
movq 0x2b0(%rsi), %r11
xorl %r14d, %r14d
testq %rax, %rax
setne %r14b
pushq $0xc
popq %r10
cmoveq %rax, %r10
leaq 0x58(%rcx,%rbx), %rbp
movq %rbp, 0x10(%rsp)
cmoveq %rax, %rbp
testq %r11, %r11
leaq 0x518(%rcx,%rbx), %r13
cmoveq %r11, %r13
leaq 0x4b8(%rcx,%rbx), %r9
movq %r9, 0x48(%rsp)
leaq 0x530(%rcx,%rbx), %r15
cmovneq %r15, %r11
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
leaq 0x1081b5(%rip), %rsi    # 0x254230
movq 0x10(%rsp), %rcx
movq %r12, %r8
pushq %r14
pushq %r11
pushq %r13
pushq %rbp
pushq %rax
pushq %r10
movq 0x68(%rsp), %r14
pushq %r14
pushq $0x1
popq %rax
pushq %rax
movq %r12, %r13
pushq -0x18(%r12)
pushq $0x1ac                  # imm = 0x1AC
pushq $0x0
leaq 0xd0(%rsp), %r12
pushq %r12
movq 0x80(%rsp), %rbp
pushq %rbp
callq 0x40d06
addq $0x70, %rsp
movq 0x10(%r12), %rax
movq %rax, 0x48(%r15)
movaps (%r12), %xmm0
movups %xmm0, 0x38(%r15)
movq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0x1444ad
movq 0x48(%r15), %rax
leaq 0x70(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups 0x38(%r15), %xmm0
movaps %xmm0, (%rcx)
movq %rcx, %r15
movq 0x28(%rsp), %rcx
leaq (%rcx,%rbx), %rax
addq $0x4e8, %rax            # imm = 0x4E8
addq %rcx, %rbx
addq $0x580, %rbx            # imm = 0x580
subq $0x8, %rsp
movsd 0x97490(%rip), %xmm0    # 0x1e35b0
movq %r12, %rdi
leaq 0x108106(%rip), %rsi    # 0x254230
movq 0x10(%rsp), %rdx
movq %r13, %rcx
movq 0x58(%rsp), %r8
movq 0x50(%rsp), %r9
pushq %r14
pushq $0x100                  # imm = 0x100
pushq %rbx
pushq %rax
movq 0x68(%rsp), %r13
pushq %r13
pushq $0x3
popq %rax
pushq %rax
movq 0x48(%rsp), %r14
pushq -0x50(%r14)
pushq $0x200                  # imm = 0x200
pushq $0x0
pushq %r15
pushq %rbp
callq 0x1423b0
addq $0x60, %rsp
addq %rax, 0x70(%r14)
movq 0x10(%r15), %rax
movq %rax, -0x8(%rbx)
movaps (%r15), %xmm0
movups %xmm0, -0x18(%rbx)
movq 0x1083c1(%rip), %rsi    # 0x254548
movq 0x58(%rsp), %rbp
addq %rbp, %rsi
movq %r13, %rdi
callq 0x142c3b
movq 0x1083ca(%rip), %rsi    # 0x254568
addq %rbp, %rsi
movq %r13, %rdi
callq 0x142c3b
leaq 0x108080(%rip), %rbx    # 0x254230
movq 0x78(%rbx), %rdi
addq %rbp, %rdi
movq 0x10838a(%rip), %r8     # 0x254548
addq %rbp, %r8
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x14322b
movq 0x80(%rbx), %rdi
addq %rbp, %rdi
pushq $0x2
popq %rdx
movq %r12, %rsi
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x14322b
movq 0x108357(%rip), %rdi    # 0x254548
addq %rbp, %rdi
movq 0x78(%rbx), %rax
leaq (%rax,%rbp), %r10
leaq (%rax,%rbp), %rdx
addq $0x10, %rdx
leaq (%rax,%rbp), %rcx
addq $0x11, %rcx
movl 0x60(%rsp), %r8d
subq $0x8, %rsp
pushq $0x6
popq %r9
movq 0x20(%rsp), %rsi
pushq %r10
callq 0x14387a
popq %rax
popq %rcx
movq 0x28(%rbx), %rax
movq 0x30(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x80, %esi
movq 0x8(%rsp), %rdi
callq 0x14f9cd
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq