// File: /workspace/github_c_makefile_star2_O3/haxlang[P]hax/haxUnixAZ.c
// Function: CleanupChildren
static int
CleanupChildren(
    Hax_Interp *interp,		/* Used for error messages. */
    ClientData clientData,
    int numPids,		/* Number of entries in pidPtr array. */
    int *pidPtr,		/* Array of process ids of children. */
    int errorId			/* File descriptor index for file containing
				 * stderr output from pipeline.  -1 means
				 * there isn't any stderr output. */)
{
    Hax_Memoryp *memoryp;
    int result = HAX_OK;
    int i, pid, length;
    int waitStatus;

    memoryp = Hax_GetMemoryp(interp);

    for (i = 0; i < numPids; i++) {
	pid = Hax_WaitPids(clientData, 1, &pidPtr[i], (int *) &waitStatus);
	if (pid == -1) {
	    Hax_AppendResult(interp, "error waiting for process to exit: ",
		    Hax_UnixError(interp), (char *) NULL);
	    continue;
	}

	/*
	 * Create error messages for unusual process exits.  An
	 * extra newline gets appended to each error message, but
	 * it gets removed below (in the same fashion that an
	 * extra newline in the command's output is removed).
	 */

	if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) {
	    char msg1[20], msg2[20];

	    result = HAX_ERROR;
	    sprintf(msg1, "%d", pid);
	    if (WIFEXITED(waitStatus)) {
		sprintf(msg2, "%d", WEXITSTATUS(waitStatus));
		Hax_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2,
			(char *) NULL);
	    } else if (WIFSIGNALED(waitStatus)) {
		char *p;

		p = Hax_SignalMsg((int) (WTERMSIG(waitStatus)));
		Hax_SetErrorCode(interp, "CHILDKILLED", msg1,
			Hax_SignalId((int) (WTERMSIG(waitStatus))), p,
			(char *) NULL);
		Hax_AppendResult(interp, "child killed: ", p, "\n",
			(char *) NULL);
	    } else if (WIFSTOPPED(waitStatus)) {
		char *p;

		p = Hax_SignalMsg((int) (WSTOPSIG(waitStatus)));
		Hax_SetErrorCode(interp, "CHILDSUSP", msg1,
			Hax_SignalId((int) (WSTOPSIG(waitStatus))), p, (char *) NULL);
		Hax_AppendResult(interp, "child suspended: ", p, "\n",
			(char *) NULL);
	    } else {
		Hax_AppendResult(interp,
			"child wait status didn't make sense\n",
			(char *) NULL);
	    }
	}
    }
    ckfree(memoryp, (char *) pidPtr);

    /*
     * Read the standard error file.  If there's anything there,
     * then return an error and add the file's contents to the result
     * string.
     */

    if (errorId >= 0) {
	while (1) {
#	    define BUFFER_SIZE 1000
	    char buffer[BUFFER_SIZE+1];
	    int count;

	    count = read(errorId, buffer, BUFFER_SIZE);

	    if (count == 0) {
		break;
	    }
	    if (count < 0) {
		Hax_AppendResult(interp,
			"error reading stderr output file: ",
			Hax_UnixError(interp), (char *) NULL);
		break;
	    }
	    buffer[count] = 0;
	    Hax_AppendResult(interp, buffer, (char *) NULL);
	}
	close(errorId);
    }

    /*
     * If the last character of interp->result is a newline, then remove
     * the newline character (the newline would just confuse things).
     */

    length = strlen(interp->result);
    if ((length > 0) && (interp->result[length-1] == '\n')) {
	interp->result[length-1] = '\0';
    }

    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movl %r8d, 0x4(%rsp)
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
callq 0x3540
movq %rax, 0x8(%rsp)
testl %ebp, %ebp
movq %r14, 0x10(%rsp)
jle 0x4bca
movl %ebp, %r15d
leaq 0x40(%rsp), %r12
xorl %ebp, %ebp
movq %r12, %r13
movq 0x18(%rsp), %rdi
movl $0x1, %esi
movq %r14, %rdx
movq %rsp, %rcx
callq 0x3310
cmpl $-0x1, %eax
je 0x4af0
movzwl (%rsp), %ecx
testl $0xff7f, %ecx           # imm = 0xFF7F
movq %r13, %r12
je 0x4bbb
movq %r12, %rdi
leaq 0x3b81(%rip), %rbp      # 0x8603
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x35a0
movl (%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x4b13
movl %ecx, %edx
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jl 0x4b4a
movl %ecx, %edi
callq 0x3640
movq %rax, %rbp
movl (%rsp), %edi
andl $0x7f, %edi
callq 0x32e0
movq %rbx, %rdi
leaq 0x3d6c(%rip), %rsi      # 0x883a
movq %r13, %rdx
movq %rax, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3490
movq %rbx, %rdi
leaq 0x3d5b(%rip), %rsi      # 0x8846
jmp 0x4b8a
movq %rbx, %rdi
callq 0x31b0
movq %rbx, %rdi
leaq 0x3d08(%rip), %rsi      # 0x880a
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3510
jmp 0x4bb8
movzbl %ah, %edx
movq %rbp, %rsi
movq %rbx, %rbp
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x35a0
movq %rbp, %rdi
leaq 0x3cf9(%rip), %rsi      # 0x882e
movq %r12, %rdx
movq %rbx, %rcx
movq %rbp, %rbx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3490
jmp 0x4bb3
cmpb $0x7f, %al
jne 0x4ba0
movzbl %ah, %edi
callq 0x3640
movq %rax, %rbp
movzbl 0x1(%rsp), %edi
callq 0x32e0
movq %rbx, %rdi
leaq 0x3ce8(%rip), %rsi      # 0x8855
movq %r13, %rdx
movq %rax, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3490
movq %rbx, %rdi
leaq 0x3cd5(%rip), %rsi      # 0x885f
movq %rbp, %rdx
leaq 0x3d00(%rip), %rcx      # 0x8894
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
jmp 0x4bb3
movq %rbx, %rdi
leaq 0x3cc7(%rip), %rsi      # 0x8871
xorl %edx, %edx
xorl %eax, %eax
callq 0x3510
movl $0x1, %ebp
movq %r13, %r12
addq $0x4, %r14
decq %r15
jne 0x4a44
jmp 0x4bcc
xorl %ebp, %ebp
leaq 0x36c5(%rip), %rdx      # 0x8298
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x6cb, %ecx            # imm = 0x6CB
callq 0x31f0
movl 0x4(%rsp), %r15d
testl %r15d, %r15d
js 0x4c65
leaq 0x40(%rsp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r15d, %edi
callq 0x32f0
testl %eax, %eax
je 0x4c5d
leaq 0x40(%rsp), %r14
testl %eax, %eax
js 0x4c3f
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, 0x40(%rsp,%rax)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3510
movl $0x3e8, %edx            # imm = 0x3E8
movl %r15d, %edi
movq %r14, %rsi
callq 0x32f0
testl %eax, %eax
jne 0x4c0c
jmp 0x4c5d
movq %rbx, %rdi
callq 0x31b0
leaq 0x3c48(%rip), %rsi      # 0x8896
movq %rbx, %rdi
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3510
movl %r15d, %edi
callq 0x3290
movq (%rbx), %rbx
movq %rbx, %rdi
callq 0x3190
testl %eax, %eax
jle 0x4c80
decl %eax
cmpb $0xa, (%rbx,%rax)
jne 0x4c80
movb $0x0, (%rbx,%rax)
movl %ebp, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq