// File: /workspace/github_c_makefile_star2_O2/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 0xf152
leaq 0x58c9(%rip), %rsi      # 0x149f9
leaq 0x58d9(%rip), %rdx      # 0x14a10
pushq $0x2f
popq %rcx
movl $0x800c0000, %edi       # imm = 0x800C0000
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9d4c
movq %rsi, %r14
movq %rdi, %rbx
pushq $0x1
popq %rdi
pushq $0x78
popq %rsi
callq 0x32b0
movq %rax, (%rbx)
testq %rax, %rax
je 0xf202
movq %rax, %r15
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x32b0
movq %rax, (%r15)
testq %rax, %rax
je 0xf235
movq %r14, 0x8(%r15)
addq $0x10, %r15
movq %r15, %rdi
callq 0xf7c1
testb %al, %al
je 0xf21c
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0xf510
testb %al, %al
je 0xf21c
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0xdf34
testb %al, %al
je 0xf21c
xorps %xmm0, %xmm0
movq %rsp, %rdi
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
andq $0x0, 0x30(%rdi)
callq 0x3290
testl %eax, %eax
je 0xf26a
movl %eax, %ebp
movq %rbx, %rdi
callq 0xf2fb
leaq 0x5808(%rip), %rsi      # 0x149f9
leaq 0x5818(%rip), %rdx      # 0x14a10
pushq $0x4b
popq %rcx
movl %ebp, %edi
jmp 0xf2e5
callq 0x3080
movl (%rax), %edi
leaq 0x57e9(%rip), %rsi      # 0x149f9
leaq 0x57f9(%rip), %rdx      # 0x14a10
pushq $0x33
popq %rcx
jmp 0xf257
movq %rbx, %rdi
callq 0xf2fb
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 0xbcac
leaq 0x57ae(%rip), %rsi      # 0x149f9
leaq 0x57be(%rip), %rdx      # 0x14a10
pushq $0x39
popq %rcx
movl %ebp, %edi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9e45
leaq 0x16d(%rip), %r12       # 0xf3de
movq %rsp, %r13
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0xf29f
movq (%rbx), %rcx
movq (%rcx), %rdi
addq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3420
testl %eax, %eax
jne 0xf2a7
incq %rbp
addq $0x8, %r15
jmp 0xf279
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0xf2bc
cmpq %r14, %rbp
setb %r14b
movq (%rbx), %rcx
movq (%rcx), %rcx
andq $0x0, (%rcx,%rbp,8)
movl %eax, %r15d
movq %rsp, %rdi
callq 0x34e0
testb %r14b, %r14b
je 0xf2ef
movq %rbx, %rdi
callq 0xf2fb
leaq 0x5721(%rip), %rsi      # 0x149f9
leaq 0x5731(%rip), %rdx      # 0x14a10
pushq $0x6d
popq %rcx
movl %r15d, %edi
callq 0x9e45
jmp 0xf226
cmpq $0x0, (%rbx)
setne %al
jmp 0xf226