// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/thread_pool.c
// Function: hts_tpool_process_flush
int hts_tpool_process_flush(hts_tpool_process *q) {
    int i;
    hts_tpool *p = q->p;

    DBG_OUT(stderr, "Flushing pool %p\n", p);

    // Drains the queue
    pthread_mutex_lock(&p->pool_m);

    // Wake up everything for the final sprint!
    for (i = 0; i < p->tsize; i++)
        if (p->t_stack[i])
            pthread_cond_signal(&p->t[i].pending_c);

    // Ensure there is room for the final sprint.
    // Ideally we shouldn't get here, but the "q->qsize - q->n_output >
    // n_processing" check in tpool_worker means we can trigger a
    // deadlock there.  This negates that possibility.
    if (q->qsize < q->n_output + q->n_input + q->n_processing)
        q->qsize = q->n_output + q->n_input + q->n_processing;

    // When shutdown, we won't be launching more, but we can still
    // wait for any processing jobs complete.
    if (q->shutdown) {
        while (q->n_processing)
            pthread_cond_wait(&q->none_processing_c, &p->pool_m);
    }

    // Wait for n_input and n_processing to hit zero.
    while (!q->shutdown && (q->n_input || q->n_processing)) {
        struct timeval now;
        struct timespec timeout;

        while (q->n_input && !q->shutdown) {
            gettimeofday(&now, NULL);
            timeout.tv_sec = now.tv_sec + 1;
            timeout.tv_nsec = now.tv_usec * 1000;
            pthread_cond_timedwait(&q->input_empty_c, &p->pool_m, &timeout);
        }

        // Note: even if q->shutdown is set, we still have to wait until
        // q->n_processing is zero as we cannot terminate while things are
        // running otherwise we free up the data being worked on.
        while (q->n_processing) {
            gettimeofday(&now, NULL);
            timeout.tv_sec = now.tv_sec + 1;
            timeout.tv_nsec = now.tv_usec * 1000;
            pthread_cond_timedwait(&q->none_processing_c, &p->pool_m,
                                   &timeout);
        }
        if (q->shutdown) break;
    }

    pthread_mutex_unlock(&p->pool_m);

    DBG_OUT(stderr, "Flushed complete for pool %p, queue %p\n", p, q);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r14
movq (%rdi), %r15
leaq 0x38(%r15), %rbx
movq %rbx, %rdi
callq 0x4750
pushq $0x18
popq %r12
xorl %r13d, %r13d
movslq 0x18(%r15), %rax
cmpq %rax, %r13
jge 0x2cb6f
movq 0x28(%r15), %rax
cmpl $0x0, (%rax,%r13,4)
je 0x2cb66
movq 0x20(%r15), %rdi
addq %r12, %rdi
callq 0x4040
incq %r13
addq $0x48, %r12
jmp 0x2cb46
movl 0x44(%r14), %ecx
addl 0x48(%r14), %ecx
movl 0x4c(%r14), %eax
addl %eax, %ecx
cmpl %ecx, 0x28(%r14)
jge 0x2cb87
movl %ecx, 0x28(%r14)
cmpl $0x0, 0x50(%r14)
je 0x2cbb7
leaq 0xf0(%r14), %r15
testl %eax, %eax
je 0x2cbaa
movq %r15, %rdi
movq %rbx, %rsi
callq 0x47c0
movl 0x4c(%r14), %eax
jmp 0x2cb95
cmpl $0x0, 0x50(%r14)
jne 0x2cc72
xorl %eax, %eax
leaq 0xc0(%r14), %r15
leaq 0xf0(%r14), %r12
testl %eax, %eax
setne %al
leaq 0x18(%rsp), %r13
leaq 0x8(%rsp), %rbp
movl 0x44(%r14), %ecx
testl %ecx, %ecx
sete %dl
xorb $0x1, %al
testb %al, %dl
jne 0x2cc72
testl %ecx, %ecx
je 0x2cc5e
cmpl $0x0, 0x50(%r14)
jne 0x2cc5e
movq %r13, %rdi
xorl %esi, %esi
callq 0x4810
movq 0x18(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
imulq $0x3e8, 0x20(%rsp), %rax # imm = 0x3E8
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x47a0
movl 0x44(%r14), %ecx
jmp 0x2cbe7
movq %r13, %rdi
xorl %esi, %esi
callq 0x4810
movq 0x18(%rsp), %rax
incq %rax
movq %rax, 0x8(%rsp)
imulq $0x3e8, 0x20(%rsp), %rax # imm = 0x3E8
movq %rax, 0x10(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x47a0
cmpl $0x0, 0x4c(%r14)
jne 0x2cc2b
xorl %eax, %eax
cmpl $0x0, 0x50(%r14)
je 0x2cbd4
movq %rbx, %rdi
callq 0x4340
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq