// File: /workspace/github_c_makefile_star2_O1/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 0xfd4c
leaq 0x5d21(%rip), %rsi      # 0x15a49
leaq 0x5d31(%rip), %rdx      # 0x15a60
movl $0x800c0000, %edi       # imm = 0x800C0000
movl $0x2f, %ecx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa348
movq %rsi, %r14
movq %rdi, %rbx
movl $0x1, %edi
movl $0x78, %esi
callq 0x32b0
movq %rax, (%rbx)
testq %rax, %rax
je 0xfe0b
movq %rax, %r15
movl $0x8, %esi
movq %r14, %rdi
callq 0x32b0
movq %rax, (%r15)
testq %rax, %rax
je 0xfe40
movq %r14, 0x8(%r15)
addq $0x10, %r15
movq %r15, %rdi
callq 0x103f3
testb %al, %al
je 0xfe27
movq (%rbx), %rdi
addq $0x18, %rdi
callq 0x10178
testb %al, %al
je 0xfe27
movq (%rbx), %rdi
addq $0x48, %rdi
callq 0xeaa0
testb %al, %al
je 0xfe27
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 0xfe77
movl %eax, %ebp
movq %rbx, %rdi
callq 0xff38
leaq 0x5c51(%rip), %rsi      # 0x15a49
leaq 0x5c61(%rip), %rdx      # 0x15a60
movl %ebp, %edi
movl $0x4b, %ecx
jmp 0xff22
callq 0x3080
movl (%rax), %edi
leaq 0x5c30(%rip), %rsi      # 0x15a49
leaq 0x5c40(%rip), %rdx      # 0x15a60
movl $0x33, %ecx
jmp 0xfe64
movq %rbx, %rdi
callq 0xff38
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 0xc618
leaq 0x5bf3(%rip), %rsi      # 0x15a49
leaq 0x5c03(%rip), %rdx      # 0x15a60
movl %ebp, %edi
movl $0x39, %ecx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa444
testq %r14, %r14
setne %r12b
je 0xfef4
movq (%rbx), %rcx
movq (%rcx), %rdi
leaq 0x1a0(%rip), %rdx       # 0x1002d
movq %rsp, %rsi
callq 0x3410
testl %eax, %eax
je 0xfea0
movl %eax, %r15d
xorl %ebp, %ebp
jmp 0xfee1
leaq -0x1(%r14), %r15
movl $0x8, %r12d
movq %rsp, %r13
xorl %ebp, %ebp
cmpq %rbp, %r15
je 0xfef1
movq (%rbx), %rcx
movq (%rcx), %rdi
addq %r12, %rdi
movq %r13, %rsi
leaq 0x166(%rip), %rdx       # 0x1002d
callq 0x3410
addq $0x8, %r12
incq %rbp
testl %eax, %eax
je 0xfeaf
cmpq %r14, %rbp
setb %r12b
movl %eax, %r15d
movq (%rbx), %rax
movq (%rax), %rax
movq $0x0, (%rax,%rbp,8)
jmp 0xfef7
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsp, %rdi
callq 0x34d0
testb %r12b, %r12b
je 0xff2c
movq %rbx, %rdi
callq 0xff38
leaq 0x5b36(%rip), %rsi      # 0x15a49
leaq 0x5b46(%rip), %rdx      # 0x15a60
movl %r15d, %edi
movl $0x6d, %ecx
callq 0xa444
jmp 0xfe31
cmpq $0x0, (%rbx)
setne %al
jmp 0xfe31