// File: /workspace/github_c_makefile_star2_O1/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 0x20fac
movq %rdx, %rbx
cmpl $0x1, %esi
je 0x20f90
cmpl $0x2, %esi
jne 0x21518
cmpl $0x0, 0xc(%r14)
jle 0x20f68
xorl %r15d, %r15d
movq 0x30(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x1cdc8
incq %r15
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jl 0x20f4f
movq 0x30(%rbx), %rdi
callq 0x2040
leaq 0x11700(%rip), %rax     # 0x32678
testb $0x1, (%rax)
jne 0x2123c
callq 0x3097
movq %rax, 0x8(%rsp)
jmp 0x21245
movl 0xc(%r14), %edi
movslq 0xc(%rbx), %rcx
leaq 0x6d9(%rip), %rsi       # 0x21678
movq %rbx, %rdx
callq 0x347c
jmp 0x2151a
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 %al
andl $0x40, %r13d
shrl $0x6, %r13d
orb %al, %r13b
movl $0x1, %edi
movl $0x38, %esi
callq 0x21e0
movq %rax, %rbx
cmpl $0x2, %r12d
movq %r13, 0x10(%rsp)
jl 0x21019
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq %rbx, %r9
addq $0x8, %r9
movl %r12d, %edi
movl %ebp, %ecx
movl %r15d, %r8d
callq 0xf913
jmp 0x21038
movzbl %r13b, %r8d
movq (%r14), %rsi
movq 0x20(%r14), %rax
movq (%rax), %rdi
movq %rbx, %r9
addq $0x8, %r9
movl %ebp, %edx
movl %r15d, %ecx
callq 0xf1aa
movq %rax, 0x10(%rbx)
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x21508
movq %r14, (%rbx)
movl 0x8(%rbx), %edx
testl %edx, %edx
jle 0x2109e
xorl %eax, %eax
movq 0x10(%rbx), %rdx
leaq (%rax,%rax,4), %rsi
movslq (%rdx,%rsi,8), %rcx
testq %rcx, %rcx
jle 0x21092
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 0x2106e
incq %rax
movslq 0x8(%rbx), %rdx
cmpq %rdx, %rax
jl 0x21056
testl %edx, %edx
jle 0x210c9
movq 0x10(%rbx), %rcx
movl 0x8(%r14), %eax
movl %edx, %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 0x210b6
movl %eax, 0x8(%r14)
movslq 0xc(%r14), %r12
movl $0x8, %esi
movq %r12, %rdi
callq 0x21e0
movq %rax, 0x30(%rbx)
testq %r12, %r12
jle 0x210ff
xorl %r15d, %r15d
callq 0x1cd9c
movq 0x30(%rbx), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movslq 0xc(%r14), %rax
cmpq %rax, %r15
jl 0x210e6
movslq 0x8(%rbx), %r14
leaq (%r14,%r14), %rdi
movl $0x4, %esi
callq 0x21e0
movq %rax, 0x18(%rbx)
leaq (%rax,%r14,4), %rax
movq %rax, 0x20(%rbx)
movq $0x0, 0x8(%rsp)
movl $0x8, %edx
xorl %edi, %edi
movq %r14, %rsi
callq 0x3054
movq %rax, 0x28(%rbx)
movl 0x8(%rbx), %eax
testl %eax, %eax
movq 0x10(%rsp), %rsi
jle 0x21510
movl $0x1, %ebp
movl $0x30, %r15d
movl %eax, %eax
cmpq %rax, %rbp
setne %al
andb %sil, %al
cmpb $0x1, %al
jne 0x211f3
movq 0x10(%rbx), %rax
movq -0x28(%rax,%r15), %r14
movq (%rax,%r15), %r12
movq %r14, %rdi
callq 0x2110
cmpl $0x3, %eax
jl 0x211a1
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movb (%r14,%rcx), %cl
addb $-0x30, %cl
cmpb $0x9, %cl
ja 0x211a1
leal -0x2(%rax), %r13d
cmpb $0x2f, (%r14,%r13)
je 0x211a4
movl %eax, %r13d
movq %r12, %rdi
callq 0x2110
cmpl $0x3, %eax
jl 0x211d0
movl %eax, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
addl %edx, %ecx
movb (%r12,%rcx), %cl
addb $-0x30, %cl
cmpb $0x9, %cl
ja 0x211d0
leal -0x2(%rax), %ecx
cmpb $0x2f, (%r12,%rcx)
je 0x211d2
movl %eax, %ecx
cmpl %ecx, %r13d
movq 0x10(%rsp), %rsi
jne 0x211f3
movslq %r13d, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2070
movq 0x10(%rsp), %rsi
testl %eax, %eax
je 0x2121f
movl %ebp, %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 %ebp, %eax
movq %rax, 0x8(%rsp)
leaq 0x1(%rbp), %rcx
movslq 0x8(%rbx), %rax
addq $0x28, %r15
cmpq %rax, %rbp
movq %rcx, %rbp
jl 0x21154
jmp 0x21510
movq $0x0, 0x8(%rsp)
cmpl $0x0, 0xc(%rbx)
jle 0x214c8
leaq 0x30(%r14), %rax
movq %rax, 0x10(%rsp)
xorl %edx, %edx
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
movslq (%rax,%rdx,4), %rbp
movq %rdx, 0x20(%rsp)
movslq (%rcx,%rdx,4), %rcx
leaq (%rcx,%rbp), %r15
movq 0x18(%r14), %rax
movl (%rax), %eax
notl %eax
testb $-0x40, %al
movq %rcx, 0x18(%rsp)
jne 0x21324
leaq (,%rbp,4), %r13
addq %rbp, %r13
shlq $0x3, %r13
movq 0x10(%rbx), %r12
addq %r13, %r12
leaq (,%rcx,4), %rsi
movq 0x8(%rsp), %rdi
callq 0x31c1
movq 0x18(%rsp), %rcx
testl %ecx, %ecx
jle 0x212dc
movslq %r15d, %r8
addq 0x10(%rbx), %r13
movq %rax, %rdi
movq %rbp, %rdx
movl (%r13), %esi
movl %esi, (%rdi)
incq %rdx
addq $0x4, %rdi
addq $0x28, %r13
cmpq %r8, %rdx
jl 0x212c6
movq 0x18(%r14), %r9
movq (%r9), %r10
btl $0xe, %r10d
jb 0x213df
movq 0x28(%r14), %rdx
movq (%rdx), %rsi
movq 0x28(%rbx), %rdx
movq (%rdx,%rbp,8), %rdx
movq 0x8(%r12), %r9
movq 0x10(%rsp), %rdi
movq %rax, %r13
movq %rax, %r8
movq 0x8(%rsp), %r12
pushq %r12
pushq %r10
callq 0x200b7
addq $0x10, %rsp
jmp 0x2141e
testl %ecx, %ecx
jle 0x21443
leaq (,%rbp,8), %rax
leaq (%rax,%rax,4), %r13
movq %rbp, %r12
movq 0x10(%rbx), %rax
movq 0x18(%r14), %rcx
movq (%rcx), %r10
btl $0xe, %r10d
jb 0x2137e
movq 0x28(%r14), %rcx
movq (%rcx), %rsi
movq 0x28(%rbx), %rcx
movq (%rcx,%r12,8), %rdx
movq 0x8(%rax,%r13), %r9
addq %r13, %rax
movq 0x10(%rsp), %rdi
movl $0x1, %ecx
movq %rax, %r8
pushq 0x8(%rsp)
pushq %r10
callq 0x200b7
jmp 0x213af
movq 0x28(%r14), %rdx
movq 0x40(%r14), %r8
movq (%rdx), %rdi
movq 0x28(%rbx), %rdx
movq (%rdx,%r12,8), %rsi
movl 0xa0(%rcx), %edx
movl 0xa4(%rcx), %ecx
movq 0x48(%r14), %r9
subq $0x8, %rsp
pushq 0x8(%rax,%r13)
callq 0x1ecec
addq $0x10, %rsp
cmpl $0x0, 0x30(%r14)
je 0x213cd
movq 0x38(%r14), %rdi
movq 0x10c13(%rip), %rax     # 0x31fd8
movq (%rax), %rsi
callq 0x43ec
incq %r12
addq $0x28, %r13
cmpq %r15, %r12
jl 0x2133b
jmp 0x21443
movq 0x28(%r14), %rcx
movq %rax, %r13
movq 0x40(%r14), %r8
movq (%rcx), %rdi
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movl 0xa0(%r9), %edx
movl 0xa4(%r9), %ecx
movq 0x48(%r14), %r9
subq $0x8, %rsp
pushq 0x8(%r12)
callq 0x1ecec
addq $0x10, %rsp
movq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x30f6
cmpl $0x0, 0x30(%r14)
je 0x21443
movq 0x38(%r14), %rdi
movq 0x10b9d(%rip), %rax     # 0x31fd8
movq (%rax), %rsi
callq 0x43ec
cmpl $0x0, 0x18(%rsp)
jle 0x214b3
movslq %r15d, %r15
movq %rbp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,4), %r12
movq 0x28(%rbx), %rax
movq (%rax,%rbp,8), %rdi
callq 0x1ac72
movq 0x10(%rbx), %rax
movq 0x10(%rax,%r12), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x8(%rax,%r12), %rdi
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x18(%rax,%r12), %rdi
testq %rdi, %rdi
je 0x21494
callq 0x2040
movq 0x10(%rbx), %rax
movq 0x20(%rax,%r12), %rdi
testq %rdi, %rdi
je 0x214a7
callq 0x2040
incq %rbp
addq $0x28, %r12
cmpq %r15, %rbp
jl 0x21458
movq 0x20(%rsp), %rdx
incq %rdx
movslq 0xc(%rbx), %rax
cmpq %rax, %rdx
jl 0x2125a
movq 0x28(%rbx), %rdi
callq 0x2040
movq 0x18(%rbx), %rdi
callq 0x2040
movq 0x10(%rbx), %rdi
callq 0x2040
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x214f2
callq 0x30b8
leaq 0x10f07(%rip), %rax     # 0x32400
cmpl $0x3, (%rax)
jge 0x2152c
movq %rbx, %rdi
callq 0x2040
jmp 0x21518
movq %rbx, %rdi
callq 0x2040
cmpq $0x0, 0x18(%rsp)
jne 0x2151a
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 0x2906
jmp 0x214fe