// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl 0x94(%rax), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jle 0x9738
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x971c
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
je 0x9657
jmp 0x970e
movl $0x1, -0x4c(%rbp)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x970c
movq -0x48(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x96ad
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x96f4
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x88(%rcx), %rcx
movq -0x30(%rbp), %rdx
movl 0x94(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movl 0x94(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x94(%rax)
jmp 0x9701
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
andl $-0x2, %ecx
movl %ecx, 0x8(%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9855
jmp 0x970e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x9633
jmp 0x971e
movq -0x48(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x48(%rbp)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x9622
cmpl $0x0, -0x4c(%rbp)
jne 0x9755
callq 0x3080
movl $0xa, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x9855
leaq -0x50(%rbp), %rdi
callq 0x35f0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jge 0x9772
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x9855
movq -0x30(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl 0x94(%rax), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x9799
jmp 0x9850
movl -0x3c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl (%rcx), %eax
je 0x97a9
jmp 0x9836
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x981d
movl -0x50(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
je 0x97d6
movl -0x50(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x981b
movq -0x48(%rbp), %rax
movq -0x30(%rbp), %rcx
movq 0x88(%rcx), %rcx
movq -0x30(%rbp), %rdx
movl 0x94(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movl 0x94(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x94(%rax)
jmp 0x9834
movl -0x50(%rbp), %ecx
movq -0x48(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %ecx
orl $0x1, %ecx
movl %ecx, 0x8(%rax)
jmp 0x9850
movq -0x48(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x48(%rbp)
movl -0x38(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x978e
jmp 0x95ff
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop