// File: karel-brinda[P]ococo/ext/htslib/cram/thread_pool.c
// Function: t_pool_init
t_pool *t_pool_init(int qsize, int tsize) {
    int i;
    t_pool *p = malloc(sizeof(*p));
    p->qsize = qsize;
    p->tsize = tsize;
    p->njobs = 0;
    p->nwaiting = 0;
    p->shutdown = 0;
    p->head = p->tail = NULL;
    p->t_stack = NULL;
#ifdef DEBUG_TIME
    p->total_time = p->wait_time = 0;
#endif

    p->t = malloc(tsize * sizeof(p->t[0]));

    pthread_mutex_init(&p->pool_m, NULL);
    pthread_cond_init(&p->empty_c, NULL);
    pthread_cond_init(&p->full_c, NULL);

    pthread_mutex_lock(&p->pool_m);

#ifdef IN_ORDER
    if (!(p->t_stack = malloc(tsize * sizeof(*p->t_stack))))
	return NULL;
    p->t_stack_top = -1;

    for (i = 0; i < tsize; i++) {
	t_pool_worker_t *w = &p->t[i];
	p->t_stack[i] = 0;
	w->p = p;
	w->idx = i;
	w->wait_time = 0;
	pthread_cond_init(&w->pending_c, NULL);
	if (0 != pthread_create(&w->tid, NULL, t_pool_worker, w))
	    return NULL;
    }
#else
    pthread_cond_init(&p->pending_c, NULL);

    for (i = 0; i < tsize; i++) {
	t_pool_worker_t *w = &p->t[i];
	w->p = p;
	w->idx = i;
	pthread_cond_init(&w->pending_c, NULL);
	if (0 != pthread_create(&w->tid, NULL, t_pool_worker, w))
	    return NULL;
    }
#endif

    pthread_mutex_unlock(&p->pool_m);

    return p;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0xc(%rbp)
movl %esi, -0x10(%rbp)
movl $0x108, %edi            # imm = 0x108
callq 0x47a0
movq %rax, -0x20(%rbp)
movl -0xc(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x10(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0xc(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0xe8(%rax)
movslq -0x10(%rbp), %rax
imulq $0x50, %rax, %rdi
callq 0x47a0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x48a0
movq -0x20(%rbp), %rdi
addq $0x58, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x46b0
movq -0x20(%rbp), %rdi
addq $0xb8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x46b0
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
callq 0x4880
movslq -0x10(%rbp), %rdi
shlq $0x2, %rdi
callq 0x47a0
movq -0x20(%rbp), %rcx
movq %rax, 0xe8(%rcx)
cmpq $0x0, %rax
jne 0x606dd
movq $0x0, -0x8(%rbp)
jmp 0x607b0
movq -0x20(%rbp), %rax
movl $0xffffffff, 0xf0(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x6079b
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0x50, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0xe8(%rax), %rax
movslq -0x14(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl -0x14(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x28(%rbp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x46b0
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
movq -0x28(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
leaq 0x4c(%rip), %rdx        # 0x607c0
callq 0x4a90
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x6078b
movq $0x0, -0x8(%rbp)
jmp 0x607b0
jmp 0x6078d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x606f2
movq -0x20(%rbp), %rdi
addq $0x30, %rdi
callq 0x4420
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)