// File: chhylp123[P]hifiasm/inter.cpp
// Function: worker_for_ul_recorrect_alignment(void*, long, int)
static void worker_for_ul_recorrect_alignment(void *data, long i, int tid) // callback for kt_for()
{
    utepdat_t *s = (utepdat_t*)data;
    ha_ovec_buf_t *b = s->hab[tid];
    glchain_t *bl = &(s->ll[tid]);
    int64_t /**rid = s->id+i,**/ winLen = MIN((((double)THRESHOLD_MAX_SIZE)/s->opt->diff_ec_ul), WINDOW), is_correct;
    // uint64_t align = 0;
    int fully_cov, abnormal;
	// if(UL_INF.a[s->id+i].rlen != s->len[i]) {
	// 	fprintf(stderr, "[M::%s] rid:%ld, s->len:%lu, UL_INF->rlen:%u\n", __func__, s->id+i, s->len[i], UL_INF.a[s->id+i].rlen);
	// }
	is_correct = ck_ul_alignment(&(UL_INF.a[s->id+i]));
	if(is_correct) {
		assert((UL_INF.a[s->id+i].rlen == s->len[i]) && (!s->seq[i]));
		return;
	}
	assert(UL_INF.a[s->id+i].rlen&((uint32_t)(0x80000000)));
	UL_INF.a[s->id+i].rlen<<=1; UL_INF.a[s->id+i].rlen>>=1;
	assert((UL_INF.a[s->id+i].rlen == s->len[i]));
	
    // void *km = s->buf?(s->buf[tid]?s->buf[tid]->km:NULL):NULL;
    // if(s->id+i!=41927 && s->id+i!=47072 && s->id+i!=67641 && s->id+i!=90305 && s->id+i!=698342 && s->id+i!=329421) {
	// 	return;
	// }
	// if(s->id+i!=41927) return;

    // fprintf(stderr, "\n[M::%s] rid:%ld, len:%lu\n", __func__, s->id+i, s->len[i]);
    // if (memcmp(UL_INF.nid.a[s->id+i].a, "d0aab024-b3a7-40fb-83cc-22c3d6d951f8", UL_INF.nid.a[s->id+i].n-1)) return;
    // fprintf(stderr, "[M::%s::] ==> len: %lu\n", __func__, s->len[i]);
    ha_get_ul_candidates_interface(b->abl, i, s->seq[i], s->len[i], s->opt->w, s->opt->k, s->uu, &b->olist, &b->olist_hp, &b->clist, s->opt->bw_thres, 
        s->opt->max_n_chain, 1, NULL/**&(b->k_flag)**/, &b->r_buf, &(b->tmp_region), NULL, &(b->sp), 1, NULL);
    
    clear_Cigar_record(&b->cigar1);
    clear_Round2_alignment(&b->round2);
    // return;
    // b->num_correct_base += overlap_statistics(&b->olist, NULL, 0);   

    b->self_read.seq = s->seq[i]; b->self_read.length = s->len[i]; b->self_read.size = 0;
    correct_ul_overlap(&b->olist, s->uu, &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, s->opt->diff_ec_ul, winLen, NULL);

    // uint64_t k;
    // for (k = 0; k < b->olist.length; k++) {
    //  if(b->olist.list[k].is_match == 1) b->num_correct_base += b->olist.list[k].x_pos_e+1-b->olist.list[k].x_pos_s;
    //  if(b->olist.list[k].is_match == 2) b->num_recorrect_base += b->olist.list[k].x_pos_e+1-b->olist.list[k].x_pos_s;
    // }


    // gl_chain_refine(&b->olist, &b->correct, &b->hap, bl, s->uu, s->opt->diff_ec_ul, winLen, s->len[i], km);
    gl_chain_refine_advance_combine(s->buf[tid], &(UL_INF.a[s->id+i]), &b->olist, &b->correct, &b->hap, &(s->sps[tid]), bl, &(s->gdp[tid]), s->uu, s->opt->diff_ec_ul, winLen, s->len[i], s->uopt, s->id+i, tid, NULL);
    // return;
    // 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;
    memset(&b->self_read, 0, sizeof(b->self_read));



	is_correct = ck_ul_alignment(&(UL_INF.a[s->id+i]));
	if(is_correct) b->num_correct_base++;
    s->hab[tid]->num_read_base++;

	// fprintf(stderr, "[M::%s] rid:%ld, dd:%u\n", __func__, s->id+i, UL_INF.a[s->id+i].dd);
    // int64_t mem[6], mem_hab[6];
    // if(get_utepdat_t_mem_tid(s, tid, mem, mem_hab)>((int64_t)5*(int64_t)1073741824)) {
    //     fprintf(stderr, "[M::%s::tid->%d::rid->%ld] buffer[0]: %.3fGB(%.3fGB::%.3fGB::%.3fGB::%.3fGB::%.3fGB), buffer[1]: %.3fGB, buffer[2]: %.3fGB, buffer[3]: %.3fGB, buffer[4]: %.3fGB, buffer[5]: %.3fGB\n", 
    //             __func__, tid, i, mem[0]/1073741824.0, 
	// 			mem_hab[0]/1073741824.0, mem_hab[1]/1073741824.0, mem_hab[2]/1073741824.0, 
	// 			mem_hab[3]/1073741824.0, mem_hab[4]/1073741824.0,
	// 			mem[1]/1073741824.0, mem[2]/1073741824.0,
    //             mem[3]/1073741824.0, mem[4]/1073741824.0, mem[5]/1073741824.0);
    // }

    // align = kv_ul_ov_t_statistics(&(bl->tk), i, &(b->num_recorrect_base));
    // if(align == s->len[i]) {
    //  free(s->seq[i]); s->seq[i] = NULL;
    // }
    // b->num_correct_base += align;

    // uint64_t k;
    // b->num_read_base += overlap_statistics(&b->olist, NULL, NULL, 1);
    // for (k = 0; k < bl->tk.n; k++) {
    //  if(bl->tk.a[k].sec == 0) b->num_correct_base += bl->tk.a[k].qe - bl->tk.a[k].qs;
    //  if(bl->tk.a[k].sec > 0) b->num_recorrect_base += bl->tk.a[k].qe - bl->tk.a[k].qs;
    // }
    // for (k = 0; k < bl->lo.n; k++) {
    //  b->num_read_base += bl->lo.a[k].qe - bl->lo.a[k].qs;
    // }

    // uint32_t l1 = overlap_statistics(&b->olist, s->uu->ug, 1), l2 = overlap_statistics(&b->olist, s->uu->ug, 2);
    // 
    // if(l1 == 0 && l2 > 0) fprintf(stderr, "[M::%s::%lu::no_match]\n", UL_INF.nid.a[s->id+i].a, s->len[i]);
    // fprintf(stderr, "[M::%s::%lu::] l1->%u; l2->%u\n", UL_INF.nid.a[s->id+i].a, s->len[i], l1, l2);
    // fprintf(stderr, "[M::%s::rid->%ld] done\n", __func__, s->id+i);
	// exit(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movl %edx, -0x3c(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq 0x88(%rax), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsd 0x75dc1(%rip), %xmm1    # 0x3822a8
divsd 0x68(%rax), %xmm1
movsd 0x75dac(%rip), %xmm0    # 0x3822a0
ucomisd %xmm1, %xmm0
jb 0x30c515
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsd 0x75d9f(%rip), %xmm0    # 0x3822a8
divsd 0x68(%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
jmp 0x30c524
movsd 0x75d83(%rip), %xmm0    # 0x3822a0
movsd %xmm0, -0x78(%rbp)
jmp 0x30c524
movsd -0x78(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x60(%rbp)
leaq 0xb8e7f(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
addq -0x38(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rdi
callq 0x2bb5e0
movl %eax, %eax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x30c5e8
leaq 0xb8e4b(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x38(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, %ecx
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rdx
movq -0x38(%rbp), %rsi
xorl %eax, %eax
cmpq (%rdx,%rsi,8), %rcx
movb %al, -0x79(%rbp)
jne 0x30c5b9
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
setne %al
xorb $-0x1, %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x30c5c2
jmp 0x30c5c4
jmp 0x30c5e3
leaq 0x896e1(%rip), %rdi     # 0x395cac
leaq 0x8425d(%rip), %rsi     # 0x39082f
movl $0x315d, %edx           # imm = 0x315D
leaq 0x89704(%rip), %rcx     # 0x395ce2
callq 0x2130
jmp 0x30cabc
leaq 0xb8dc9(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x38(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
andl $0x80000000, %eax       # imm = 0x80000000
cmpl $0x0, %eax
je 0x30c615
jmp 0x30c634
leaq 0x89700(%rip), %rdi     # 0x395d1c
leaq 0x8420c(%rip), %rsi     # 0x39082f
movl $0x3160, %edx           # imm = 0x3160
leaq 0x896b3(%rip), %rcx     # 0x395ce2
callq 0x2130
leaq 0xb8d7d(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x38(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
shll %ecx
movl %ecx, 0x18(%rax)
leaq 0xb8d57(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x38(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %ecx
shrl %ecx
movl %ecx, 0x18(%rax)
leaq 0xb8d31(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
movq -0x48(%rbp), %rcx
movq 0x50(%rcx), %rcx
addq -0x38(%rbp), %rcx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movq -0x48(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq -0x38(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x30c6b5
jmp 0x30c6d4
leaq 0x89690(%rip), %rdi     # 0x395d4c
leaq 0x8416c(%rip), %rsi     # 0x39082f
movl $0x3162, %edx           # imm = 0x3162
leaq 0x89613(%rip), %rcx     # 0x395ce2
callq 0x2130
movq -0x50(%rbp), %rax
movq 0xe8(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x48(%rbp), %rax
movq (%rax), %rax
movslq (%rax), %r8
movq -0x48(%rbp), %rax
movq (%rax), %rax
movslq 0x4(%rax), %r9
movq -0x48(%rbp), %rax
movq 0x30(%rax), %r13
movq -0x50(%rbp), %r12
addq $0xa0, %r12
movq -0x50(%rbp), %r15
addq $0xc0, %r15
movq -0x50(%rbp), %r14
addq $0x48, %r14
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsd 0x60(%rax), %xmm0
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl 0x80(%rax), %ebx
movq -0x50(%rbp), %r11
addq $0x12160, %r11          # imm = 0x12160
movq -0x50(%rbp), %r10
addq $0x121a0, %r10          # imm = 0x121A0
movq -0x50(%rbp), %rax
addq $0x12250, %rax          # imm = 0x12250
movq %rax, -0xa0(%rbp)
xorl %eax, %eax
movq -0xa0(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movl %ebx, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq %r11, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movl $0x1, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
callq 0x1a3ac0
movq -0x50(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
callq 0x31d60
movq -0x50(%rbp), %rdi
addq $0x9330, %rdi           # imm = 0x9330
callq 0x31cb0
movq -0x48(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x50(%rbp), %rdi
addq $0xa0, %rdi
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x50(%rbp), %rdx
addq $0x8, %rdx
movq -0x50(%rbp), %rcx
addq $0x2c0, %rcx            # imm = 0x2C0
movq -0x50(%rbp), %r8
addq $0x28, %r8
movq -0x50(%rbp), %r9
addq $0x148, %r9             # imm = 0x148
movq -0x50(%rbp), %rax
addq $0x200, %rax            # imm = 0x200
movq %rax, -0x98(%rbp)
movq -0x50(%rbp), %r13
addq $0x108, %r13            # imm = 0x108
movq -0x50(%rbp), %r12
addq $0x9040, %r12           # imm = 0x9040
movq -0x50(%rbp), %r15
addq $0x9330, %r15           # imm = 0x9330
movq -0x50(%rbp), %r14
addq $0x12160, %r14          # imm = 0x12160
movq -0x50(%rbp), %rbx
addq $0x121a0, %rbx          # imm = 0x121A0
addq $0x48, %rbx
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsd 0x68(%rax), %xmm0
movq -0x60(%rbp), %rax
movq %rax, -0x90(%rbp)
xorl %eax, %eax
leaq -0x6c(%rbp), %r11
leaq -0x70(%rbp), %r10
xorl %eax, %eax
movq -0x98(%rbp), %rax
movq %rax, (%rsp)
movq -0x90(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x1, 0x38(%rsp)
movq %r11, 0x40(%rsp)
movq %r10, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
callq 0x4c660
movq -0x48(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0xb8a68(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rsi
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
addq -0x38(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rsi
movq -0x50(%rbp), %rdx
addq $0xa0, %rdx
movq -0x50(%rbp), %rcx
addq $0x2c0, %rcx            # imm = 0x2C0
movq -0x50(%rbp), %r8
addq $0x9040, %r8            # imm = 0x9040
movq -0x48(%rbp), %rax
movq 0x68(%rax), %r9
movslq -0x3c(%rbp), %rax
imulq $0x18, %rax, %rax
addq %rax, %r9
movq -0x58(%rbp), %r13
movq -0x48(%rbp), %rax
movq 0x90(%rax), %r12
movslq -0x3c(%rbp), %rax
imulq $0xc0, %rax, %rax
addq %rax, %r12
movq -0x48(%rbp), %rax
movq 0x30(%rax), %r15
movq -0x48(%rbp), %rax
movq (%rax), %rax
movsd 0x68(%rax), %xmm0
movq -0x60(%rbp), %r14
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x38(%rbp), %r10
movq (%rax,%r10,8), %rbx
movq -0x48(%rbp), %rax
movq 0x28(%rax), %r11
movq -0x48(%rbp), %rax
movq 0x50(%rax), %r10
addq -0x38(%rbp), %r10
movslq -0x3c(%rbp), %rax
movq %rax, -0x88(%rbp)
xorl %eax, %eax
movq -0x88(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
callq 0x2a08d0
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2100
leaq 0xb8960(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rax
addq -0x38(%rbp), %rax
imulq $0x58, %rax, %rax
addq %rax, %rdi
callq 0x2bb5e0
movl %eax, %eax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x30ca97
movq -0x50(%rbp), %rax
movq 0xf8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xf8(%rax)
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0xf0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xf0(%rax)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop