// File: /workspace/github_c_makefile_star2_O2/haxlang[P]hax/haxUtil.c
// Function: HaxFindElement
int
HaxFindElement(
    Hax_Interp *interp,		/* Interpreter to use for error reporting. */
    char *list,			/* String containing Hax list with zero
				 * or more elements (possibly in braces). */
    char **elementPtr,		/* Fill in with location of first significant
				 * character in first element of list. */
    char **nextPtr,		/* Fill in with location of character just
				 * after all white space following end of
				 * argument (i.e. next argument or end of
				 * list). */
    long int *sizePtr,		/* If non-zero, fill in with size of
				 * element. */
    int *bracePtr		/* If non-zero fill in with non-zero/zero
				 * to indicate that arg was/wasn't
				 * in braces. */)
{
    char *p;
    int openBraces = 0;
    int inQuotes = 0;
    long int size;

    /*
     * Skim off leading white space and check for an opening brace or
     * quote.   Note:  use of "isascii" below and elsewhere in this
     * procedure is a temporary hack (7/27/90) because Mx uses characters
     * with the high-order bit set for some things.  This should probably
     * be changed back eventually, or all of Hax should call isascii.
     */

    while (isascii(*list) && isspace(*list)) {
	list++;
    }
    if (*list == '{') {
	openBraces = 1;
	list++;
    } else if (*list == '"') {
	inQuotes = 1;
	list++;
    }
    if (bracePtr != 0) {
	*bracePtr = openBraces;
    }
    p = list;

    /*
     * Find the end of the element (either a space or a close brace or
     * the end of the string).
     */

    while (1) {
	switch (*p) {

	    /*
	     * Open brace: don't treat specially unless the element is
	     * in braces.  In this case, keep a nesting count.
	     */

	    case '{':
		if (openBraces != 0) {
		    openBraces++;
		}
		break;

	    /*
	     * Close brace: if element is in braces, keep nesting
	     * count and quit when the last close brace is seen.
	     */

	    case '}':
		if (openBraces == 1) {
		    char *p2;

		    size = p - list;
		    p++;
		    if ((isascii(*p) && isspace(*p)) || (*p == 0)) {
			goto done;
		    }
		    for (p2 = p; (*p2 != 0) && (!isspace(*p2)) && (p2 < p+20);
			    p2++) {
			/* null body */
		    }
		    Hax_ResetResult(interp);
		    sprintf(interp->result,
			    "list element in braces followed by \"%.*s\" instead of space",
			    (int)(p2-p), p);
		    return HAX_ERROR;
		} else if (openBraces != 0) {
		    openBraces--;
		}
		break;

	    /*
	     * Backslash:  skip over everything up to the end of the
	     * backslash sequence.
	     */

	    case '\\': {
		int sizeb;

		(void) Hax_Backslash(p, &sizeb);
		p += sizeb - 1;
		break;
	    }

	    /*
	     * Space: ignore if element is in braces or quotes;  otherwise
	     * terminate element.
	     */

	    case ' ':
	    case '\f':
	    case '\n':
	    case '\r':
	    case '\t':
	    case '\v':
		if ((openBraces == 0) && !inQuotes) {
		    size = p - list;
		    goto done;
		}
		break;

	    /*
	     * Double-quote:  if element is in quotes then terminate it.
	     */

	    case '"':
		if (inQuotes) {
		    char *p2;

		    size = p-list;
		    p++;
		    if ((isascii(*p) && isspace(*p)) || (*p == 0)) {
			goto done;
		    }
		    for (p2 = p; (*p2 != 0) && (!isspace(*p2)) && (p2 < p+20);
			    p2++) {
			/* null body */
		    }
		    Hax_ResetResult(interp);
		    sprintf(interp->result,
			    "list element in quotes followed by \"%.*s\" %s",
			    (int)(p2-p), p, "instead of space");
		    return HAX_ERROR;
		}
		break;

	    /*
	     * End of list:  terminate element.
	     */

	    case 0:
		if (openBraces != 0) {
		    Hax_SetResult(interp,
			    (char *) "unmatched open brace in list",
			    HAX_STATIC);
		    return HAX_ERROR;
		} else if (inQuotes) {
		    Hax_SetResult(interp,
			    (char *) "unmatched open quote in list",
			    HAX_STATIC);
		    return HAX_ERROR;
		}
		size = p - list;
		goto done;

	}
	p++;
    }

    done:
    while (isascii(*p) && isspace(*p)) {
	p++;
    }
    *elementPtr = list;
    *nextPtr = p;
    if (sizePtr != 0) {
	*sizePtr = size;
    }
    return HAX_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %r14
movsbq (%r13), %rbx
testq %rbx, %rbx
js 0xfad0
callq 0x4660
movq (%rax), %rax
testb $0x20, 0x1(%rax,%rbx,2)
je 0xfad0
incq %r13
jmp 0xfab2
cmpb $0x22, %bl
movq %r14, (%rsp)
je 0xfaed
movzbl %bl, %eax
cmpl $0x7b, %eax
jne 0xfafa
pushq $0x1
popq %r14
incq %r13
movb $0x1, %r12b
jmp 0xfb00
incq %r13
movb $0x1, %bl
xorl %r14d, %r14d
xorl %r12d, %r12d
jmp 0xfb02
movb $0x1, %r12b
xorl %r14d, %r14d
xorl %ebx, %ebx
testq %r15, %r15
je 0xfb0a
movl %r14d, (%r15)
movq %r13, %rbp
movb (%rbp), %r15b
movzbl %r15b, %eax
leal -0x9(%rax), %ecx
cmpl $0x5, %ecx
jb 0xfb3f
cmpl $0x20, %eax
je 0xfb3f
cmpl $0x22, %eax
je 0xfb4e
cmpl $0x5c, %eax
je 0xfb6c
cmpl $0x7b, %eax
je 0xfb86
cmpl $0x7d, %eax
je 0xfb58
testl %eax, %eax
jne 0xfb49
jmp 0xfbe5
testl %r14d, %r14d
setne %al
orb %bl, %al
je 0xfb96
incq %rbp
jmp 0xfb0d
testb %r12b, %r12b
jne 0xfb49
jmp 0xfbfa
testl %r14d, %r14d
je 0xfb49
cmpl $0x1, %r14d
je 0xfc91
decl %r14d
jmp 0xfb49
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x42c0
movslq 0xc(%rsp), %rax
addq %rax, %rbp
decq %rbp
jmp 0xfb49
leal 0x1(%r14), %eax
testl %r14d, %r14d
cmovel %r14d, %eax
movl %eax, %r14d
jmp 0xfb49
movq %rbp, %r14
subq %r13, %r14
movq 0x20(%rsp), %rbx
testb %r15b, %r15b
js 0xfbc2
callq 0x4660
movq (%rax), %rax
movzbl %r15b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0xfbc2
movb 0x1(%rbp), %r15b
incq %rbp
jmp 0xfba1
movq 0x10(%rsp), %rax
movq %r13, (%rax)
movq 0x18(%rsp), %rax
movq %rbp, (%rax)
xorl %eax, %eax
testq %rbx, %rbx
je 0xfd46
movq %r14, (%rbx)
jmp 0xfd46
testl %r14d, %r14d
je 0xfcf9
leaq 0x54df(%rip), %rsi      # 0x150d4
jmp 0xfd38
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0xfc2f
movq %rbp, %r14
subq %r13, %r14
callq 0x4660
testb %r15b, %r15b
je 0xfcf1
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0xfcf1
xorl %r14d, %r14d
testb %r15b, %r15b
je 0xfc62
leaq 0x1(%r14), %r12
callq 0x4660
cmpq $0x14, %r12
ja 0xfc62
movq (%rax), %rax
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0xfc62
movb 0x2(%rbp,%r14), %r15b
movq %r12, %r14
jmp 0xfc32
movq (%rsp), %r15
movq %r15, %rdi
callq 0x42d0
movq (%r15), %rdi
leaq 0x542f(%rip), %rsi      # 0x150a7
leaq 0x5417(%rip), %r8       # 0x15096
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45f0
jmp 0xfd43
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0xfcbe
movq %rbp, %r14
subq %r13, %r14
callq 0x4660
testb %r15b, %r15b
je 0xfcf1
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0xfcf1
xorl %r14d, %r14d
testb %r15b, %r15b
je 0xfd0c
leaq 0x1(%r14), %r12
callq 0x4660
cmpq $0x14, %r12
ja 0xfd0c
movq (%rax), %rax
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0xfd0c
movb 0x2(%rbp,%r14), %r15b
movq %r12, %r14
jmp 0xfcc1
movq %rbx, %rbp
jmp 0xfb9c
testb %r12b, %r12b
je 0xfd31
movq %rbp, %r14
subq %r13, %r14
xorl %r15d, %r15d
jmp 0xfb9c
movq (%rsp), %r15
movq %r15, %rdi
callq 0x42d0
movq (%r15), %rdi
leaq 0x534a(%rip), %rsi      # 0x1506c
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45f0
jmp 0xfd43
leaq 0x53b9(%rip), %rsi      # 0x150f1
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x4500
pushq $0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq