// File: /workspace/github_c_makefile_star2_O1/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 0x5f38
cmpl $0x3, %ebx
jne 0x606b
movq %r14, (%rsp)
movq 0x10(%rbp), %rbx
jmp 0x5f43
movq %r14, (%rsp)
leaq 0x3d4d(%rip), %rbx      # 0x9c90
leaq 0x332a(%rip), %rdx      # 0x9274
movl $0x30, %esi
movq %r15, %rdi
movl $0x411, %ecx            # imm = 0x411
callq 0x34c0
movq %rax, %r14
leaq 0x20(%rax), %r13
movq $0x0, 0x20(%rax)
addq $0x28, %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movups %xmm0, 0xc(%r14)
movl $0xffffffff, 0x28(%r14) # imm = 0xFFFFFFFF
movq 0x8(%rbp), %rax
movb (%rax), %cl
movzbl (%rbx), %edx
cmpl $0x61, %edx
je 0x5faa
cmpl $0x77, %edx
je 0x5faa
cmpl $0x72, %edx
jne 0x5fdb
movl $0x1, 0x10(%r14)
jmp 0x5fb2
movl $0x1, 0x14(%r14)
movzbl 0x1(%rbx), %edx
testl %edx, %edx
je 0x60a0
cmpl $0x2b, %edx
jne 0x5fdb
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x10(%r14)
cmpb $0x0, 0x2(%rbx)
je 0x60a0
leaq 0x3530(%rip), %rsi      # 0x9512
leaq 0x3137(%rip), %rcx      # 0x9120
movq %r12, %rdi
movq %rbx, %rdx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
movq (%r14), %rdi
testq %rdi, %rdi
je 0x6006
callq 0x3160
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x6014
callq 0x3160
movl 0x18(%r14), %esi
testl %esi, %esi
jle 0x6041
movq (%r13), %rdx
movq (%rsp), %rdi
callq 0x32b0
movq (%r13), %rsi
leaq 0x3240(%rip), %rdx      # 0x9274
movq %r15, %rdi
movl $0x491, %ecx            # imm = 0x491
callq 0x31f0
movq 0x18(%rsp), %rax
movl (%rax), %edi
cmpl $-0x1, %edi
je 0x6052
callq 0x3290
leaq 0x321b(%rip), %rdx      # 0x9274
movq %r15, %rdi
movq %r14, %rsi
movl $0x496, %ecx            # imm = 0x496
callq 0x31f0
jmp 0x608a
movq (%rbp), %rdx
leaq 0x30cd(%rip), %rsi      # 0x9143
leaq 0x3481(%rip), %rcx      # 0x94fe
movq %r12, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3510
movl $0x1, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x7c, %cl
jne 0x618e
incq %rax
leaq 0x14(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %r12, %rdi
movq %rax, %rsi
callq 0x3530
testl %eax, %eax
jne 0x5ff9
xorl %eax, %eax
cmpl $0x0, 0x14(%r14)
leaq 0xc(%rsp), %rcx
movq %rcx, %r9
cmoveq %rax, %r9
cmpl $0x0, 0x10(%r14)
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(%r14)
movq 0x20(%rsp), %rsi
leaq 0x3150(%rip), %rdx      # 0x9274
movq %r15, %rdi
movl $0x45c, %ecx            # imm = 0x45C
callq 0x31f0
cmpl $0x0, 0x18(%r14)
js 0x5ff9
cmpl $0x0, 0x10(%r14)
je 0x615f
movl 0x10(%rsp), %edi
cmpl $-0x1, %edi
je 0x624a
leaq 0x3b39(%rip), %rsi      # 0x9c90
callq 0x34a0
movq %rax, (%r14)
cmpl $0x0, 0x14(%r14)
je 0x61be
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x6268
xorl %ebx, %ebx
cmpq $0x0, (%r14)
setne %bl
leaq 0x3432(%rip), %rsi      # 0x95b5
callq 0x34a0
movq %rax, (%r14,%rbx,8)
jmp 0x61be
cmpb $0x7e, (%rax)
jne 0x61ab
movq %r12, %rdi
movq (%rsp), %rsi
movq %rax, %rdx
callq 0x3570
testq %rax, %rax
je 0x5ff9
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3520
movq %rax, (%r14)
testq %rax, %rax
je 0x6218
movq (%r14), %rdi
callq 0x33e0
movl %eax, %ebx
movq %r12, %rdi
movq (%rsp), %r15
movq %r15, %rsi
movl %eax, %edx
callq 0x3650
movq 0x8(%r15), %rax
movslq %ebx, %rbx
cmpq $0x0, (%rax,%rbx,8)
je 0x61f5
leaq 0x33c9(%rip), %rdi      # 0x95b7
xorl %eax, %eax
callq 0x3130
movq 0x8(%r15), %rax
movq %r14, (%rax,%rbx,8)
movq (%r12), %rdi
leaq 0x33d3(%rip), %rsi      # 0x95db
xorl %ebp, %ebp
movl %ebx, %edx
xorl %eax, %eax
callq 0x35a0
jmp 0x608f
movq 0x8(%rbp), %rbx
movq %r12, %rdi
callq 0x31b0
leaq 0x32fd(%rip), %rsi      # 0x9528
leaq 0x2fb9(%rip), %rcx      # 0x91eb
movq %r12, %rdi
movq %rbx, %rdx
movq %rax, %r8
xorl %r9d, %r9d
xorl %eax, %eax
callq 0x3510
jmp 0x5ff9
movl 0xc(%rsp), %edi
cmpl $-0x1, %edi
je 0x6258
callq 0x3290
leaq 0x32d9(%rip), %rsi      # 0x9538
leaq 0x32f2(%rip), %rdx      # 0x9558
jmp 0x6276
leaq 0x3309(%rip), %rsi      # 0x9578
leaq 0x3320(%rip), %rdx      # 0x9596
movq %r12, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3510
jmp 0x5ff9