// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: worker_pipeline(void*, int, void*)
static void *worker_pipeline(void *data, int step, void *in) // callback for kt_pipeline()
{
    sldat_t *p = (sldat_t*)data;
    ///uint64_t total_base = 0, total_pair = 0;
    if (step == 0) { // step 1: read a block of sequences
        int ret1, ret2;
        uint64_t l1, l2;
		stepdat_t *s;
		CALLOC(s, 1);
        s->idx = p->idx; s->id = p->total_pair; s->t_ch = p->t_ch;
        while (((ret1 = kseq_read(p->ks1)) >= 0)&&((ret2 = kseq_read(p->ks2)) >= 0)) 
        {
            if (p->ks1->seq.l < p->idx->k || p->ks2->seq.l < p->idx->k) 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);
            }

            l1 = p->ks1->seq.l; l2 = p->ks2->seq.l;
            MALLOC(s->seq[s->n], l1+l2);
            s->sum_len += l1+l2;
            memcpy(s->seq[s->n], p->ks1->seq.s, l1);
            memcpy(s->seq[s->n]+l1, p->ks2->seq.s, l2);
            s->len[s->n++] = (uint64_t)(l1<<32)|(uint64_t)l2;

            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
        stepdat_t *s = (stepdat_t*)in;
        CALLOC(s->pos_buf, p->n_thread);
        CALLOC(s->pos, s->n);
        int i;
        kt_for(p->n_thread, worker_for_alignment, s, s->n);
        for (i = 0; i < s->n; ++i) {
            free(s->seq[i]);
            p->total_base += (s->len[i]>>32) + (uint32_t)s->len[i];
        }
        
        free(s->seq); free(s->len);
        for (i = 0; i < (int)p->n_thread; ++i) {
            free(s->pos_buf[i].a.a);
        }
        free(s->pos_buf);
		return s;
    }
    else if (step == 2) { // step 3: dump
        stepdat_t *s = (stepdat_t*)in;
        int i;
        for (i = 0; i < s->n; ++i) {
            // if(s->pos[i].a == NULL) continue;
            // kv_push(pe_hit_hap, p->hits, s->pos[i]);
            if(s->pos[i].s == (uint64_t)-1) continue;
            kv_push(pe_hit, p->hits.a, s->pos[i]);
        }
        free(s->pos);
        free(s);
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
testl %esi, %esi
je 0x15d8ab
movq %rdx, %rbx
cmpl $0x1, %esi
je 0x15d7e8
cmpl $0x2, %esi
jne 0x15da73
movl 0x8(%rbx), %eax
testl %eax, %eax
jle 0x15d7da
xorl %r15d, %r15d
movl $0x2, %r12d
xorl %r13d, %r13d
movq 0x40(%rbx), %rcx
cmpq $-0x1, (%rcx,%r15)
je 0x15d7cb
movq 0x38(%r14), %rdx
movq 0x48(%r14), %rax
cmpq 0x40(%r14), %rdx
jne 0x15d7a8
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r12, %rsi
movq %rsi, 0x40(%r14)
shlq $0x5, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x48(%r14)
movq 0x40(%rbx), %rcx
movq 0x38(%r14), %rdx
leaq 0x1(%rdx), %rsi
movq %rsi, 0x38(%r14)
shlq $0x5, %rdx
movups (%rcx,%r15), %xmm0
movups 0x10(%rcx,%r15), %xmm1
movups %xmm1, 0x10(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movl 0x8(%rbx), %eax
incq %r13
movslq %eax, %rcx
addq $0x20, %r15
cmpq %rcx, %r13
jl 0x15d768
movq 0x40(%rbx), %rdi
callq 0x2220
jmp 0x15da6b
movq 0x20(%r14), %r15
movl $0x18, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, 0x38(%rbx)
movslq 0x8(%rbx), %r12
movl $0x20, %esi
movq %r12, %rdi
callq 0x2120
movq %rax, 0x40(%rbx)
leaq 0x82d9(%rip), %rsi      # 0x165af2
movl %r15d, %edi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x108f2c
cmpl $0x0, 0x8(%rbx)
jle 0x15d85e
xorl %r15d, %r15d
movq 0x28(%rbx), %rax
movq (%rax,%r15,8), %rdi
callq 0x2220
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rax
movl %eax, %ecx
shrq $0x20, %rax
addq %rax, %rcx
addq %rcx, 0x28(%r14)
incq %r15
movslq 0x8(%rbx), %rax
cmpq %rax, %r15
jl 0x15d830
movq 0x28(%rbx), %rdi
callq 0x2220
movq 0x18(%rbx), %rdi
callq 0x2220
cmpl $0x0, 0x20(%r14)
jle 0x15d89d
movl $0x10, %r15d
xorl %r12d, %r12d
movq 0x38(%rbx), %rax
movq (%rax,%r15), %rdi
callq 0x2220
incq %r12
movslq 0x20(%r14), %rax
addq $0x18, %r15
cmpq %rax, %r12
jl 0x15d880
movq 0x38(%rbx), %rdi
callq 0x2220
jmp 0x15da75
movl $0x1, %edi
movl $0x50, %esi
callq 0x2120
movq %rax, %rbx
movq (%r14), %rax
movq %rax, (%rbx)
movq 0x30(%r14), %rax
movq %rax, 0x20(%rbx)
movq 0x90(%r14), %rax
movq %rax, 0x48(%rbx)
movq 0x8(%r14), %rdi
callq 0x13d8d6
testl %eax, %eax
js 0x15da6b
xorl %r12d, %r12d
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x0, 0xc(%rsp)
movq 0x10(%r14), %rdi
callq 0x13d8d6
testl %eax, %eax
js 0x15da5d
movq (%r14), %rax
movq 0x8(%r14), %rdi
movq 0x30(%rdi), %rbp
movq 0x40(%rax), %rax
cmpq %rax, %rbp
jb 0x15da50
movq 0x10(%r14), %rcx
movq 0x30(%rcx), %rcx
cmpq %rax, %rcx
jb 0x15da50
movl 0x1c(%rsp), %eax
cmpl %eax, 0xc(%rsp)
jne 0x15d9ab
movl %eax, %r13d
sarl %r13d
addl %eax, %r13d
cmpl $0x10, %eax
movl $0x10, %eax
cmovll %eax, %r13d
movl %r13d, 0xc(%rbx)
movslq %r13d, %r15
shlq $0x3, %r15
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, 0x18(%rbx)
movq 0x28(%rbx), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, 0x20(%rsp)
movq %rax, 0x28(%rbx)
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq 0x30(%rax), %rbp
movq 0x30(%rcx), %rcx
movl 0x10(%rbx), %r12d
movl %r13d, 0x1c(%rsp)
jmp 0x15d9b0
movq %r13, 0x20(%rsp)
movq %rcx, 0x30(%rsp)
movl %r12d, 0x2c(%rsp)
leaq (%rcx,%rbp), %r12
movq %r12, %rdi
callq 0x2280
movl 0xc(%rsp), %r13d
movslq %r13d, %rcx
movq %rcx, 0x10(%rsp)
movq 0x20(%rsp), %r15
movq %rax, (%r15,%rcx,8)
addl 0x2c(%rsp), %r12d
movl %r12d, 0x10(%rbx)
movq 0x8(%r14), %rcx
movq 0x40(%rcx), %rsi
movq %rax, %rdi
movq %rbp, %rdx
callq 0x2160
movq 0x10(%rsp), %rax
movq (%r15,%rax,8), %rdi
addq %rbp, %rdi
movq 0x10(%r14), %rax
movq 0x40(%rax), %rsi
movq 0x30(%rsp), %r15
movq %r15, %rdx
callq 0x2160
shlq $0x20, %rbp
orq %r15, %rbp
movq 0x18(%rbx), %rcx
incl %r13d
movl %r13d, 0xc(%rsp)
movl %r13d, 0x8(%rbx)
movq 0x10(%rsp), %rax
movq %rbp, (%rcx,%rax,8)
movslq %r12d, %rax
cmpq %rax, 0x18(%r14)
jle 0x15da5d
movq %rcx, 0x10(%rsp)
movq 0x8(%r14), %rdi
movq 0x20(%rsp), %r13
callq 0x13d8d6
testl %eax, %eax
jns 0x15d906
movslq 0xc(%rsp), %rax
addq %rax, 0x30(%r14)
testl %r12d, %r12d
jne 0x15da75
movq %rbx, %rdi
callq 0x2220
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq