// File: /workspace/github_c_makefile_star2_O3/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
movq %rcx, %r14
movl 0x94(%rdi), %ecx
testl %ecx, %ecx
jle 0x7144
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %rbx
movl %esi, %r13d
leaq 0x4(%rsp), %r12
movq 0x88(%rbx), %rsi
xorl %eax, %eax
movq %rsi, %rdx
testl %ebp, %ebp
jle 0x709b
movl (%rdx), %edi
xorl %r8d, %r8d
cmpl %edi, (%r15,%r8,4)
jne 0x7093
movl $0x1, %eax
testb $0x1, 0x8(%rdx)
jne 0x7156
incq %r8
cmpq %r8, %r13
jne 0x707e
addq $0xc, %rdx
leal -0x1(%rcx), %edi
cmpl $0x1, %ecx
movl %edi, %ecx
jg 0x7075
testl %eax, %eax
je 0x7144
movq %r12, %rdi
callq 0x35f0
testl %eax, %eax
js 0x719a
movslq 0x94(%rbx), %rcx
testq %rcx, %rcx
je 0x70e9
movq 0x88(%rbx), %rsi
leaq 0x8(%rsi), %rdx
movl %ecx, %edi
cmpl -0x8(%rdx), %eax
je 0x70ed
addq $0xc, %rdx
decl %edi
jne 0x70da
jmp 0x713c
xorl %ecx, %ecx
jmp 0x713c
movl (%rdx), %edi
movl 0x4(%rsp), %eax
testb $0x2, %dil
jne 0x7103
movl %eax, -0x4(%rdx)
orl $0x1, %edi
movl %edi, (%rdx)
jmp 0x713c
andl $0x7f, %eax
je 0x7117
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x713c
addq $-0x8, %rdx
leaq (%rcx,%rcx,2), %rax
movl -0x4(%rsi,%rax,4), %ecx
movl %ecx, 0x8(%rdx)
movq -0xc(%rsi,%rax,4), %rax
movq %rax, (%rdx)
movl 0x94(%rbx), %ecx
decl %ecx
movl %ecx, 0x94(%rbx)
testl %ecx, %ecx
jg 0x7069
callq 0x3080
movl $0xa, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x719a
movl 0x4(%rdx), %ecx
movl %ecx, (%r14)
movl (%rdx), %eax
andl $0x7f, %ecx
je 0x7174
shll $0x18, %ecx
addl $0x1000000, %ecx        # imm = 0x1000000
cmpl $0x2000000, %ecx        # imm = 0x2000000
jl 0x7196
movslq 0x94(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
movl -0x4(%rsi,%rcx,4), %edi
movl %edi, 0x8(%rdx)
movq -0xc(%rsi,%rcx,4), %rcx
movq %rcx, (%rdx)
decl 0x94(%rbx)
jmp 0x719a
andb $-0x2, 0x8(%rdx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq