// File: karel-brinda[P]ococo/ext/htslib/cram/thread_pool.c
// Function: t_pool_destroy
void t_pool_destroy(t_pool *p, int kill) {
    int i;
    
#ifdef DEBUG
    fprintf(stderr, "Destroying pool %p, kill=%d\n", p, kill);
#endif

    /* Send shutdown message to worker threads */
    if (!kill) {
	pthread_mutex_lock(&p->pool_m);
	p->shutdown = 1;

#ifdef DEBUG
	fprintf(stderr, "Sending shutdown request\n");
#endif

#ifdef IN_ORDER
	for (i = 0; i < p->tsize; i++)
	    pthread_cond_signal(&p->t[i].pending_c);
#else
	pthread_cond_broadcast(&p->pending_c);
#endif
	pthread_mutex_unlock(&p->pool_m);

#ifdef DEBUG
	fprintf(stderr, "Shutdown complete\n");
#endif
	for (i = 0; i < p->tsize; i++)
	    pthread_join(p->t[i].tid, NULL);
    } else {
	for (i = 0; i < p->tsize; i++)
	    pthread_kill(p->t[i].tid, SIGINT);
    }

    pthread_mutex_destroy(&p->pool_m);
    pthread_cond_destroy(&p->empty_c);
    pthread_cond_destroy(&p->full_c);
#ifdef IN_ORDER
    for (i = 0; i < p->tsize; i++)
	pthread_cond_destroy(&p->t[i].pending_c);
#else
    pthread_cond_destroy(&p->pending_c);
#endif

#ifdef DEBUG_TIME
    fprintf(stderr, "Total time=%f\n", p->total_time / 1000000.0);
    fprintf(stderr, "Wait  time=%f\n", p->wait_time  / 1000000.0);
    fprintf(stderr, "%d%% utilisation\n",
	    (int)(100 - ((100.0 * p->wait_time) / p->total_time + 0.5)));
    for (i = 0; i < p->tsize; i++)
	fprintf(stderr, "%d: Wait time=%f\n", i,
		p->t[i].wait_time / 1000000.0);
#endif

    if (p->t_stack)
	free(p->t_stack);

    free(p->t);
    free(p);

#ifdef DEBUG
    fprintf(stderr, "Destroyed pool %p\n", p);
#endif
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x60dc8
movq -0x8(%rbp), %rdi
addq $0x30, %rdi
callq 0x4880
movq -0x8(%rbp), %rax
movl $0x1, 0xc(%rax)
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x60d7b
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x10(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
callq 0x4040
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x60d48
movq -0x8(%rbp), %rdi
addq $0x30, %rdi
callq 0x4420
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x60dc6
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x44e0
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x60d8f
jmp 0x60e09
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x60e07
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movl $0x2, %esi
callq 0x4770
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x60dcf
jmp 0x60e09
movq -0x8(%rbp), %rdi
addq $0x30, %rdi
callq 0x4670
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
callq 0x4b10
movq -0x8(%rbp), %rdi
addq $0xb8, %rdi
callq 0x4b10
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x60e6d
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
movslq -0x10(%rbp), %rax
imulq $0x50, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
callq 0x4b10
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x60e3a
movq -0x8(%rbp), %rax
cmpq $0x0, 0xe8(%rax)
je 0x60e8b
movq -0x8(%rbp), %rax
movq 0xe8(%rax), %rdi
callq 0x46a0
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x46a0
movq -0x8(%rbp), %rdi
callq 0x46a0
addq $0x10, %rsp
popq %rbp
retq
nopw (%rax,%rax)