// File: /workspace/github_c_makefile_star2_O1/haxlang[P]hax/haxUnixUtil.c
// Function: Hax_WaitPids
int
Hax_WaitPids(
    ClientData clientData,
    int numPids,		/* Number of pids to wait on:  gives size
				 * of array pointed to by pidPtr. */
    int *pidPtr,		/* Pids to wait on:  return when one of
				 * these processes exits or suspends. */
    int *statusPtr		/* Wait status is returned here. */)
{
    UnixClientData *unixClientData = (UnixClientData *) clientData;
    int i, count, pid;
    WaitInfo *waitPtr;
    int anyProcesses;
    int status;

    while (1) {
	/*
	 * Scan the table of child processes to see if one of the
	 * specified children has already exited or suspended.  If so,
	 * remove it from the table and return its status.
	 */

	anyProcesses = 0;
	for (waitPtr = unixClientData->waitTable,
		count = unixClientData->waitTableUsed;
		count > 0; waitPtr++, count--) {
	    for (i = 0; i < numPids; i++) {
		if (pidPtr[i] != waitPtr->pid) {
		    continue;
		}
		anyProcesses = 1;
		if (waitPtr->flags & WI_READY) {
		    *statusPtr = *((int *) &waitPtr->status);
		    pid = waitPtr->pid;
		    if (WIFEXITED(waitPtr->status)
			    || WIFSIGNALED(waitPtr->status)) {
			*waitPtr = unixClientData->waitTable
				[unixClientData->waitTableUsed-1];
			unixClientData->waitTableUsed--;
		    } else {
			waitPtr->flags &= ~WI_READY;
		    }
		    return pid;
		}
	    }
	}

	/*
	 * Make sure that the caller at least specified one valid
	 * process to wait for.
	 */

	if (!anyProcesses) {
	    errno = ECHILD;
	    return -1;
	}

	/*
	 * Wait for a process to exit or suspend, then update its
	 * entry in the table and go back to the beginning of the
	 * loop to see if it's one of the desired processes.
	 */

	pid = wait(&status);
	if (pid < 0) {
	    return pid;
	}
	for (waitPtr = unixClientData->waitTable,
	     count = unixClientData->waitTableUsed; ;
		waitPtr++, count--) {
	    if (count == 0) {
		break;			/* Ignore unknown processes. */
	    }
	    if (pid != waitPtr->pid) {
		continue;
	    }

	    /*
	     * If the process has been detached, then ignore anything
	     * other than an exit, and drop the entry on exit.
	     */

	    if (waitPtr->flags & WI_DETACHED) {
		if (WIFEXITED(status) || WIFSIGNALED(status)) {
		    *waitPtr = unixClientData->waitTable
			[unixClientData->waitTableUsed-1];
		    unixClientData->waitTableUsed--;
		}
	    } else {
		waitPtr->status = status;
		waitPtr->flags |= WI_READY;
	    }
	    break;
	}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x94(%rdi), %eax
testl %eax, %eax
jle 0x727c
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %rbx
movl %esi, %r13d
leaq 0x4(%rsp), %r12
movq 0x88(%rbx), %rdx
xorl %esi, %esi
movq %rdx, %rcx
testl %ebp, %ebp
jle 0x71d9
movl (%rcx), %edi
xorl %r8d, %r8d
cmpl %edi, (%r15,%r8,4)
jne 0x71d1
movl $0x1, %esi
testb $0x1, 0x8(%rcx)
jne 0x728e
incq %r8
cmpq %r8, %r13
jne 0x71bc
addq $0xc, %rcx
leal -0x1(%rax), %edi
cmpl $0x1, %eax
movl %edi, %eax
jg 0x71b3
testl %esi, %esi
je 0x727c
movq %r12, %rdi
callq 0x35f0
testl %eax, %eax
js 0x72d5
movslq 0x94(%rbx), %rdx
testq %rdx, %rdx
je 0x726e
movq 0x88(%rbx), %rsi
leaq 0x8(%rsi), %rcx
movl %edx, %edi
cmpl -0x8(%rcx), %eax
je 0x7227
addq $0xc, %rcx
decl %edi
jne 0x7218
jmp 0x726e
movl (%rcx), %edi
movl 0x4(%rsp), %eax
testb $0x2, %dil
jne 0x723d
movl %eax, -0x4(%rcx)
orl $0x1, %edi
movl %edi, (%rcx)
jmp 0x726e
andl $0x7f, %eax
je 0x7251
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x726e
addq $-0x8, %rcx
leaq (%rdx,%rdx,2), %rax
movl -0x4(%rsi,%rax,4), %edx
movl %edx, 0x8(%rcx)
movq -0xc(%rsi,%rax,4), %rax
movq %rax, (%rcx)
decl 0x94(%rbx)
movl 0x94(%rbx), %eax
testl %eax, %eax
jg 0x71a7
callq 0x3080
movl $0xa, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x72d5
movl 0x4(%rcx), %eax
movl %eax, (%r14)
movl (%rcx), %eax
movl 0x4(%rcx), %esi
andl $0x7f, %esi
je 0x72af
shll $0x18, %esi
addl $0x1000000, %esi        # imm = 0x1000000
cmpl $0x2000000, %esi        # imm = 0x2000000
jl 0x72d1
movslq 0x94(%rbx), %rsi
leaq (%rsi,%rsi,2), %rsi
movl -0x4(%rdx,%rsi,4), %edi
movl %edi, 0x8(%rcx)
movq -0xc(%rdx,%rsi,4), %rdx
movq %rdx, (%rcx)
decl 0x94(%rbx)
jmp 0x72d5
andb $-0x2, 0x8(%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq