// File: /workspace/github_c_makefile_star2_O0/samtools[P]htslib/thread_pool.c
// Function: hts_tpool_init
hts_tpool *hts_tpool_init(int n) {
    int t_idx = 0;
    size_t stack_size = 0;
    pthread_attr_t pattr;
    int pattr_init_done = 0;
    hts_tpool *p = malloc(sizeof(*p));
    if (!p)
        return NULL;
    p->tsize = n;
    p->njobs = 0;
    p->nwaiting = 0;
    p->shutdown = 0;
    p->q_head = NULL;
    p->t_stack = NULL;
    p->n_count = 0;
    p->n_running = 0;
    p->t = malloc(n * sizeof(p->t[0]));
    if (!p->t) {
        free(p);
        return NULL;
    }
    p->t_stack = malloc(n * sizeof(*p->t_stack));
    if (!p->t_stack) {
        free(p->t);
        free(p);
        return NULL;
    }
    p->t_stack_top = -1;

    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&p->pool_m, &attr);
    pthread_mutexattr_destroy(&attr);

    pthread_mutex_lock(&p->pool_m);

    // Ensure new threads have a reasonably large stack.  On some platforms,
    // for example MacOS which defaults to 512Kb, this is not big enough
    // for some of the rANS codecs.

    if (pthread_attr_init(&pattr) < 0)
        goto cleanup;
    pattr_init_done = 1;
    if (pthread_attr_getstacksize(&pattr, &stack_size) < 0)
        goto cleanup;
    if (stack_size < HTS_MIN_THREAD_STACK) {
        if (pthread_attr_setstacksize(&pattr, HTS_MIN_THREAD_STACK) < 0)
            goto cleanup;
    }

    for (t_idx = 0; t_idx < n; t_idx++) {
        hts_tpool_worker *w = &p->t[t_idx];
        p->t_stack[t_idx] = 0;
        w->p = p;
        w->idx = t_idx;
        pthread_cond_init(&w->pending_c, NULL);
        if (0 != pthread_create(&w->tid, &pattr, tpool_worker, w))
            goto cleanup;
    }

    pthread_mutex_unlock(&p->pool_m);
    pthread_attr_destroy(&pattr);

    return p;

 cleanup: {
        // Any threads started will be waiting for p->pool_m, so we can
        // stop them cleanly by setting p->shutdown, releasing the mutex and
        // waiting for them to finish.
        int j;
        int save_errno = errno;
        hts_log_error("Couldn't start thread pool worker : %s",
                      strerror(errno));
        p->shutdown = 1;
        pthread_mutex_unlock(&p->pool_m);
        for (j = 0; j < t_idx; j++) {
            pthread_join(p->t[j].tid, NULL);
            pthread_cond_destroy(&p->t[j].pending_c);
        }
        pthread_mutex_destroy(&p->pool_m);
        if (pattr_init_done)
            pthread_attr_destroy(&pattr);
        free(p->t_stack);
        free(p->t);
        free(p);
        errno = save_errno;
        return NULL;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x78, %edi
callq 0x4650
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x4dae6
movq $0x0, -0x8(%rbp)
jmp 0x4de0f
movl -0xc(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x60(%rbp), %rax
movl $0x0, 0x4(%rax)
movq -0x60(%rbp), %rax
movl $0x0, (%rax)
movq -0x60(%rbp), %rax
movl $0x0, 0x8(%rax)
movq -0x60(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x60(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x60(%rbp), %rax
movl $0x0, 0x60(%rax)
movq -0x60(%rbp), %rax
movl $0x0, 0x64(%rax)
movslq -0xc(%rbp), %rax
imulq $0x48, %rax, %rdi
callq 0x4650
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x4db77
movq -0x60(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x4de0f
movslq -0xc(%rbp), %rdi
shlq $0x2, %rdi
callq 0x4650
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x60(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x4dbbd
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x45a0
movq -0x60(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x4de0f
movq -0x60(%rbp), %rax
movl $0xffffffff, 0x30(%rax) # imm = 0xFFFFFFFF
leaq -0x64(%rbp), %rdi
callq 0x4070
leaq -0x64(%rbp), %rdi
movl $0x1, %esi
callq 0x4740
movq -0x60(%rbp), %rdi
addq $0x38, %rdi
leaq -0x64(%rbp), %rsi
callq 0x4760
leaq -0x64(%rbp), %rdi
callq 0x4750
movq -0x60(%rbp), %rdi
addq $0x38, %rdi
callq 0x4730
leaq -0x50(%rbp), %rdi
callq 0x45d0
cmpl $0x0, %eax
jge 0x4dc19
jmp 0x4dd17
movl $0x1, -0x54(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x4430
cmpl $0x0, %eax
jge 0x4dc37
jmp 0x4dd17
cmpq $0x300000, -0x18(%rbp)  # imm = 0x300000
jae 0x4dc5b
leaq -0x50(%rbp), %rdi
movl $0x300000, %esi         # imm = 0x300000
callq 0x4670
cmpl $0x0, %eax
jge 0x4dc59
jmp 0x4dd17
jmp 0x4dc5b
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x4dcf4
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x10(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x10(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x60(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movl -0x10(%rbp), %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x70(%rbp), %rdi
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x45b0
movq -0x70(%rbp), %rdi
addq $0x10, %rdi
movq -0x70(%rbp), %rcx
leaq -0x50(%rbp), %rsi
leaq 0x14b(%rip), %rdx       # 0x4de20
callq 0x4910
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x4dce4
jmp 0x4dd17
jmp 0x4dce6
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x4dc62
movq -0x60(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
leaq -0x50(%rbp), %rdi
callq 0x4850
movq -0x60(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x4de0f
callq 0x4050
movl (%rax), %eax
movl %eax, -0x78(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq %rax, %rcx
movl $0x1, %edi
leaq 0xa5750(%rip), %rsi     # 0xf348c
leaq 0xa5758(%rip), %rdx     # 0xf349b
movb $0x0, %al
callq 0x1b030
movq -0x60(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x60(%rbp), %rdi
addq $0x38, %rdi
callq 0x4330
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x4ddb8
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x74(%rbp), %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4400
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x74(%rbp), %rax
imulq $0x48, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
callq 0x49a0
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x4dd69
movq -0x60(%rbp), %rdi
addq $0x38, %rdi
callq 0x4570
cmpl $0x0, -0x54(%rbp)
je 0x4ddd4
leaq -0x50(%rbp), %rdi
callq 0x4850
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x45a0
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x45a0
movq -0x60(%rbp), %rdi
callq 0x45a0
movl -0x78(%rbp), %eax
movl %eax, -0x7c(%rbp)
callq 0x4050
movl -0x7c(%rbp), %ecx
movl %ecx, (%rax)
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)