// File: /workspace/github_c_makefile_star2_O0/haxlang[P]hax/haxUnixAZ.c
// Function: Hax_OpenCmd
int
Hax_OpenCmd(
    ClientData clientData,		/* Unix ClientData */
    Hax_Interp *interp,			/* Current interpreter. */
    int argc,				/* Number of arguments. */
    char **argv				/* Argument strings. */)
{
    Hax_Memoryp *memoryp;
    UnixClientData *clientDataPtr;
    int pipeline, fd;
    char *access;
    OpenFile *filePtr;

    memoryp = Hax_GetMemoryp(interp);

    if (argc == 2) {
	access = (char *) "r";
    } else if (argc == 3) {
	access = argv[2];
    } else {
	Hax_AppendResult(interp, "wrong # args: should be \"", argv[0],
		" filename ?access?\"", (char *) NULL);
	return HAX_ERROR;
    }

    filePtr = (OpenFile *) ckalloc(memoryp, sizeof(OpenFile));
    filePtr->f = NULL;
    filePtr->f2 = NULL;
    filePtr->readable = 0;
    filePtr->writable = 0;
    filePtr->numPids = 0;
    filePtr->pidPtr = NULL;
    filePtr->errorId = -1;

    /*
     * Verify the requested form of access.
     */

    pipeline = 0;
    if (argv[1][0] == '|') {
	pipeline = 1;
    }
    switch (access[0]) {
	case 'r':
	    filePtr->readable = 1;
	    break;
	case 'w':
	    filePtr->writable = 1;
	    break;
	case 'a':
	    filePtr->writable = 1;
	    break;
	default:
	    badAccess:
	    Hax_AppendResult(interp, "illegal access mode \"", access,
		    "\"", (char *) NULL);
	    goto error;
    }
    if (access[1] == '+') {
	filePtr->readable = filePtr->writable = 1;
	if (access[2] != 0) {
	    goto badAccess;
	}
    } else if (access[1] != 0) {
	goto badAccess;
    }

    /*
     * Open the file or create a process pipeline.
     */

    if (!pipeline) {
	char *fileName = argv[1];

	if (fileName[0] == '~') {
	    fileName = Hax_TildeSubst(interp, clientData, fileName);
	    if (fileName == NULL) {
		goto error;
	    }
	}
	filePtr->f = fopen(fileName, access);
	if (filePtr->f == NULL) {
	    Hax_AppendResult(interp, "couldn't open \"", argv[1],
		    "\": ", Hax_UnixError(interp), (char *) NULL);
	    goto error;
	}
    } else {
	int *inPipePtr, *outPipePtr;
	int cmdArgc, inPipe, outPipe;
	char **cmdArgv;

	if (Hax_SplitList(interp, argv[1]+1, &cmdArgc, &cmdArgv) != HAX_OK) {
	    goto error;
	}
	inPipePtr = (filePtr->writable) ? &inPipe : NULL;
	outPipePtr = (filePtr->readable) ? &outPipe : NULL;
	inPipe = outPipe = -1;
	filePtr->numPids = Hax_CreatePipeline(interp, clientData, cmdArgc,
		cmdArgv, &filePtr->pidPtr, inPipePtr, outPipePtr,
		&filePtr->errorId);
	ckfree(memoryp, (char *) cmdArgv);
	if (filePtr->numPids < 0) {
	    goto error;
	}
	if (filePtr->readable) {
	    if (outPipe == -1) {
		if (inPipe != -1) {
		    close(inPipe);
		}
		Hax_AppendResult(interp, "can't read output from command:",
			" standard output was redirected", (char *) NULL);
		goto error;
	    }
	    filePtr->f = fdopen(outPipe, "r");
	}
	if (filePtr->writable) {
	    if (inPipe == -1) {
		Hax_AppendResult(interp, "can't write input to command:",
			" standard input was redirected", (char *) NULL);
		goto error;
	    }
	    if (filePtr->f != NULL) {
		filePtr->f2 = fdopen(inPipe, "w");
	    } else {
		filePtr->f = fdopen(inPipe, "w");
	    }
	}
    }

    /*
     * Enter this new OpenFile structure in the table for the
     * interpreter.  May have to expand the table to do this.
     */

    fd = fileno(filePtr->f);
    clientDataPtr = (UnixClientData *)clientData;
    HaxMakeFileTable(interp, clientDataPtr, fd);
    if (clientDataPtr->filePtrArray[fd] != NULL) {
	Hax_Panic((char *) "Hax_OpenCmd found file already open");
    }
    clientDataPtr->filePtrArray[fd] = filePtr;
    sprintf(interp->result, "file%d", fd);
    return HAX_OK;

    error:
    if (filePtr->f != NULL) {
	fclose(filePtr->f);
    }
    if (filePtr->f2 != NULL) {
	fclose(filePtr->f2);
    }
    if (filePtr->numPids > 0) {
	Hax_DetachPids(clientData, filePtr->numPids, filePtr->pidPtr);
	ckfree(memoryp, (char *) filePtr->pidPtr);
    }
    if (filePtr->errorId != -1) {
	close(filePtr->errorId);
    }
    ckfree(memoryp, (char *) filePtr);
    return HAX_ERROR;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x18(%rbp), %rdi
callq 0x3540
movq %rax, -0x30(%rbp)
cmpl $0x2, -0x1c(%rbp)
jne 0x700a
leaq 0x4f06(%rip), %rax      # 0xbf0a
movq %rax, -0x48(%rbp)
jmp 0x7051
cmpl $0x3, -0x1c(%rbp)
jne 0x701e
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x704f
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movq (%rax), %rdx
leaq 0x4113(%rip), %rsi      # 0xb143
leaq 0x445e(%rip), %rcx      # 0xb495
xorl %eax, %eax
movl %eax, %r8d
movb $0x0, %al
callq 0x3510
movl $0x1, -0x4(%rbp)
jmp 0x7543
jmp 0x7051
movq -0x30(%rbp), %rdi
movl $0x30, %esi
leaq 0x41aa(%rip), %rdx      # 0xb20b
movl $0x411, %ecx            # imm = 0x411
callq 0x34c0
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x50(%rbp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movl $0x0, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movsbl (%rax), %eax
cmpl $0x7c, %eax
jne 0x70dc
movl $0x1, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x84(%rbp)
subl $0x61, %eax
je 0x7124
jmp 0x70f0
movl -0x84(%rbp), %eax
subl $0x72, %eax
je 0x710a
jmp 0x70fd
movl -0x84(%rbp), %eax
subl $0x77, %eax
je 0x7117
jmp 0x7131
movq -0x50(%rbp), %rax
movl $0x1, 0x10(%rax)
jmp 0x715a
movq -0x50(%rbp), %rax
movl $0x1, 0x14(%rax)
jmp 0x715a
movq -0x50(%rbp), %rax
movl $0x1, 0x14(%rax)
jmp 0x715a
jmp 0x7133
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rdx
leaq 0x4367(%rip), %rsi      # 0xb4a9
leaq 0x3fd7(%rip), %rcx      # 0xb120
xorl %eax, %eax
movl %eax, %r8d
movb $0x0, %al
callq 0x3510
jmp 0x74a0
movq -0x48(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x2b, %eax
jne 0x718e
movq -0x50(%rbp), %rax
movl $0x1, 0x14(%rax)
movq -0x50(%rbp), %rax
movl $0x1, 0x10(%rax)
movq -0x48(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x0, %eax
je 0x718c
jmp 0x7133
jmp 0x719f
movq -0x48(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x0, %eax
je 0x719d
jmp 0x7133
jmp 0x719f
cmpl $0x0, -0x3c(%rbp)
jne 0x725d
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x7e, %eax
jne 0x71e4
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x3570
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x71e2
jmp 0x74a0
jmp 0x71e4
movq -0x58(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3520
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x7258
movq -0x18(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rdi
callq 0x31b0
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rdx
movq %rax, %r8
leaq 0x427f(%rip), %rsi      # 0xb4bf
leaq 0x3fa4(%rip), %rcx      # 0xb1eb
xorl %eax, %eax
movl %eax, %r9d
movb $0x0, %al
callq 0x3510
jmp 0x74a0
jmp 0x7420
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x1, %rsi
leaq -0x6c(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0x3530
cmpl $0x0, %eax
je 0x7284
jmp 0x74a0
movq -0x50(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x729b
leaq -0x70(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x72a6
xorl %eax, %eax
movq %rax, -0xa0(%rbp)
jmp 0x72a6
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x72c8
leaq -0x74(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x72d3
xorl %eax, %eax
movq %rax, -0xa8(%rbp)
jmp 0x72d3
movq -0xa8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x70(%rbp) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x6c(%rbp), %edx
movq -0x80(%rbp), %rcx
movq -0x50(%rbp), %r8
addq $0x20, %r8
movq -0x60(%rbp), %r9
movq -0x68(%rbp), %r10
movq -0x50(%rbp), %rax
addq $0x28, %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x33d0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x30(%rbp), %rdi
movq -0x80(%rbp), %rsi
leaq 0x3ed2(%rip), %rdx      # 0xb20b
movl $0x45c, %ecx            # imm = 0x45C
callq 0x31f0
movq -0x50(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jge 0x7352
jmp 0x74a0
movq -0x50(%rbp), %rax
cmpl $0x0, 0x10(%rax)
je 0x73ab
cmpl $-0x1, -0x74(%rbp)
jne 0x7392
cmpl $-0x1, -0x70(%rbp)
je 0x7370
movl -0x70(%rbp), %edi
callq 0x3290
movq -0x18(%rbp), %rdi
leaq 0x4154(%rip), %rsi      # 0xb4cf
leaq 0x416d(%rip), %rdx      # 0xb4ef
xorl %eax, %eax
movl %eax, %ecx
movb $0x0, %al
callq 0x3510
jmp 0x74a0
movl -0x74(%rbp), %edi
leaq 0x4b6e(%rip), %rsi      # 0xbf0a
callq 0x34a0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
cmpl $0x0, 0x14(%rax)
je 0x741e
cmpl $-0x1, -0x70(%rbp)
jne 0x73dd
movq -0x18(%rbp), %rdi
leaq 0x4149(%rip), %rsi      # 0xb50f
leaq 0x4160(%rip), %rdx      # 0xb52d
xorl %eax, %eax
movl %eax, %ecx
movb $0x0, %al
callq 0x3510
jmp 0x74a0
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
je 0x7403
movl -0x70(%rbp), %edi
leaq 0x415b(%rip), %rsi      # 0xb54c
callq 0x34a0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x741c
movl -0x70(%rbp), %edi
leaq 0x413f(%rip), %rsi      # 0xb54c
callq 0x34a0
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, (%rax)
jmp 0x741e
jmp 0x7420
movq -0x50(%rbp), %rax
movq (%rax), %rdi
callq 0x33e0
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0x40(%rbp), %edx
callq 0x3650
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x40(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x7468
leaq 0x40ed(%rip), %rdi      # 0xb54e
movb $0x0, %al
callq 0x3130
movq -0x50(%rbp), %rdx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x40(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl -0x40(%rbp), %edx
leaq 0x40e5(%rip), %rsi      # 0xb572
movb $0x0, %al
callq 0x35a0
movl $0x0, -0x4(%rbp)
jmp 0x7543
movq -0x50(%rbp), %rax
cmpq $0x0, (%rax)
je 0x74b6
movq -0x50(%rbp), %rax
movq (%rax), %rdi
callq 0x3160
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x74ce
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3160
movq -0x50(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jle 0x750d
movq -0x10(%rbp), %rdi
movq -0x50(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rdx
callq 0x32b0
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rax
movq 0x20(%rax), %rsi
leaq 0x3d08(%rip), %rdx      # 0xb20b
movl $0x491, %ecx            # imm = 0x491
callq 0x31f0
movq -0x50(%rbp), %rax
cmpl $-0x1, 0x28(%rax)
je 0x7523
movq -0x50(%rbp), %rax
movl 0x28(%rax), %edi
callq 0x3290
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
leaq 0x3cd9(%rip), %rdx      # 0xb20b
movl $0x496, %ecx            # imm = 0x496
callq 0x31f0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nop