// File: /workspace/github_c_makefile_star2_O2/lh3[P]minigraph/gmap.c
// Function: worker_pipeline
static void *worker_pipeline(void *shared, int step, void *in)
{
	int i, j, k;
    pipeline_t *p = (pipeline_t*)shared;
    if (step == 0) { // step 0: read sequences
		int with_qual = !(p->opt->flag & MG_M_NO_QUAL);
		int with_comment = !!(p->opt->flag & MG_M_COPY_COMMENT);
		int frag_mode = (p->n_fp > 1 || !!(p->opt->flag & MG_M_FRAG_MODE));
        step_t *s;
        s = (step_t*)calloc(1, sizeof(step_t));
		if (p->n_fp > 1) s->seq = mg_bseq_read_frag(p->n_fp, p->fp, p->mini_batch_size, with_qual, with_comment, &s->n_seq);
		else s->seq = mg_bseq_read(p->fp[0], p->mini_batch_size, with_qual, with_comment, frag_mode, &s->n_seq);
		if (s->seq) {
			s->p = p;
			for (i = 0; i < s->n_seq; ++i)
				mg_toupper(s->seq[i].l_seq, s->seq[i].seq);
			for (i = 0; i < s->n_seq; ++i)
				s->seq[i].rid = p->n_processed++;
			s->buf = (mg_tbuf_t**)calloc(p->n_threads, sizeof(mg_tbuf_t*));
			for (i = 0; i < p->n_threads; ++i)
				s->buf[i] = mg_tbuf_init();
			s->seg_off = (int*)calloc(2 * s->n_seq, sizeof(int));
			s->n_seg = s->seg_off + s->n_seq; // n_seg, rep_len and frag_gap are allocated together with seg_off
			KCALLOC(0, s->gcs, s->n_seq);
			for (i = 1, j = 0; i <= s->n_seq; ++i)
				if (i == s->n_seq || !frag_mode || !mg_qname_same(s->seq[i-1].name, s->seq[i].name)) {
					s->n_seg[s->n_frag] = i - j;
					s->seg_off[s->n_frag++] = j;
					j = i;
				}
			return s;
		} else free(s);
    } else if (step == 1) { // step 1: map
		kt_for(p->n_threads, worker_for, in, ((step_t*)in)->n_frag);
		return in;
    } else if (step == 2) { // step 2: output
		void *km = 0;
        step_t *s = (step_t*)in;
		for (i = 0; i < p->n_threads; ++i) mg_tbuf_destroy(s->buf[i]);
		free(s->buf);
		if (!(mg_dbg_flag & MG_DBG_NO_KALLOC)) km = km_init();
		for (k = 0; k < s->n_frag; ++k) {
			int seg_st = s->seg_off[k], seg_en = s->seg_off[k] + s->n_seg[k];
			if ((p->opt->flag & MG_M_FRAG_MODE) && (p->opt->flag & MG_M_FRAG_MERGE)) {
				mg_bseq1_t *t = &s->seq[seg_st];
				int32_t *qlens;
				KMALLOC(km, qlens, seg_en - seg_st); // TODO: if this is an issue (quite unlikely), preallocate
				for (i = seg_st; i < seg_en; ++i)
					qlens[i - seg_st] = s->seq[i].l_seq;
				if (p->opt->flag & MG_M_CAL_COV)
					mg_cov_map(p->gi->g, s->gcs[seg_st], p->opt->min_cov_mapq, p->opt->min_cov_blen, p->c_seg, p->c_link, t->name);
				else mg_write_gaf(&p->str, p->gi->g, s->gcs[seg_st], seg_en - seg_st, qlens, t->name, p->opt->flag, km);
				kfree(km, qlens);
				if (p->str.l) mg_err_fputs(p->str.s, stdout);
			} else {
				for (i = seg_st; i < seg_en; ++i) {
					mg_bseq1_t *t = &s->seq[i];
					if (p->opt->flag & MG_M_CAL_COV)
						mg_cov_map(p->gi->g, s->gcs[i], p->opt->min_cov_mapq, p->opt->min_cov_blen, p->c_seg, p->c_link, t->name);
					else mg_write_gaf(&p->str, p->gi->g, s->gcs[i], 1, &t->l_seq, t->name, p->opt->flag, km);
					if (p->str.l) mg_err_fputs(p->str.s, stdout);
				}
			}
			for (i = seg_st; i < seg_en; ++i) {
				mg_gchain_free(s->gcs[i]);
				free(s->seq[i].seq); free(s->seq[i].name);
				if (s->seq[i].qual) free(s->seq[i].qual);
				if (s->seq[i].comment) free(s->seq[i].comment);
			}
		}
		free(s->gcs); free(s->seg_off); free(s->seq); // n_seg, rep_len and frag_gap were allocated with seg_off; no memory leak here
		if (km) km_destroy(km);
		if (mg_verbose >= 3)
			fprintf(stderr, "[M::%s::%.3f*%.2f] mapped %d sequences\n", __func__, realtime() - mg_realtime0, cputime() / (realtime() - mg_realtime0), s->n_seq);
		free(s);
	}
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
testl %esi, %esi
je 0x1cfe2
movq %rdx, %rbx
cmpl $0x1, %esi
je 0x1cfc6
cmpl $0x2, %esi
jne 0x1d4a2
xorl %r15d, %r15d
movslq 0xc(%r14), %rax
movq 0x30(%rbx), %rdi
cmpq %rax, %r15
jge 0x1d03f
movq (%rdi,%r15,8), %rdi
callq 0x19598
incq %r15
jmp 0x1cfa7
movl 0xc(%r14), %edi
movslq 0xc(%rbx), %rcx
leaq 0x692(%rip), %rsi       # 0x1d667
movq %rbx, %rdx
callq 0x2968
jmp 0x1d4a4
movq 0x18(%r14), %rax
movq (%rax), %r15
xorl %r13d, %r13d
btl $0x12, %r15d
setae %r13b
movl %r15d, %r12d
shrl $0x10, %r12d
andl $0x1, %r12d
movl 0x10(%r14), %ebp
cmpl $0x2, %ebp
setge (%rsp)
andl $0x40, %r15d
pushq $0x1
popq %rdi
pushq $0x38
popq %rsi
callq 0x21e0
movq %rax, %rbx
cmpl $0x2, %ebp
jl 0x1d05e
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq %rbx, %r9
addq $0x8, %r9
movl %ebp, %edi
movl %r13d, %ecx
movl %r12d, %r8d
callq 0xd3a1
jmp 0x1d081
callq 0x2040
leaq 0x1162d(%rip), %rax     # 0x2e678
testb $0x1, (%rax)
jne 0x1d215
callq 0x2533
jmp 0x1d217
movl %r15d, %r8d
shrl $0x6, %r8d
movq (%r14), %rsi
movq 0x20(%r14), %rax
movq (%rax), %rdi
movq %rbx, %r9
addq $0x8, %r9
movl %r13d, %edx
movl %r12d, %ecx
callq 0xce0f
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x1d49a
shrl $0x6, %r15d
orb %r15b, (%rsp)
movq %r14, (%rbx)
xorl %eax, %eax
xorl %ecx, %ecx
movslq 0x8(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x1d0e9
movq 0x10(%rbx), %rsi
imulq $0x28, %rcx, %rdi
movl (%rsi,%rdi), %edx
movq 0x10(%rsi,%rdi), %rsi
testl %edx, %edx
cmovlel %eax, %edx
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x1d0e4
movzbl (%rsi,%rdi), %r8d
leal -0x7b(%r8), %r9d
leal -0x20(%r8), %r10d
cmpb $-0x1a, %r9b
movzbl %r10b, %r9d
cmovbl %r8d, %r9d
movb %r9b, (%rsi,%rdi)
incq %rdi
jmp 0x1d0bd
incq %rcx
jmp 0x1d09d
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
imulq $0x28, %rdx, %rcx
cmpq %rax, %rcx
je 0x1d112
movl 0x8(%r14), %edx
leal 0x1(%rdx), %esi
movl %esi, 0x8(%r14)
movq 0x10(%rbx), %rsi
movl %edx, 0x4(%rsi,%rax)
addq $0x28, %rax
jmp 0x1d0f4
movl 0xc(%r14), %ebp
movslq %ebp, %rdi
pushq $0x8
popq %rsi
callq 0x21e0
movq %rax, 0x30(%rbx)
xorl %r15d, %r15d
movslq %ebp, %rax
cmpq %rax, %r15
jge 0x1d146
callq 0x19570
movq 0x30(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movl 0xc(%r14), %ebp
jmp 0x1d128
movslq 0x8(%rbx), %r14
leaq (%r14,%r14), %rdi
pushq $0x4
popq %rsi
callq 0x21e0
movq %rax, 0x18(%rbx)
leaq (%rax,%r14,4), %rax
movq %rax, 0x20(%rbx)
movq $0x0, 0x8(%rsp)
pushq $0x8
popq %rdx
xorl %edi, %edi
movq %r14, %rsi
callq 0x24f0
movq %rax, 0x28(%rbx)
pushq $0x30
popq %r13
pushq $0x1
popq %rsi
movslq 0x8(%rbx), %rax
cmpq %rax, %rsi
jg 0x1d4a4
movl %eax, %eax
cmpq %rax, %rsi
setne %al
andb (%rsp), %al
cmpb $0x1, %al
jne 0x1d1dd
movq 0x10(%rbx), %rax
movq -0x28(%rax,%r13), %r14
movq (%rax,%r13), %r12
movq %rsi, %r15
movq %r14, %rdi
callq 0x1d90f
movl %eax, %ebp
movq %r12, %rdi
callq 0x1d90f
movq %r15, %rsi
cmpl %eax, %ebp
jne 0x1d1dd
movslq %ebp, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2070
movq %r15, %rsi
testl %eax, %eax
je 0x1d209
movl %esi, %eax
movq 0x8(%rsp), %rdi
subl %edi, %eax
movq 0x20(%rbx), %rcx
movslq 0xc(%rbx), %rdx
movl %eax, (%rcx,%rdx,4)
movq 0x18(%rbx), %rax
movslq 0xc(%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, 0xc(%rbx)
movl %edi, (%rax,%rcx,4)
movl %esi, %eax
movq %rax, 0x8(%rsp)
incq %rsi
addq $0x28, %r13
jmp 0x1d183
xorl %eax, %eax
movq %rax, 0x10(%rsp)
movq %r14, 0x8(%rsp)
addq $0x30, %r14
xorl %edx, %edx
movq %r14, (%rsp)
movslq 0xc(%rbx), %rax
cmpq %rax, %rdx
jge 0x1d464
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
movslq (%rax,%rdx,4), %r12
movq %rdx, 0x20(%rsp)
movslq (%rcx,%rdx,4), %rcx
leaq (%rcx,%r12), %rbp
movq 0x8(%rsp), %r14
movq 0x18(%r14), %rax
movl (%rax), %eax
notl %eax
testb $-0x40, %al
jne 0x1d2ab
movq 0x10(%rbx), %r15
movq %rcx, 0x18(%rsp)
leaq (,%rcx,4), %rsi
movq 0x10(%rsp), %rdi
callq 0x266c
movq %rax, %r13
movslq %ebp, %rbp
imulq $0x28, %r12, %rax
xorl %ecx, %ecx
leaq (%r12,%rcx), %rdx
cmpq %rbp, %rdx
jge 0x1d35c
movq 0x10(%rbx), %rdx
movl (%rdx,%rax), %edx
movl %edx, (%r13,%rcx,4)
addq $0x28, %rax
incq %rcx
jmp 0x1d289
imulq $0x28, %r12, %r13
movq %r12, %r15
cmpq %rbp, %r15
jge 0x1d400
movq 0x10(%rbx), %rax
movq 0x28(%rbx), %rdx
movq 0x18(%r14), %rcx
movq 0x28(%r14), %rsi
movq (%rcx), %r10
btl $0xe, %r10d
movq (%rsi), %rsi
movq (%rdx,%r15,8), %rdx
jb 0x1d300
movq 0x8(%rax,%r13), %r9
addq %r13, %rax
movq (%rsp), %r14
movq %r14, %rdi
pushq $0x1
popq %rcx
movq %rax, %r8
pushq 0x10(%rsp)
pushq %r10
callq 0x1c229
popq %rax
popq %rcx
jmp 0x1d332
movl 0xa0(%rcx), %r10d
movl 0xa4(%rcx), %ecx
movq 0x40(%r14), %r8
movq 0x48(%r14), %r9
subq $0x8, %rsp
movq %rsi, %rdi
movq %rdx, %rsi
movl %r10d, %edx
pushq 0x8(%rax,%r13)
callq 0x1b260
popq %rax
popq %rcx
movq (%rsp), %r14
cmpl $0x0, (%r14)
movq 0x8(%rsp), %r14
je 0x1d350
movq 0x38(%r14), %rdi
movq 0x10c90(%rip), %rax     # 0x2dfd8
movq (%rax), %rsi
callq 0x382c
incq %r15
addq $0x28, %r13
jmp 0x1d2b2
imulq $0x28, %r12, %rax
addq %rax, %r15
movq 0x18(%r14), %rcx
movq 0x28(%r14), %rdx
movq (%rcx), %rax
btl $0xe, %eax
movq (%rdx), %rsi
movq 0x28(%rbx), %rdx
movq (%rdx,%r12,8), %rdx
jb 0x1d3a3
movq 0x8(%r15), %r9
movq (%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rcx
movq %r13, %r8
movq 0x10(%rsp), %r14
pushq %r14
pushq %rax
callq 0x1c229
popq %rax
popq %rcx
jmp 0x1d3d7
movl 0xa0(%rcx), %eax
movl 0xa4(%rcx), %ecx
movq 0x40(%r14), %r8
movq 0x48(%r14), %r9
subq $0x8, %rsp
movq %rsi, %rdi
movq %rdx, %rsi
movl %eax, %edx
pushq 0x8(%r15)
callq 0x1b260
popq %rax
popq %rcx
movq 0x10(%rsp), %r14
movq (%rsp), %r15
movq %r14, %rdi
movq %r13, %rsi
callq 0x2577
cmpl $0x0, (%r15)
je 0x1d400
movq 0x8(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x10be0(%rip), %rax     # 0x2dfd8
movq (%rax), %rsi
callq 0x382c
imulq $0x28, %r12, %r15
cmpq %rbp, %r12
jge 0x1d457
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rdi
callq 0x17683
movq 0x10(%rbx), %rax
movq 0x10(%rax,%r15), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x8(%rax,%r15), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x18(%rax,%r15), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x20(%rax,%r15), %rdi
callq 0x2040
incq %r12
addq $0x28, %r15
jmp 0x1d404
movq 0x20(%rsp), %rdx
incq %rdx
jmp 0x1d22b
movq 0x28(%rbx), %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
callq 0x2040
movq 0x10(%rbx), %rdi
callq 0x2040
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1d48e
callq 0x253c
leaq 0x10f6b(%rip), %rax     # 0x2e400
cmpl $0x3, (%rax)
jge 0x1d4b6
movq %rbx, %rdi
callq 0x2040
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10b3b(%rip), %rax     # 0x2dff8
movq (%rax), %r14
callq 0x37fd
leaq 0x111b4(%rip), %r15     # 0x2e680
subsd (%r15), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x3794
movsd %xmm0, (%rsp)
callq 0x37fd
subsd (%r15), %xmm0
movsd (%rsp), %xmm1
divsd %xmm0, %xmm1
movl 0x8(%rbx), %ecx
leaq 0x7b3d(%rip), %rsi      # 0x2503b
leaq 0x7b5e(%rip), %rdx      # 0x25063
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
movb $0x2, %al
callq 0x2210
jmp 0x1d49a