// File: /workspace/github_c_makefile_star2_O3/aremmell[P]libsir/src/sirthreadpool.c
// Function: sir_threadpool_create
bool _sir_threadpool_create(sir_threadpool** pool, size_t num_threads) {
    if (!pool || !num_threads || num_threads > SIR_THREADPOOL_MAX_THREADS)
        return _sir_seterror(_SIR_E_INVALID);

    *pool = calloc(1, sizeof(sir_threadpool));
    if (!*pool)
        return _sir_handleerr(errno);

    (*pool)->threads = calloc(num_threads, sizeof(sir_thread));
    if (!(*pool)->threads) {
        int err = errno;
        _sir_safefree(pool);
        return _sir_handleerr(err);
    }

    (*pool)->num_threads = num_threads;

    if (!_sir_queue_create(&(*pool)->jobs) || !_sir_condcreate(&(*pool)->cond) ||
        !_sir_mutexcreate(&(*pool)->mutex)) {
        bool destroy = _sir_threadpool_destroy(pool);
        SIR_ASSERT_UNUSED(destroy, destroy);
        return false;
    }

#if !defined(__WIN__)
    pthread_attr_t attr = {0};
    int op = pthread_attr_init(&attr);
    if (0 != op) {
        bool destroy = _sir_threadpool_destroy(pool);
        SIR_ASSERT_UNUSED(destroy, destroy);
        return _sir_handleerr(op);
    }
#endif

    int thrd_err     = 0;
    bool thrd_create = true;
    for (size_t n = 0; n < num_threads; n++) {
#if !defined(__WIN__)
        op = pthread_create(&(*pool)->threads[n], &attr, &thread_pool_proc, *pool);
        if (0 != op) {
            (*pool)->threads[n] = 0;
            thrd_err    = op;
            thrd_create = false;
            break;
        }
#else /* __WIN__ */
        (*pool)->threads[n] = (HANDLE)_beginthreadex(NULL, 0, &thread_pool_proc,
            *pool, 0, NULL);
        if (!(*pool)->threads[n]) {
            thrd_err    = errno;
            thrd_create = false;
            break;
        }
#endif
    }

#if !defined(__WIN__)
    op = pthread_attr_destroy(&attr);
    SIR_ASSERT_UNUSED(0 == op, op);
#endif

    if (!thrd_create) {
        bool destroy = _sir_threadpool_destroy(pool);
        SIR_ASSERT_UNUSED(destroy, destroy);
        return _sir_handleerr(thrd_err);
    }

    return !!*pool;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
testq %rdi, %rdi
setne %al
leaq -0x183(%rsi), %rcx
cmpq $-0x182, %rcx           # imm = 0xFE7E
setae %cl
testb %cl, %al
jne 0xfaec
leaq 0x5f71(%rip), %rsi      # 0x15a39
leaq 0x5f81(%rip), %rdx      # 0x15a50
movl $0x800c0000, %edi       # imm = 0x800C0000
movl $0x2f, %ecx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa338
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, %edi
movl $0x78, %esi
callq 0x32b0
movq %rax, (%rbx)
testq %rax, %rax
je 0xfbab
movq %rax, %r15
movl $0x8, %esi
movq %r14, %rdi
callq 0x32b0
movq %rax, (%r15)
testq %rax, %rax
je 0xfbe0
movq %r14, 0x8(%r15)
addq $0x10, %r15
movq %r15, %rdi
callq 0x100de
testb %al, %al
je 0xfbc7
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0xfec4
testb %al, %al
je 0xfbc7
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0xe884
testb %al, %al
je 0xfbc7
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq $0x0, 0x30(%rdi)
callq 0x3290
testl %eax, %eax
je 0xfc17
movl %eax, %ebp
movq %rbx, %rdi
callq 0xfc98
leaq 0x5ea1(%rip), %rsi      # 0x15a39
leaq 0x5eb1(%rip), %rdx      # 0x15a50
movl %ebp, %edi
movl $0x4b, %ecx
jmp 0xfc8e
callq 0x3080
movl (%rax), %edi
leaq 0x5e80(%rip), %rsi      # 0x15a39
leaq 0x5e90(%rip), %rdx      # 0x15a50
movl $0x33, %ecx
jmp 0xfc04
movq %rbx, %rdi
callq 0xfc98
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3080
movl (%rax), %ebp
movq %rbx, %rdi
callq 0xc558
leaq 0x5e43(%rip), %rsi      # 0x15a39
leaq 0x5e53(%rip), %rdx      # 0x15a50
movl %ebp, %edi
movl $0x39, %ecx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa434
xorl %r13d, %r13d
leaq 0x16a(%rip), %r15       # 0xfd8b
movq %rsp, %r12
movq (%rbx), %rcx
movq (%rcx), %rdi
addq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3410
testl %eax, %eax
jne 0xfc59
addq $0x8, %r13
decq %r14
jne 0xfc24
movq %rsp, %rdi
callq 0x34d0
cmpq $0x0, (%rbx)
setne %al
jmp 0xfbd1
movl %eax, %ebp
movq (%rbx), %rax
movq (%rax), %rax
movq $0x0, (%rax,%r13)
movq %rsp, %rdi
callq 0x34d0
movq %rbx, %rdi
callq 0xfc98
leaq 0x5db9(%rip), %rsi      # 0x15a39
leaq 0x5dc9(%rip), %rdx      # 0x15a50
movl %ebp, %edi
movl $0x6d, %ecx
callq 0xa434
jmp 0xfbd1