// File: /workspace/github_c_makefile_star2_O1/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 0x4c06
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 0x4b2c
movzwl (%rsp), %ecx
testl $0xff7f, %ecx           # imm = 0xFF7F
movq %r13, %r12
je 0x4bf7
movq %r12, %rdi
leaq 0x4b21(%rip), %rbp      # 0x95df
movq %rbp, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x35a0
movl (%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x4b4f
movl %ecx, %edx
shll $0x18, %edx
addl $0x1000000, %edx        # imm = 0x1000000
cmpl $0x2000000, %edx        # imm = 0x2000000
jl 0x4b86
movl %ecx, %edi
callq 0x3640
movq %rax, %rbp
movl (%rsp), %edi
andl $0x7f, %edi
callq 0x32e0
movq %rbx, %rdi
leaq 0x4d0c(%rip), %rsi      # 0x9816
movq %r13, %rdx
movq %rax, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3490
movq %rbx, %rdi
leaq 0x4cfb(%rip), %rsi      # 0x9822
jmp 0x4bc6
movq %rbx, %rdi
callq 0x31b0
movq %rbx, %rdi
leaq 0x4ca8(%rip), %rsi      # 0x97e6
movq %rax, %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3510
jmp 0x4bf4
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 0x4c99(%rip), %rsi      # 0x980a
movq %r12, %rdx
movq %rbx, %rcx
movq %rbp, %rbx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3490
jmp 0x4bef
cmpb $0x7f, %al
jne 0x4bdc
movzbl %ah, %edi
callq 0x3640
movq %rax, %rbp
movzbl 0x1(%rsp), %edi
callq 0x32e0
movq %rbx, %rdi
leaq 0x4c88(%rip), %rsi      # 0x9831
movq %r13, %rdx
movq %rax, %rcx
movq %rbp, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3490
movq %rbx, %rdi
leaq 0x4c75(%rip), %rsi      # 0x983b
movq %rbp, %rdx
leaq 0x4ca0(%rip), %rcx      # 0x9870
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
jmp 0x4bef
movq %rbx, %rdi
leaq 0x4c67(%rip), %rsi      # 0x984d
xorl %edx, %edx
xorl %eax, %eax
callq 0x3510
movl $0x1, %ebp
movq %r13, %r12
addq $0x4, %r14
decq %r15
jne 0x4a80
jmp 0x4c08
xorl %ebp, %ebp
leaq 0x4665(%rip), %rdx      # 0x9274
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movl $0x6cb, %ecx            # imm = 0x6CB
callq 0x31f0
movl 0x4(%rsp), %r15d
testl %r15d, %r15d
js 0x4ca1
leaq 0x40(%rsp), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
movl %r15d, %edi
callq 0x32f0
testl %eax, %eax
je 0x4c99
leaq 0x40(%rsp), %r14
testl %eax, %eax
js 0x4c7b
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 0x4c48
jmp 0x4c99
movq %rbx, %rdi
callq 0x31b0
leaq 0x4be8(%rip), %rsi      # 0x9872
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 0x4cbc
decl %eax
cmpb $0xa, (%rbx,%rax)
jne 0x4cbc
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