// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movb (%rsi), %bl
testb %bl, %bl
js 0x1085e
callq 0x4650
movq (%rax), %rax
movzbl %bl, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x1085e
movb 0x1(%r13), %bl
incq %r13
testb %bl, %bl
jns 0x10849
cmpb $0x22, %bl
je 0x1087e
movzbl %bl, %eax
movb $0x1, %r12b
xorl %r14d, %r14d
cmpl $0x7b, %eax
jne 0x1087a
incq %r13
movl $0x1, %r14d
xorl %ebx, %ebx
jmp 0x10889
incq %r13
movb $0x1, %bl
xorl %r14d, %r14d
xorl %r12d, %r12d
testq %r15, %r15
je 0x10891
movl %r14d, (%r15)
movq %r13, %rbp
movb (%rbp), %r15b
movzbl %r15b, %eax
cmpl $0x1f, %eax
jg 0x108bc
leal -0x9(%rax), %ecx
cmpl $0x5, %ecx
jae 0x10922
testl %r14d, %r14d
setne %al
orb %bl, %al
je 0x1093b
incq %rbp
jmp 0x10894
cmpl $0x5b, %eax
jle 0x108e0
cmpl $0x5c, %eax
je 0x10908
cmpl $0x7d, %eax
je 0x108f4
cmpl $0x7b, %eax
jne 0x108b7
leal 0x1(%r14), %eax
testl %r14d, %r14d
cmovel %r14d, %eax
movl %eax, %r14d
jmp 0x108b7
cmpl $0x20, %eax
je 0x108a9
cmpl $0x22, %eax
jne 0x108b7
testb %r12b, %r12b
jne 0x108b7
jmp 0x10988
testl %r14d, %r14d
je 0x108b7
cmpl $0x1, %r14d
je 0x10a20
decl %r14d
jmp 0x108b7
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x42c0
movslq 0xc(%rsp), %rax
addq %rax, %rbp
decq %rbp
jmp 0x108b7
testl %eax, %eax
jne 0x108b7
testl %r14d, %r14d
je 0x10aae
leaq 0x57ce(%rip), %rsi      # 0x16104
jmp 0x10ac8
movq %rbp, %r14
subq %r13, %r14
callq 0x4650
movq (%rax), %rax
movq 0x20(%rsp), %rdx
movzbl %r15b, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x10965
movb 0x1(%rbp), %r15b
incq %rbp
testb %r15b, %r15b
jns 0x1094e
movq 0x10(%rsp), %rax
movq %r13, (%rax)
movq 0x18(%rsp), %rax
movq %rbp, (%rax)
xorl %eax, %eax
testq %rdx, %rdx
je 0x10ad8
movq %r14, (%rdx)
jmp 0x10ad8
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0x109bd
movq %rbp, %r14
subq %r13, %r14
callq 0x4650
testb %r15b, %r15b
je 0x10aa6
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x10aa6
callq 0x4650
movq (%rax), %rax
movl $0x1, %r14d
cmpq $0x14, %r14
ja 0x109ee
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x109ee
movb 0x1(%rbp,%r14), %r15b
incq %r14
testb %r15b, %r15b
jne 0x109cb
decl %r14d
movq (%rsp), %r15
movq %r15, %rdi
callq 0x42d0
movq (%r15), %rdi
leaq 0x56d0(%rip), %rsi      # 0x160d7
leaq 0x56b8(%rip), %r8       # 0x160c6
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45e0
jmp 0x10ad3
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0x10a4d
movq %rbp, %r14
subq %r13, %r14
callq 0x4650
testb %r15b, %r15b
je 0x10aa6
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x10aa6
callq 0x4650
movq (%rax), %rax
movl $0x1, %r14d
cmpq $0x14, %r14
ja 0x10a7e
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x10a7e
movb 0x1(%rbp,%r14), %r15b
incq %r14
testb %r15b, %r15b
jne 0x10a5b
decl %r14d
movq (%rsp), %r15
movq %r15, %rdi
callq 0x42d0
movq (%r15), %rdi
leaq 0x5605(%rip), %rsi      # 0x1609c
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45e0
jmp 0x10ad3
movq %rbx, %rbp
jmp 0x10941
testb %r12b, %r12b
je 0x10ac1
movq %rbp, %r14
subq %r13, %r14
xorl %r15d, %r15d
jmp 0x10941
leaq 0x5659(%rip), %rsi      # 0x16121
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x44f0
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq