// File: FreeSlave[P]vhlt/common/threads.cpp
// Function: RunThreadsOn(int, bool, void (*)(int))
void            RunThreadsOn(int workcnt, bool showpacifier, q_threadfunction func)
{
    intptr_t        i;
    pthread_t       work_threads[MAX_THREADS];
    pthread_addr_t  status;
    pthread_attr_t  attrib;
    double          start, end;

    threadstart = I_FloatTime();
    start = threadstart;
    for (i = 0; i < THREADTIMES_SIZE; i++)
    {
        threadtimes[i] = 0;
    }

    dispatch = 0;
    workcount = workcnt;
    oldf = -1;
    pacifier = showpacifier;
    threaded = true;
    q_entry = func;

    if (pacifier)
    {
        setbuf(stdout, NULL);
    }

    threads_InitCrit();

    if (pthread_attr_init(&attrib) == -1)
    {
        Error("pthread_attr_init failed");
    }
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
    if (pthread_attr_setstacksize(&attrib, 0x400000) == -1)
    {
        Error("pthread_attr_setstacksize failed");
    }
#endif

    for (i = 0; i < g_numthreads; i++)
    {
        if (pthread_create(&work_threads[i], &attrib, ThreadEntryStub, (void*)i) == -1)
        {
            Error("pthread_create failed");
        }
    }

    for (i = 0; i < g_numthreads; i++)
    {
        if (pthread_join(work_threads[i], &status) == -1)
        {
            Error("pthread_join failed");
        }
    }

    threads_UninitCrit();

    q_entry = NULL;
    threaded = false;

    end = I_FloatTime();
    if (pacifier)
    {
#ifdef ZHLT_CONSOLE
		PrintConsole
#else
        printf
#endif
			("\r%60s\r", "");
    }

    Log(" (%.2f seconds)\n", end - start);
}

// Assembly:
subq $0x268, %rsp            # imm = 0x268
movb %sil, %al
movl %edi, 0x264(%rsp)
andb $0x1, %al
movb %al, 0x263(%rsp)
movq %rdx, 0x258(%rsp)
callq 0x93c0
movsd %xmm0, 0x20cd1b9(%rip)  # 0x20da988
movsd 0x20cd1b1(%rip), %xmm0  # 0x20da988
movsd %xmm0, 0x8(%rsp)
movq $0x0, 0x250(%rsp)
cmpq $0x64, 0x250(%rsp)
jge 0xd821
movq 0x250(%rsp), %rcx
leaq 0x20cce4d(%rip), %rax   # 0x20da650
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movq 0x250(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x250(%rsp)
jmp 0xd7e9
movl $0x0, 0x20cce0d(%rip)   # 0x20da638
movl 0x264(%rsp), %eax
movl %eax, 0x20cce08(%rip)   # 0x20da640
movl $0xffffffff, 0x20ccdfa(%rip) # imm = 0xFFFFFFFF
movb 0x263(%rsp), %al
andb $0x1, %al
movb %al, 0x20ccdf3(%rip)    # 0x20da644
movb $0x1, 0x20cd138(%rip)   # 0x20da990
movq 0x258(%rsp), %rax
movq %rax, 0x20cd119(%rip)   # 0x20da980
testb $0x1, 0x20ccdd6(%rip)   # 0x20da644
je 0xd883
movq 0x28749(%rip), %rax     # 0x35fc0
movq (%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4540
callq 0xdc00
leaq 0x10(%rsp), %rdi
callq 0x4490
cmpl $-0x1, %eax
jne 0xd8a5
leaq 0x1fb5f(%rip), %rdi     # 0x2d3fd
movb $0x0, %al
callq 0xba80
leaq 0x10(%rsp), %rdi
movl $0x400000, %esi         # imm = 0x400000
callq 0x4510
cmpl $-0x1, %eax
jne 0xd8c7
leaq 0x1fb56(%rip), %rdi     # 0x2d416
movb $0x0, %al
callq 0xba80
movq $0x0, 0x250(%rsp)
movq 0x250(%rsp), %rax
movslq 0x29b02(%rip), %rcx     # 0x373e4
cmpq %rcx, %rax
jge 0xd93f
movq 0x250(%rsp), %rax
leaq 0x50(%rsp), %rdi
shlq $0x3, %rax
addq %rax, %rdi
movq 0x250(%rsp), %rcx
leaq 0x10(%rsp), %rsi
leaq 0x381(%rip), %rdx       # 0xdc90
callq 0x46a0
cmpl $-0x1, %eax
jne 0xd927
leaq 0x1fb17(%rip), %rdi     # 0x2d437
movb $0x0, %al
callq 0xba80
jmp 0xd929
movq 0x250(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x250(%rsp)
jmp 0xd8d3
movq $0x0, 0x250(%rsp)
movq 0x250(%rsp), %rax
movslq 0x29a8a(%rip), %rcx     # 0x373e4
cmpq %rcx, %rax
jge 0xd9a1
movq 0x250(%rsp), %rax
movq 0x50(%rsp,%rax,8), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4350
cmpl $-0x1, %eax
jne 0xd989
leaq 0x1facb(%rip), %rdi     # 0x2d44d
movb $0x0, %al
callq 0xba80
jmp 0xd98b
movq 0x250(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x250(%rsp)
jmp 0xd94b
callq 0xdc70
movq $0x0, 0x20ccfcf(%rip) # 0x20da980
movb $0x0, 0x20ccfd8(%rip)   # 0x20da990
callq 0x93c0
movsd %xmm0, (%rsp)
testb $0x1, 0x20ccc7b(%rip)   # 0x20da644
je 0xd9e0
leaq 0x1fa8f(%rip), %rdi     # 0x2d461
leaq 0x1e360(%rip), %rsi     # 0x2bd39
movb $0x0, %al
callq 0xc6b0
movsd (%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
leaq 0x1fa76(%rip), %rdi     # 0x2d468
movb $0x1, %al
callq 0xb5d0
addq $0x268, %rsp            # imm = 0x268
retq
nopw %cs:(%rax,%rax)