// File: yueww[P]TSR/yyThreadPool.c
// Function: thread_work(void*)
void *thread_work(void *args){
    pthread_detach(pthread_self());
    threadpool_t *pThreadPool=(threadpool_t *)args;
    while(keepAlive){
        bsem_wait((pThreadPool->tasks).has_tasks);
        if(keepAlive){
            pthread_mutex_lock(&(pThreadPool->tmutex));
            pThreadPool->num_running_threads++;
            pthread_mutex_unlock(&(pThreadPool->tmutex));
            task *t;
            pthread_mutex_lock(&((pThreadPool->tasks).qmutex));
            if((pThreadPool->tasks).qlen==0){
                pthread_mutex_unlock(&((pThreadPool->tasks).qmutex));
                continue;
            }else if((pThreadPool->tasks).qlen==1){
                t=(pThreadPool->tasks).front;
                (pThreadPool->tasks).front=NULL;
                (pThreadPool->tasks).rear=NULL;
            }else{
                t=(pThreadPool->tasks).front;
                (pThreadPool->tasks).front=((pThreadPool->tasks).front)->next;
                bsem_post((pThreadPool->tasks).has_tasks);
            }
            (pThreadPool->tasks).qlen--;
            if((pThreadPool->tasks).qlen==((pThreadPool->tasks).max_len-1)){
                pthread_cond_signal(&((pThreadPool->tasks).not_full));
            }
            pthread_mutex_unlock(&((pThreadPool->tasks).qmutex));
            void *(*func)(void *)=t->func;
            func(t->args);
            free(t);
            pthread_mutex_lock(&(pThreadPool->tmutex));
            pThreadPool->num_running_threads--;
#ifdef LIMITTASK
            pthread_cond_signal(&(pThreadPool->has_idle));
#endif
            pthread_mutex_unlock(&(pThreadPool->tmutex));
        }
    }
    pthread_mutex_lock(&(pThreadPool->tmutex));
    pThreadPool->num_alive_threads--;
    pthread_mutex_unlock(&(pThreadPool->tmutex));
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
callq 0x2290
movq %rax, %rdi
callq 0x21a0
movq -0x8(%rbp), %rax
movq %rax, -0x10(%rbp)
movl 0xc96d(%rip), %eax      # 0xf2c4
cmpl $0x0, %eax
je 0x2ac8
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x2b00
movl 0xc951(%rip), %eax      # 0xf2c4
cmpl $0x0, %eax
je 0x2ac3
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x2440
movq -0x10(%rbp), %rax
movl 0x84(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x84(%rax)
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x21f0
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x2440
movq -0x10(%rbp), %rax
cmpl $0x0, 0x6c(%rax)
jne 0x29d8
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x21f0
jmp 0x2951
movq -0x10(%rbp), %rax
cmpl $0x1, 0x6c(%rax)
jne 0x2a06
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
jmp 0x2a30
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x2b50
jmp 0x2a32
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x6c(%rax)
movq -0x10(%rbp), %rax
movl 0x6c(%rax), %eax
movq -0x10(%rbp), %rcx
movl 0x68(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2a61
movq -0x10(%rbp), %rdi
addq $0x38, %rdi
callq 0x2030
movq -0x10(%rbp), %rdi
addq $0x10, %rdi
callq 0x21f0
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rdi
callq *%rax
movq -0x18(%rbp), %rdi
callq 0x2370
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x2440
movq -0x10(%rbp), %rax
movl 0x84(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x84(%rax)
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x21f0
jmp 0x2951
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x2440
movq -0x10(%rbp), %rax
movl 0x80(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x80(%rax)
movq -0x10(%rbp), %rdi
addq $0x88, %rdi
callq 0x21f0
ud2
nopl (%rax)