// File: /workspace/github_c_makefile_star2_O2/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 0x3530
movq %rax, %r15
cmpl $0x2, %ebx
je 0x5d3f
cmpl $0x3, %ebx
jne 0x5e67
movq %r14, (%rsp)
movq 0x10(%rbp), %r14
jmp 0x5d4a
movq %r14, (%rsp)
leaq 0x2f6a(%rip), %r14      # 0x8cb4
leaq 0x2547(%rip), %rdx      # 0x8298
pushq $0x30
popq %rsi
movq %r15, %rdi
movl $0x411, %ecx            # imm = 0x411
callq 0x34b0
movq %rax, %rbx
andq $0x0, 0x20(%rax)
leaq 0x20(%rax), %r13
addq $0x28, %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movups %xmm0, 0xc(%rbx)
orl $-0x1, 0x28(%rbx)
movq 0x8(%rbp), %rax
movb (%rax), %cl
movzbl (%r14), %edx
cmpl $0x61, %edx
je 0x5da6
cmpl $0x77, %edx
je 0x5da6
cmpl $0x72, %edx
jne 0x5dd8
movl $0x1, 0x10(%rbx)
jmp 0x5dad
movl $0x1, 0x14(%rbx)
movzbl 0x1(%r14), %edx
testl %edx, %edx
je 0x5e9a
cmpl $0x2b, %edx
jne 0x5dd8
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x10(%rbx)
cmpb $0x0, 0x2(%r14)
je 0x5e9a
leaq 0x2757(%rip), %rsi      # 0x8536
leaq 0x233a(%rip), %rcx      # 0x8120
movq %r12, %rdi
movq %r14, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3500
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x5e03
callq 0x3160
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x5e11
callq 0x3160
movl 0x18(%rbx), %esi
testl %esi, %esi
jle 0x5e3d
movq (%r13), %rdx
movq (%rsp), %rdi
callq 0x32a0
movq (%r13), %rsi
leaq 0x2468(%rip), %rdx      # 0x8298
movq %r15, %rdi
movl $0x491, %ecx            # imm = 0x491
callq 0x31f0
movq 0x18(%rsp), %rax
movl (%rax), %edi
cmpl $-0x1, %edi
je 0x5e4e
callq 0x3280
leaq 0x2443(%rip), %rdx      # 0x8298
movq %r15, %rdi
movq %rbx, %rsi
movl $0x496, %ecx            # imm = 0x496
callq 0x31f0
jmp 0x5e86
movq (%rbp), %rdx
leaq 0x22d1(%rip), %rsi      # 0x8143
leaq 0x26a9(%rip), %rcx      # 0x8522
movq %r12, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3500
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x7c, %cl
jne 0x5f82
incq %rax
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq 0x3520
testl %eax, %eax
jne 0x5df6
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
orl $-0x1, (%rdx)
orl $-0x1, (%rcx)
movl 0x14(%rsp), %edx
movq 0x20(%rsp), %rcx
movq %r12, %rdi
movq (%rsp), %rsi
movq %r13, %r8
pushq 0x18(%rsp)
pushq %rax
callq 0x33c0
popq %rcx
popq %rdx
movl %eax, 0x18(%rbx)
movq 0x20(%rsp), %rsi
leaq 0x2382(%rip), %rdx      # 0x8298
movq %r15, %rdi
movl $0x45c, %ecx            # imm = 0x45C
callq 0x31f0
cmpl $0x0, 0x18(%rbx)
js 0x5df6
cmpl $0x0, 0x10(%rbx)
je 0x5f4f
movl 0x10(%rsp), %edi
cmpl $-0x1, %edi
je 0x603f
leaq 0x2d6d(%rip), %rsi      # 0x8cb4
callq 0x3490
movq %rax, (%rbx)
cmpl $0x0, 0x14(%rbx)
je 0x5f7d
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x605d
xorl %r14d, %r14d
cmpq $0x0, (%rbx)
setne %r14b
leaq 0x2665(%rip), %rsi      # 0x85d9
callq 0x3490
movq %rax, (%rbx,%r14,8)
movq (%rbx), %rax
jmp 0x5fb2
cmpb $0x7e, (%rax)
jne 0x5f9f
movq %r12, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
callq 0x3560
testq %rax, %rax
je 0x5df6
movq %rax, %rdi
movq %r14, %rsi
callq 0x3510
movq %rax, (%rbx)
testq %rax, %rax
je 0x600d
movq %rax, %rdi
callq 0x33d0
movl %eax, %ebp
movq %r12, %rdi
movq (%rsp), %r15
movq %r15, %rsi
movl %eax, %edx
callq 0x3640
movq 0x8(%r15), %rax
movslq %ebp, %r14
cmpq $0x0, (%rax,%r14,8)
je 0x5fed
leaq 0x25f9(%rip), %rdi      # 0x85db
xorl %eax, %eax
callq 0x3130
movq 0x8(%r15), %rax
movq %rbx, (%rax,%r14,8)
movq (%r12), %rdi
leaq 0x2603(%rip), %rsi      # 0x85ff
xorl %ebx, %ebx
movl %r14d, %edx
xorl %eax, %eax
callq 0x3590
jmp 0x5e89
movq 0x8(%rbp), %r14
movq %r12, %rdi
callq 0x31b0
leaq 0x252c(%rip), %rsi      # 0x854c
leaq 0x21c4(%rip), %rcx      # 0x81eb
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3500
jmp 0x5df6
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x604d
callq 0x3280
leaq 0x2508(%rip), %rsi      # 0x855c
leaq 0x2521(%rip), %rdx      # 0x857c
jmp 0x606b
leaq 0x2538(%rip), %rsi      # 0x859c
leaq 0x254f(%rip), %rdx      # 0x85ba
movq %r12, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3500
jmp 0x5df6