// File: chhylp123[P]hifiasm/inter.cpp
// Function: worker_ul_scall_pipeline(void*, int, void*)
static void *worker_ul_scall_pipeline(void *data, int step, void *in) // callback for kt_pipeline()
{
    uldat_t *p = (uldat_t*)data;
    ///uint64_t total_base = 0, total_pair = 0;
    if (step == 0) { // step 1: read a block of sequences
        int ret;
        uint64_t l;
		utepdat_t *s;
		CALLOC(s, 1);
        s->ha_flt_tab = p->ha_flt_tab; s->ha_idx = p->ha_idx; s->id = p->total_pair; 
		s->opt = p->opt; s->uu = p->uu; s->uopt = p->uopt; s->rg = p->rg;
        while ((ret = kseq_read(p->ks)) >= 0) 
        {
            if ((p->ks->seq.l < (uint64_t)p->opt->k) || (p->ks->seq.l < asm_opt.ul_min_base)) continue;
            if (s->n == s->m) {
                s->m = s->m < 16? 16 : s->m + (s->n>>1);
                REALLOC(s->len, s->m);
                REALLOC(s->seq, s->m);
            }
			
			append_ul_t(&UL_INF, NULL, p->ks->name.s, p->ks->name.l, NULL, 0, NULL, 0, P_CHAIN_COV, s->uopt, 0);		
            l = p->ks->seq.l;
            MALLOC(s->seq[s->n], l);
            s->sum_len += l;
            memcpy(s->seq[s->n], p->ks->seq.s, l);
			// fprintf(stderr, "s->n->%d, l->%lu\n", s->n, l);
            s->len[s->n++] = l;
            if (s->sum_len >= p->chunk_size) break;            
        }
        p->total_pair += s->n;
        if (s->sum_len == 0) free(s);
		else return s;
    }
    else if (step == 1) { // step 2: alignment
        utepdat_t *s = (utepdat_t*)in;

		uint64_t i;
		CALLOC(s->hab, p->n_thread); CALLOC(s->ll, p->n_thread); 
		CALLOC(s->sps, p->n_thread); 
		// CALLOC(s->buf, p->n_thread);
		for (i = 0; i < p->n_thread; ++i) {
			// s->buf[i] = mg_tbuf_init();
			// s->hab[i] = ha_ovec_buf_init(s->buf[i]->km, 0, 0, 1);
			// s->buf[i] = NULL;
			// s->hab[i] = ha_ovec_buf_init(NULL, 0, 0, 1);
			s->hab[i] = ha_ovec_init(0, 0, 1);
		}
		// fprintf(stderr, "[M::%s::Start] ==> s->id: %lu, s->n:% d\n", __func__, s->id, s->n);
		kt_for(p->n_thread, worker_for_ul_scall_alignment, s, s->n);
		// fprintf(stderr, "[M::%s::Done] ==> s->id: %lu, s->n:% d\n", __func__, s->id, s->n);
		///debug
		/**
		uint64_t i;
        CALLOC(s->mzs, p->n_thread);
        CALLOC(s->sps, p->n_thread);
		CALLOC(s->gcs, s->n);

		s->buf = (mg_tbuf_t**)calloc(p->n_thread, sizeof(mg_tbuf_t*));
		for (i = 0; i < p->n_thread; ++i) s->buf[i] = mg_tbuf_init();
        
        kt_for(p->n_thread, worker_for_ul_alignment, s, s->n);
        for (i = 0; i < (uint64_t)s->n; ++i) {
            free(s->seq[i]);
            p->total_base += s->len[i];
        }
        free(s->seq); free(s->len); 
		
		for (i = 0; i < p->n_thread; ++i) {
			mg_tbuf_destroy(s->buf[i]);
			free(s->mzs[i].a); free(s->sps[i].a);
		}
		**/
		for (i = 0; i < p->n_thread; ++i) {
			s->num_bases += s->hab[i]->num_read_base;
			s->num_corrected_bases += s->hab[i]->num_correct_base;
			s->num_recorrected_bases += s->hab[i]->num_recorrect_base;
			// mg_tbuf_destroy(s->buf[i]);
			ha_ovec_destroy(s->hab[i]); kv_destroy(s->sps[i]);
			free(s->ll[i].lo.a); /**free(s->ll[i].tk.a);**/ free(s->ll[i].srt.a.a); free(s->ll[i].tc.a);
		}
		free(s->hab); free(s->sps); 
		/**free(s->ll);**/ // free(s->buf);
		//free(s->mzs); free(s->sps);
		return s;
    }
    else if (step == 2) { // step 3: dump
        utepdat_t *s = (utepdat_t*)in;
		uint64_t i, rid;
		p->num_bases += s->num_bases;
		p->num_corrected_bases += s->num_corrected_bases;
		p->num_recorrected_bases += s->num_recorrected_bases;
		// fprintf(stderr, "[M::%s::dump_start] ==> s->id: %lu, s->n:% d\n", __func__, s->id, s->n);
		for (i = 0; i < p->n_thread; ++i) {
			push_uc_block_t(s->uopt, &(s->ll[i].tk), s->seq, s->len, s->id);
			free(s->ll[i].tk.a);
		}
		for (i = 0; i < (uint64_t)s->n; ++i) {
			rid = s->id + i; 
			if((UL_INF.n <= rid) || (UL_INF.n > rid && UL_INF.a[rid].rlen != s->len[i])) {
				append_ul_t(&UL_INF, &rid, NULL, 0, s->seq[i], s->len[i], NULL, 0, P_CHAIN_COV, s->uopt, 0);
			} 
			free(s->seq[i]); 
		}
		// fprintf(stderr, "[M::%s::dump_done] ==> s->id: %lu, s->n:% d\n", __func__, s->id, s->n);
		/**
        for (i = 0; i < (uint64_t)s->n; ++i) {
			///debug
			
            // if(s->pos[i].s == (uint64_t)-1) continue;
            // kv_push(pe_hit, p->hits.a, s->pos[i]);
			// if(!s->gcs[i]) continue;
			// dump_gaf(&(p->hits), s->gcs[i], 1);
			// free(s->gcs[i]->gc); free(s->gcs[i]->a); free(s->gcs[i]->lc); free(s->gcs[i]);
			
			rid = s->id + i;
			append_ul_t(&UL_INF, &rid, NULL, 0, s->seq[i], s->len[i], NULL, 0, P_CHAIN_COV);	
			// fprintf(stderr, "%.*s\n", (int)s->len[i], s->seq[i]);	
			free(s->seq[i]); p->total_base += s->len[i];
        }
		**/
		///debug
		/**
        free(s->gcs);
		**/
		
        free(s->ll); free(s->len); free(s->seq); free(s);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x2f2dcd
movl $0x1, %edi
movl $0xd0, %esi
callq 0x2140
movq %rax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2cf590
movl %eax, -0x2c(%rbp)
cmpl $0x0, %eax
jl 0x2f2d92
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x30(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x10(%rcx), %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jb 0x2f2bfd
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x30(%rax), %rax
leaq 0xd1c06(%rip), %rcx     # 0x3c47f8
movl 0x260(%rcx), %ecx
cmpq %rcx, %rax
jae 0x2f2bff
jmp 0x2f2ba9
movq -0x40(%rbp), %rax
movl 0x38(%rax), %eax
movq -0x40(%rbp), %rcx
cmpl 0x3c(%rcx), %eax
jne 0x2f2c8a
movq -0x40(%rbp), %rax
cmpl $0x10, 0x3c(%rax)
jge 0x2f2c23
movl $0x10, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2f2c38
movq -0x40(%rbp), %rax
movl 0x3c(%rax), %eax
movq -0x40(%rbp), %rcx
movl 0x38(%rcx), %ecx
sarl %ecx
addl %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x40(%rbp), %rax
movslq 0x3c(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x40(%rbp), %rax
movslq 0x3c(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x10(%rax), %rdx
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq (%rax), %rcx
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
leaq 0xd2708(%rip), %rdi     # 0x3c53b8
xorl %esi, %esi
movl %esi, %r9d
movss 0x9dac3(%rip), %xmm0    # 0x390780
xorl %esi, %esi
movq %r9, %rsi
movq %r9, %r8
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0xeb50
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x30(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rdx
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x40(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movslq 0x40(%rax), %rcx
addq %rdx, %rcx
movl %ecx, 0x40(%rax)
movq -0x40(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x40(%rbp), %rcx
movslq 0x38(%rcx), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x40(%rax), %rsi
movq -0x38(%rbp), %rdx
callq 0x2180
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x40(%rbp), %rsi
movl 0x38(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x38(%rsi)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x40(%rbp), %rax
movslq 0x40(%rax), %rax
movq -0x28(%rbp), %rcx
cmpq 0x50(%rcx), %rax
jl 0x2f2d8d
jmp 0x2f2d92
jmp 0x2f2ba9
movq -0x40(%rbp), %rax
movslq 0x38(%rax), %rcx
movq -0x28(%rbp), %rax
addq 0x68(%rax), %rcx
movq %rcx, 0x68(%rax)
movq -0x40(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x2f2dbb
movq -0x40(%rbp), %rdi
callq 0x2210
jmp 0x2f2dc8
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2f322d
jmp 0x2f3225
cmpl $0x1, -0x14(%rbp)
jne 0x2f3003
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movl $0x8, %esi
callq 0x2140
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movl $0x68, %esi
callq 0x2140
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movl $0x18, %esi
callq 0x2140
movq %rax, %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x68(%rax)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jae 0x2f2e84
xorl %esi, %esi
movl $0x1, %edx
movl %esi, %edi
callq 0x14c90
movq %rax, %rdx
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x50(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2f2e44
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movl %eax, %edi
movq -0x48(%rbp), %rdx
movq -0x48(%rbp), %rax
movslq 0x38(%rax), %rcx
leaq 0x1802f(%rip), %rsi     # 0x30aed0
callq 0x16d710
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jae 0x2f2fd9
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0xf0(%rax), %rcx
movq -0x48(%rbp), %rax
addq 0xa8(%rax), %rcx
movq %rcx, 0xa8(%rax)
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0xf8(%rax), %rcx
movq -0x48(%rbp), %rax
addq 0xb0(%rax), %rcx
movq %rcx, 0xb0(%rax)
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq 0x100(%rax), %rcx
movq -0x48(%rbp), %rax
addq 0xb8(%rax), %rcx
movq %rcx, 0xb8(%rax)
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x14f00
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rax
imulq $0x18, -0x50(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
movq 0x88(%rax), %rax
imulq $0x68, -0x50(%rbp), %rcx
addq %rcx, %rax
movq (%rax), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
movq 0x88(%rax), %rax
imulq $0x68, -0x50(%rbp), %rcx
addq %rcx, %rax
movq 0x58(%rax), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
movq 0x88(%rax), %rax
imulq $0x68, -0x50(%rbp), %rcx
addq %rcx, %rax
movq 0x30(%rax), %rdi
callq 0x2210
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2f2eae
movq -0x48(%rbp), %rax
movq 0x80(%rax), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0x2210
movq -0x48(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x2f322d
cmpl $0x2, -0x14(%rbp)
jne 0x2f3221
movq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq 0xa8(%rax), %rcx
movq -0x28(%rbp), %rax
addq 0xd0(%rax), %rcx
movq %rcx, 0xd0(%rax)
movq -0x58(%rbp), %rax
movq 0xb0(%rax), %rcx
movq -0x28(%rbp), %rax
addq 0xd8(%rax), %rcx
movq %rcx, 0xd8(%rax)
movq -0x58(%rbp), %rax
movq 0xb8(%rax), %rcx
movq -0x28(%rbp), %rax
addq 0xe0(%rax), %rcx
movq %rcx, 0xe0(%rax)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq 0x58(%rcx), %rax
jae 0x2f30e8
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x58(%rbp), %rax
movq 0x88(%rax), %rsi
imulq $0x68, -0x60(%rbp), %rax
addq %rax, %rsi
addq $0x18, %rsi
movq -0x58(%rbp), %rax
movq 0x58(%rax), %rdx
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x58(%rbp), %rax
movq 0x50(%rax), %r8
callq 0x2ceec0
movq -0x58(%rbp), %rax
movq 0x88(%rax), %rax
imulq $0x68, -0x60(%rbp), %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x2210
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2f3074
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movslq 0x38(%rcx), %rcx
cmpq %rcx, %rax
jae 0x2f31ee
movq -0x58(%rbp), %rax
movq 0x50(%rax), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq 0xd229c(%rip), %rax     # 0x3c53b8
movq 0x50(%rax), %rax
cmpq -0x68(%rbp), %rax
jbe 0x2f3163
leaq 0xd228b(%rip), %rax     # 0x3c53b8
movq 0x50(%rax), %rax
cmpq -0x68(%rbp), %rax
jbe 0x2f31c8
leaq 0xd2276(%rip), %rax     # 0x3c53b8
movq 0x48(%rax), %rax
imulq $0x58, -0x68(%rbp), %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movq -0x58(%rbp), %rcx
movq 0x48(%rcx), %rcx
movq -0x60(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
je 0x2f31c8
movq -0x58(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %r8
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %r9
movq -0x58(%rbp), %rax
movq 0x28(%rax), %rax
leaq 0xd2226(%rip), %rdi     # 0x3c53b8
leaq -0x68(%rbp), %rsi
xorl %ecx, %ecx
movss 0x9d5e0(%rip), %xmm0    # 0x390780
xorl %edx, %edx
movq %rcx, %rdx
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0xeb50
movq -0x58(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2210
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x2f30f0
movq -0x58(%rbp), %rax
movq 0x88(%rax), %rdi
callq 0x2210
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x2210
movq -0x58(%rbp), %rax
movq 0x58(%rax), %rdi
callq 0x2210
movq -0x58(%rbp), %rdi
callq 0x2210
jmp 0x2f3223
jmp 0x2f3225
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)