// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rdi, %r14
cmpl $0x2, %esi
je 0x1e1d05
cmpl $0x1, %esi
je 0x1e1c92
testl %esi, %esi
jne 0x1e1e43
movl $0x1, %edi
movl $0xd0, %esi
callq 0x2120
movq %rax, %rbx
movdqu (%r14), %xmm0
movdqu %xmm0, 0x8(%rax)
movq 0x68(%r14), %rax
movq %rax, 0x50(%rbx)
movq 0x10(%r14), %rax
movq %rax, (%rbx)
movq 0x30(%r14), %rax
movq %rax, 0x30(%rbx)
movq 0x28(%r14), %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x28(%rbx)
movq 0x20(%r14), %rax
movq %rax, 0x20(%rbx)
movq 0x48(%r14), %rdi
callq 0x1ce059
testl %eax, %eax
js 0x1e1e3b
xorl %r12d, %r12d
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq 0x10(%r14), %rcx
movq 0x48(%r14), %rbp
movq 0x30(%rbp), %rax
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jb 0x1e1c6b
leaq 0xb3c0c(%rip), %rcx     # 0x295758
movl 0x260(%rcx), %ecx
cmpq %rcx, %rax
jb 0x1e1c6b
cmpl %r13d, 0x28(%rsp)
jne 0x1e1bb2
movl %r13d, %r15d
sarl %r15d
addl %r13d, %r15d
cmpl $0x10, %r13d
movl $0x10, %eax
cmovll %eax, %r15d
movl %r15d, 0x3c(%rbx)
movslq %r15d, %r12
shlq $0x3, %r12
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x2370
movq %rax, 0x48(%rbx)
movq 0x58(%rbx), %rdi
movq %r12, %rsi
callq 0x2370
movq %rax, %r12
movq %rax, 0x58(%rbx)
movq 0x48(%r14), %rbp
movl %r15d, 0x2c(%rsp)
jmp 0x1e1bbc
movl %r13d, 0x2c(%rsp)
movq 0x38(%rsp), %r12
movq (%rbp), %rcx
movq 0x10(%rbp), %rdx
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, (%rsp)
movl $0x0, 0x18(%rsp)
leaq 0xb4732(%rip), %rdi     # 0x296318
xorl %esi, %esi
xorl %r8d, %r8d
xorl %r9d, %r9d
movd 0x5dfc2(%rip), %xmm0    # 0x23fbb8
callq 0x3535a
movq 0x48(%r14), %rax
movq 0x30(%rax), %r15
movq %r15, %rdi
callq 0x2280
movl 0x28(%rsp), %r13d
movslq %r13d, %r14
movq %r12, 0x38(%rsp)
movq %rax, (%r12,%r14,8)
movl 0x40(%rbx), %r12d
addl %r15d, %r12d
movl %r12d, 0x40(%rbx)
movq 0x40(%rsp), %rcx
movq 0x48(%rcx), %rbp
movq 0x40(%rbp), %rsi
movq %rax, %rdi
movq %r15, %rdx
callq 0x2160
movq 0x48(%rbx), %rcx
incl %r13d
movl %r13d, 0x28(%rsp)
movl %r13d, 0x38(%rbx)
movq %rcx, 0x30(%rsp)
movq %r15, (%rcx,%r14,8)
movq 0x40(%rsp), %r14
movslq %r12d, %rax
cmpq %rax, 0x50(%r14)
movl 0x2c(%rsp), %r13d
jle 0x1e1c7b
movq %rbp, %rdi
callq 0x1ce059
testl %eax, %eax
jns 0x1e1b2c
movslq 0x28(%rsp), %rax
addq %rax, 0x68(%r14)
testl %r12d, %r12d
jne 0x1e1f15
jmp 0x1e1e3b
movq 0x58(%r14), %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x80(%rbx)
movl $0x68, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x88(%rbx)
movl $0x18, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x68(%rbx)
testq %r15, %r15
je 0x1e1e4a
xorl %r15d, %r15d
xorl %edi, %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x387f4
movq 0x80(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movq 0x58(%r14), %rdi
cmpq %rdi, %r15
jb 0x1e1cdb
jmp 0x1e1e4c
movdqu 0xa8(%rbx), %xmm0
movdqu 0xd0(%r14), %xmm1
paddq %xmm0, %xmm1
movdqu %xmm1, 0xd0(%r14)
movq 0xb8(%rbx), %rax
addq %rax, 0xe0(%r14)
cmpq $0x0, 0x58(%r14)
je 0x1e1d7d
movl $0x18, %r15d
xorl %r12d, %r12d
movq 0x88(%rbx), %rsi
addq %r15, %rsi
movq 0x58(%rbx), %rdx
movq 0x28(%rbx), %rdi
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %r8
callq 0x1cdbd6
movq 0x88(%rbx), %rax
movq (%rax,%r15), %rdi
callq 0x2220
incq %r12
addq $0x68, %r15
cmpq 0x58(%r14), %r12
jb 0x1e1d41
cmpl $0x0, 0x38(%rbx)
je 0x1e1e1d
xorl %r12d, %r12d
leaq 0xb4587(%rip), %r14     # 0x296318
leaq 0x50(%rsp), %r15
movq 0x50(%rbx), %rax
addq %r12, %rax
movq %rax, 0x50(%rsp)
movq 0x48(%rbx), %rcx
cmpq %rax, 0x50(%r14)
jbe 0x1e1dc3
movq 0x48(%r14), %rdx
imulq $0x58, %rax, %rax
movl 0x18(%rdx,%rax), %eax
movq (%rcx,%r12,8), %r9
cmpq %rax, %r9
jne 0x1e1dc7
jmp 0x1e1e00
movq (%rcx,%r12,8), %r9
movq 0x28(%rbx), %rax
movq 0x58(%rbx), %rcx
movq (%rcx,%r12,8), %r8
movq %rax, 0x10(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, (%rsp)
movl $0x0, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movd 0x5ddbd(%rip), %xmm0    # 0x23fbb8
callq 0x3535a
movq 0x58(%rbx), %rax
movq (%rax,%r12,8), %rdi
callq 0x2220
incq %r12
movslq 0x38(%rbx), %rax
cmpq %rax, %r12
jb 0x1e1d96
movq 0x88(%rbx), %rdi
callq 0x2220
movq 0x48(%rbx), %rdi
callq 0x2220
movq 0x58(%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
xorl %ebx, %ebx
jmp 0x1e1f15
xorl %edi, %edi
movslq 0x38(%rbx), %rcx
leaq 0xb02f(%rip), %rsi      # 0x1ece86
movq %rbx, %rdx
callq 0x108f2c
cmpq $0x0, 0x58(%r14)
je 0x1e1f00
movl $0x2, %r15d
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rdi
movdqu 0xa8(%rbx), %xmm0
paddq 0xf0(%rdi), %xmm0
movdqu %xmm0, 0xa8(%rbx)
movq 0x100(%rdi), %rax
addq %rax, 0xb8(%rbx)
callq 0x38b4b
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x2220
movq 0x88(%rbx), %rax
movq (%rax,%r12), %rdi
callq 0x2220
movq 0x88(%rbx), %rax
movq 0x58(%rax,%r12), %rdi
callq 0x2220
movq 0x88(%rbx), %rax
movq 0x30(%rax,%r12), %rdi
callq 0x2220
incq %r13
addq $0x68, %r12
addq $0x3, %r15
cmpq 0x58(%r14), %r13
jb 0x1e1e76
movq 0x80(%rbx), %rdi
callq 0x2220
movq 0x68(%rbx), %rdi
callq 0x2220
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq