// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbp
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x3540
movq %rax, %r15
cmpl $0x2, %ebx
je 0x5e1b
cmpl $0x3, %ebx
jne 0x5f4b
movq %r14, (%rsp)
movq 0x10(%rbp), %r14
jmp 0x5e26
movq %r14, (%rsp)
leaq 0x2e8e(%rip), %r14      # 0x8cb4
leaq 0x246b(%rip), %rdx      # 0x8298
movl $0x30, %esi
movq %r15, %rdi
movl $0x411, %ecx            # imm = 0x411
callq 0x34c0
movq %rax, %rbx
leaq 0x20(%rax), %r13
movq $0x0, 0x20(%rax)
addq $0x28, %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movups %xmm0, 0xc(%rbx)
movl $0xffffffff, 0x28(%rbx) # imm = 0xFFFFFFFF
movq 0x8(%rbp), %rax
movb (%rax), %cl
movzbl (%r14), %edx
cmpl $0x61, %edx
je 0x5e8a
cmpl $0x77, %edx
je 0x5e8a
cmpl $0x72, %edx
jne 0x5ebc
movl $0x1, 0x10(%rbx)
jmp 0x5e91
movl $0x1, 0x14(%rbx)
movzbl 0x1(%r14), %edx
testl %edx, %edx
je 0x5f80
cmpl $0x2b, %edx
jne 0x5ebc
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x10(%rbx)
cmpb $0x0, 0x2(%r14)
je 0x5f80
leaq 0x2673(%rip), %rsi      # 0x8536
leaq 0x2256(%rip), %rcx      # 0x8120
movq %r12, %rdi
movq %r14, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x5ee7
callq 0x3160
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x5ef5
callq 0x3160
movl 0x18(%rbx), %esi
testl %esi, %esi
jle 0x5f21
movq (%r13), %rdx
movq (%rsp), %rdi
callq 0x32b0
movq (%r13), %rsi
leaq 0x2384(%rip), %rdx      # 0x8298
movq %r15, %rdi
movl $0x491, %ecx            # imm = 0x491
callq 0x31f0
movq 0x18(%rsp), %rax
movl (%rax), %edi
cmpl $-0x1, %edi
je 0x5f32
callq 0x3290
leaq 0x235f(%rip), %rdx      # 0x8298
movq %r15, %rdi
movq %rbx, %rsi
movl $0x496, %ecx            # imm = 0x496
callq 0x31f0
jmp 0x5f6a
movq (%rbp), %rdx
leaq 0x21ed(%rip), %rsi      # 0x8143
leaq 0x25c5(%rip), %rcx      # 0x8522
movq %r12, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
movl $0x1, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x7c, %cl
jne 0x606d
incq %rax
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq 0x3530
testl %eax, %eax
jne 0x5eda
xorl %eax, %eax
cmpl $0x0, 0x14(%rbx)
leaq 0xc(%rsp), %rcx
movq %rcx, %r9
cmoveq %rax, %r9
cmpl $0x0, 0x10(%rbx)
leaq 0x10(%rsp), %rdx
cmovneq %rdx, %rax
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %esi, (%rdx)
movl %esi, (%rcx)
movl 0x14(%rsp), %edx
movq 0x20(%rsp), %rcx
movq %r12, %rdi
movq (%rsp), %rsi
movq %r13, %r8
pushq 0x18(%rsp)
pushq %rax
callq 0x33d0
addq $0x10, %rsp
movl %eax, 0x18(%rbx)
movq 0x20(%rsp), %rsi
leaq 0x2297(%rip), %rdx      # 0x8298
movq %r15, %rdi
movl $0x45c, %ecx            # imm = 0x45C
callq 0x31f0
cmpl $0x0, 0x18(%rbx)
js 0x5eda
cmpl $0x0, 0x10(%rbx)
je 0x603a
movl 0x10(%rsp), %edi
cmpl $-0x1, %edi
je 0x612a
leaq 0x2c82(%rip), %rsi      # 0x8cb4
callq 0x34a0
movq %rax, (%rbx)
cmpl $0x0, 0x14(%rbx)
je 0x6068
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x6148
xorl %r14d, %r14d
cmpq $0x0, (%rbx)
setne %r14b
leaq 0x257a(%rip), %rsi      # 0x85d9
callq 0x34a0
movq %rax, (%rbx,%r14,8)
movq (%rbx), %rax
jmp 0x609d
cmpb $0x7e, (%rax)
jne 0x608a
movq %r12, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
callq 0x3570
testq %rax, %rax
je 0x5eda
movq %rax, %rdi
movq %r14, %rsi
callq 0x3520
movq %rax, (%rbx)
testq %rax, %rax
je 0x60f8
movq %rax, %rdi
callq 0x33e0
movl %eax, %ebp
movq %r12, %rdi
movq (%rsp), %r15
movq %r15, %rsi
movl %eax, %edx
callq 0x3650
movq 0x8(%r15), %rax
movslq %ebp, %r14
cmpq $0x0, (%rax,%r14,8)
je 0x60d8
leaq 0x250e(%rip), %rdi      # 0x85db
xorl %eax, %eax
callq 0x3130
movq 0x8(%r15), %rax
movq %rbx, (%rax,%r14,8)
movq (%r12), %rdi
leaq 0x2518(%rip), %rsi      # 0x85ff
xorl %ebx, %ebx
movl %r14d, %edx
xorl %eax, %eax
callq 0x35a0
jmp 0x5f6f
movq 0x8(%rbp), %r14
movq %r12, %rdi
callq 0x31b0
leaq 0x2441(%rip), %rsi      # 0x854c
leaq 0x20d9(%rip), %rcx      # 0x81eb
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3510
jmp 0x5eda
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x6138
callq 0x3290
leaq 0x241d(%rip), %rsi      # 0x855c
leaq 0x2436(%rip), %rdx      # 0x857c
jmp 0x6156
leaq 0x244d(%rip), %rsi      # 0x859c
leaq 0x2464(%rip), %rdx      # 0x85ba
movq %r12, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3510
jmp 0x5eda