// File: /workspace/github_c_makefile_star2_O3/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 0x20fd5
movq %rdx, %rbx
cmpl $0x1, %esi
je 0x20fb9
cmpl $0x2, %esi
jne 0x21540
cmpl $0x0, 0xc(%r14)
jle 0x20f92
xorl %r15d, %r15d
movq 0x30(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x1ce50
incq %r15
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jl 0x20f79
movq 0x30(%rbx), %rdi
callq 0x2040
leaq 0x116d6(%rip), %rax     # 0x32678
testb $0x1, (%rax)
jne 0x21272
callq 0x2fc7
movq %rax, (%rsp)
jmp 0x2127a
movl 0xc(%r14), %edi
movslq 0xc(%rbx), %rcx
leaq 0x6de(%rip), %rsi       # 0x216a6
movq %rbx, %rdx
callq 0x33a0
jmp 0x21542
movq 0x18(%r14), %rax
movq (%rax), %r13
xorl %ebp, %ebp
btl $0x12, %r13d
setae %bpl
movl %r13d, %r15d
shrl $0x10, %r15d
andl $0x1, %r15d
movl 0x10(%r14), %r12d
cmpl $0x2, %r12d
setge (%rsp)
andl $0x40, %r13d
movl $0x1, %edi
movl $0x38, %esi
callq 0x21e0
movq %rax, %rbx
cmpl $0x2, %r12d
jl 0x21037
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq %rbx, %r9
addq $0x8, %r9
movl %r12d, %edi
movl %ebp, %ecx
movl %r15d, %r8d
callq 0xfd07
jmp 0x21059
movl %r13d, %r8d
shrl $0x6, %r8d
movq (%r14), %rsi
movq 0x20(%r14), %rax
movq (%rax), %rdi
movq %rbx, %r9
addq $0x8, %r9
movl %ebp, %edx
movl %r15d, %ecx
callq 0xf557
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x21538
movq %r14, (%rbx)
movl 0x8(%rbx), %r12d
testl %r12d, %r12d
jle 0x210ec
xorl %eax, %eax
movq 0x10(%rbx), %rdx
leaq (%rax,%rax,4), %rsi
movslq (%rdx,%rsi,8), %rcx
testq %rcx, %rcx
jle 0x210b4
movq 0x10(%rdx,%rsi,8), %rdx
xorl %esi, %esi
movzbl (%rdx,%rsi), %edi
leal -0x7b(%rdi), %r8d
leal -0x20(%rdi), %r9d
cmpb $-0x1a, %r8b
movzbl %r9b, %r8d
cmovbl %edi, %r8d
movb %r8b, (%rdx,%rsi)
incq %rsi
cmpq %rsi, %rcx
jne 0x2108c
movl 0x8(%rbx), %r12d
incq %rax
movslq %r12d, %rcx
cmpq %rcx, %rax
jl 0x21074
testl %r12d, %r12d
jle 0x210ec
movq 0x10(%rbx), %rcx
movl 0x8(%r14), %eax
movl %r12d, %edx
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rdx
xorl %esi, %esi
movl %eax, 0x4(%rcx,%rsi)
incl %eax
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0x210d9
movl %eax, 0x8(%r14)
movslq 0xc(%r14), %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x30(%rbx)
testq %r15, %r15
jle 0x21126
xorl %r15d, %r15d
callq 0x1ce24
movq 0x30(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jl 0x21109
movl 0x8(%rbx), %r12d
movq %r13, %r14
leal (%r12,%r12), %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x21e0
movq %rax, 0x18(%rbx)
movslq %r12d, %rsi
leaq (%rax,%rsi,4), %rax
movq %rax, 0x20(%rbx)
movl $0x8, %edx
xorl %edi, %edi
callq 0x2f84
movq %rax, 0x28(%rbx)
movl 0x8(%rbx), %r13d
testl %r13d, %r13d
jle 0x21542
shrl $0x6, %r14d
orb %r14b, (%rsp)
movl $0x1, %r12d
movl $0x30, %ebp
xorl %esi, %esi
movl %r13d, %eax
cmpq %rax, %r12
setne %al
andb (%rsp), %al
cmpb $0x1, %al
jne 0x2122d
movq %rsi, 0x10(%rsp)
movq 0x10(%rbx), %rax
movq -0x28(%rax,%rbp), %rdi
movq (%rax,%rbp), %r15
movq %rdi, 0x8(%rsp)
callq 0x2110
cmpl $0x3, %eax
jl 0x211d9
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movq 0x8(%rsp), %rdx
movb (%rdx,%rcx), %cl
addb $-0x30, %cl
cmpb $0x9, %cl
ja 0x211d9
leal -0x2(%rax), %r14d
movq 0x8(%rsp), %rcx
cmpb $0x2f, (%rcx,%r14)
je 0x211dc
movl %eax, %r14d
movq %r15, %rdi
callq 0x2110
cmpl $0x3, %eax
jl 0x21208
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movb (%r15,%rcx), %cl
addb $-0x30, %cl
cmpb $0x9, %cl
ja 0x21208
leal -0x2(%rax), %ecx
cmpb $0x2f, (%r15,%rcx)
je 0x2120a
movl %eax, %ecx
cmpl %ecx, %r14d
movq 0x10(%rsp), %rsi
jne 0x2122d
movslq %r14d, %rdx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x2070
movq 0x10(%rsp), %rsi
testl %eax, %eax
je 0x21255
movl %r12d, %eax
subl %esi, %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 %esi, (%rax,%rcx,4)
movl 0x8(%rbx), %r13d
movl %r12d, %esi
leaq 0x1(%r12), %rax
movslq %r13d, %rcx
addq $0x28, %rbp
cmpq %rcx, %r12
movq %rax, %r12
jl 0x2117b
jmp 0x21542
movq $0x0, (%rsp)
cmpl $0x0, 0xc(%rbx)
jle 0x21503
leaq 0x30(%r14), %rax
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
movslq (%rax,%rbp,4), %r12
movslq (%rcx,%rbp,4), %rcx
leaq (%rcx,%r12), %rax
movq %rax, 0x10(%rsp)
movq 0x18(%r14), %rax
movl (%rax), %eax
notl %eax
testb $-0x40, %al
jne 0x21356
movq %rbp, 0x20(%rsp)
leaq (%r12,%r12,4), %rbp
shlq $0x3, %rbp
movq 0x10(%rbx), %r15
addq %rbp, %r15
leaq (,%rcx,4), %rsi
movq (%rsp), %rdi
movq %rcx, 0x18(%rsp)
callq 0x30e5
movq 0x18(%rsp), %rcx
movq %rax, %r13
testl %ecx, %ecx
jle 0x21312
movslq 0x10(%rsp), %rax
addq 0x10(%rbx), %rbp
movq %r13, %rdi
movq %r12, %rdx
movl (%rbp), %esi
movl %esi, (%rdi)
incq %rdx
addq $0x4, %rdi
addq $0x28, %rbp
cmpq %rax, %rdx
jl 0x212fd
movq 0x18(%r14), %rdi
movq 0x28(%r14), %rdx
movq (%rdi), %rax
btl $0xe, %eax
movq (%rdx), %rsi
movq 0x28(%rbx), %rdx
movq (%rdx,%r12,8), %rdx
jb 0x21422
movq 0x8(%r15), %r9
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %r8
movq (%rsp), %r15
pushq %r15
pushq %rax
callq 0x20124
addq $0x10, %rsp
jmp 0x21458
testl %ecx, %ecx
jle 0x214f3
movq %rcx, 0x18(%rsp)
movq %rbp, 0x20(%rsp)
leaq (,%r12,8), %rax
leaq (%rax,%rax,4), %rbp
movq %r12, %r15
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 0x213c0
movq 0x8(%rax,%rbp), %r9
addq %rbp, %rax
movq 0x8(%rsp), %r13
movq %r13, %rdi
movl $0x1, %ecx
movq %rax, %r8
pushq (%rsp)
pushq %r10
callq 0x20124
addq $0x10, %rsp
jmp 0x213f4
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,%rbp)
callq 0x1ed4c
addq $0x10, %rsp
movq 0x8(%rsp), %r13
cmpl $0x0, (%r13)
je 0x2140e
movq 0x38(%r14), %rdi
movq 0x10bd2(%rip), %rax     # 0x31fd8
movq (%rax), %rsi
callq 0x42d4
incq %r15
addq $0x28, %rbp
cmpq 0x10(%rsp), %r15
jl 0x21377
jmp 0x2147c
movl 0xa0(%rdi), %eax
movl 0xa4(%rdi), %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 0x1ed4c
addq $0x10, %rsp
movq (%rsp), %r15
movq 0x8(%rsp), %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x301f
cmpl $0x0, (%rbp)
je 0x2147c
movq 0x38(%r14), %rdi
movq 0x10b64(%rip), %rax     # 0x31fd8
movq (%rax), %rsi
callq 0x42d4
cmpl $0x0, 0x18(%rsp)
movq 0x20(%rsp), %rbp
jle 0x214f3
movslq 0x10(%rsp), %r15
movq %r12, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r13
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rdi
callq 0x1ac90
movq 0x10(%rbx), %rax
movq 0x10(%rax,%r13), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x8(%rax,%r13), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x18(%rax,%r13), %rdi
testq %rdi, %rdi
je 0x214d8
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x20(%rax,%r13), %rdi
testq %rdi, %rdi
je 0x214e7
callq 0x2040
incq %r12
addq $0x28, %r13
cmpq %r15, %r12
jl 0x21498
incq %rbp
movslq 0xc(%rbx), %rax
cmpq %rax, %rbp
jl 0x2128f
movq 0x28(%rbx), %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
callq 0x2040
movq 0x10(%rbx), %rdi
callq 0x2040
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x2152c
callq 0x2fe1
leaq 0x10ecd(%rip), %rax     # 0x32400
cmpl $0x3, (%rax)
jge 0x21554
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 %rbx, %rdi
callq 0x283c
jmp 0x21538