// File: /workspace/github_cpp_makefile_star2_O1/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), %rbp
movslq %edx, %rax
imulq $0x5c0, %rax, %rbx      # imm = 0x5C0
leaq (%rbx,%rbp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x1006a0(%rip), %r13    # 0x28a758
cvtsi2sdl 0xd0(%r13), %xmm0
movsd 0xa48c7(%rip), %xmm1    # 0x22e990
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
leaq 0x24(%rsp), %rdx
movl %eax, (%rdx)
mulsd 0xa33ef(%rip), %xmm0    # 0x22d4d0
cvttsd2si %xmm0, %rax
leaq 0x6c(%rsp), %r12
movl %eax, (%r12)
leaq 0x10113a(%rip), %rsi    # 0x28b230
movq %r15, %rdx
callq 0x33cf8
leaq (%rbx,%rbp), %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 (%rbx,%rbp), %r11
addq $0x40, %r11
xorl %esi, %esi
xorl %eax, %eax
cmpq %rsi, 0x2a8(%r13)
sete %al
leaq 0xa7bc3(%rip), %rsi     # 0x231d10
movsd (%rsi,%rax,8), %xmm0
movl 0xe8(%r13), %eax
leaq (%rbx,%rbp), %rsi
addq $0x568, %rsi            # imm = 0x568
subq $0x80, %rsp
movl $0x3, %r10d
movq %r10, 0x60(%rsp)
movl $0x1, %r10d
movl %r10d, 0x58(%rsp)
movl %r10d, 0x50(%rsp)
movq %r12, 0x48(%rsp)
leaq 0xa4(%rsp), %r12
movq %r12, 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 0x101064(%rip), %rax    # 0x28b230
movq %rax, (%rsp)
movq $0xc00, 0x78(%rsp)      # imm = 0xC00
movl $0x20, 0x70(%rsp)
movl $0x2, 0x68(%rsp)
movsd 0x9b2af(%rip), %xmm1    # 0x2254a0
movq %r15, %r12
movl %r12d, %esi
callq 0x129c88
addq $0x80, %rsp
movq -0x38(%r13), %rax
movq %r14, %r13
addq %rax, 0x28(%r14)
movq %r14, %rdi
movq %r14, 0x38(%rsp)
callq 0x179530
movq %rax, 0x40(%rsp)
leaq (%rbx,%rbp), %r8
addq $0x20, %r8
movq %r8, 0x28(%rsp)
leaq 0x3e0(%rbp,%rbx), %r9
movq %r9, 0x50(%rsp)
leaq 0x100517(%rip), %rax    # 0x28a758
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 0x500(%rbp,%rbx), %r15
leaq 0x4d0(%rbp,%rbx), %rdx
movq %rdx, 0x18(%rsp)
leaq (,%r12,8), %rdx
movq %r12, 0x30(%rsp)
leaq (%rdx,%rdx,2), %rsi
movq %rsi, 0x58(%rsp)
leaq 0xa7a86(%rip), %rdx     # 0x231d20
movsd (%rdx,%r10,8), %xmm1
leaq 0x100f89(%rip), %rdx    # 0x28b230
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
pushq %r10
pushq %r14
pushq %r11
pushq 0x30(%rsp)
pushq %r15
pushq $0x1
pushq $0x1f
pushq %r12
pushq %rax
pushq 0x88(%rsp)
callq 0x180c3c
addq $0x50, %rsp
movq 0x78(%r15), %rax
leaq 0x70(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x68(%r15), %rax
movq %rax, (%rcx)
movq 0x70(%r15), %rax
movq %rax, 0x8(%rcx)
leaq 0xf8(%rbp,%rbx), %rdx
leaq 0x100444(%rip), %rcx    # 0x28a758
movq 0x2a8(%rcx), %rax
movq 0x2b0(%rcx), %r11
xorl %r12d, %r12d
testq %rax, %rax
setne %r12b
movl $0xc, %r10d
cmoveq %rax, %r10
leaq 0x58(%rbp,%rbx), %r13
movq %r13, 0x10(%rsp)
cmoveq %rax, %r13
testq %r11, %r11
leaq 0x518(%rbp,%rbx), %r14
cmoveq %r11, %r14
leaq 0x4b8(%rbp,%rbx), %r9
movq %r9, 0x48(%rsp)
leaq 0x530(%rbp,%rbx), %r15
cmovneq %r15, %r11
subq $0x8, %rsp
movq 0x40(%rsp), %rdi
leaq 0x100eb4(%rip), %rsi    # 0x28b230
movq 0x10(%rsp), %rcx
movq 0x30(%rsp), %r8
pushq %r12
pushq %r11
pushq %r14
pushq %r13
pushq %rax
pushq %r10
movq 0x68(%rsp), %r14
pushq %r14
movl $0x1, %eax
pushq %rax
pushq -0x18(%r8)
pushq $0x1ac                  # imm = 0x1AC
xorl %eax, %eax
pushq %rax
leaq 0xd0(%rsp), %r13
pushq %r13
movq 0x80(%rsp), %r12
pushq %r12
callq 0x72fd8
addq $0x70, %rsp
movq 0x10(%r13), %rax
movq %rax, 0x48(%r15)
movq (%r13), %rax
movq %rax, 0x38(%r15)
movq 0x8(%r13), %rax
movq %rax, 0x40(%r15)
movq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x181722
movq 0x48(%r15), %rax
leaq 0x70(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq 0x38(%r15), %rax
movq %rax, (%rcx)
movq 0x40(%r15), %rax
movq %rax, 0x8(%rcx)
movq %rcx, %r10
leaq (%rbx,%rbp), %rax
addq $0x4e8, %rax            # imm = 0x4E8
addq %rbp, %rbx
addq $0x580, %rbx            # imm = 0x580
subq $0x8, %rsp
movsd 0x9b135(%rip), %xmm0    # 0x225560
movq %r13, %rdi
leaq 0x100dfb(%rip), %rsi    # 0x28b230
movq 0x10(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x50(%rsp), %r9
pushq %r14
pushq $0x100                  # imm = 0x100
pushq %rbx
pushq %rax
movq 0x68(%rsp), %r15
pushq %r15
movl $0x3, %eax
pushq %rax
movq 0x48(%rsp), %r14
pushq -0x50(%r14)
pushq $0x200                  # imm = 0x200
xorl %eax, %eax
pushq %rax
movq %r10, %rbp
pushq %r10
pushq %r12
callq 0x17f25c
addq $0x60, %rsp
addq %rax, 0x70(%r14)
movq 0x10(%rbp), %rax
movq %rax, -0x8(%rbx)
movq (%rbp), %rax
movq %rax, -0x18(%rbx)
movq 0x8(%rbp), %rax
movq %rax, -0x10(%rbx)
movq 0x1010a5(%rip), %rsi    # 0x28b548
movq 0x58(%rsp), %r12
addq %r12, %rsi
movq %r15, %rdi
callq 0x17fb09
movq 0x1010ae(%rip), %rsi    # 0x28b568
addq %r12, %rsi
movq %r15, %rdi
callq 0x17fb09
leaq 0x100d64(%rip), %rbx    # 0x28b230
movq 0x78(%rbx), %rdi
addq %r12, %rdi
movq 0x10106e(%rip), %r8     # 0x28b548
addq %r12, %r8
movq %r13, %rsi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x18036a
movq 0x80(%rbx), %rdi
addq %r12, %rdi
movq %r13, %rsi
movl $0x2, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x18036a
movq 0x101037(%rip), %rdi    # 0x28b548
addq %r12, %rdi
movq 0x78(%rbx), %rax
leaq (%rax,%r12), %r10
leaq (%rax,%r12), %rdx
addq $0x10, %rdx
leaq (%rax,%r12), %rcx
addq $0x11, %rcx
movl 0x60(%rsp), %r8d
subq $0x8, %rsp
movl $0x6, %r9d
movq 0x20(%rsp), %rsi
pushq %r10
callq 0x180a22
addq $0x10, %rsp
movq 0x28(%rbx), %rax
movq 0x30(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl $0x80, %esi
movq 0x8(%rsp), %rdi
callq 0x18db6a
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq