// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %r8, 0x10(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, (%rsp)
movb (%rsi), %bpl
testb %bpl, %bpl
js 0x10f26
callq 0x4640
movq (%rax), %rax
movzbl %bpl, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x10f26
movb 0x1(%r13), %bpl
incq %r13
testb %bpl, %bpl
jns 0x10f0f
cmpb $0x22, %bpl
je 0x10f49
movzbl %bpl, %eax
movb $0x1, %r15b
xorl %r12d, %r12d
cmpl $0x7b, %eax
jne 0x10f44
incq %r13
movl $0x1, %r12d
xorl %r14d, %r14d
jmp 0x10f55
incq %r13
movb $0x1, %r14b
xorl %r12d, %r12d
xorl %r15d, %r15d
testq %rbx, %rbx
je 0x10f5d
movl %r12d, (%rbx)
leaq 0xc(%rsp), %rbx
movq %r13, %rbp
movzbl (%rbp), %eax
cmpl $0x1f, %eax
jg 0x10f8a
leal -0x9(%rax), %ecx
cmpl $0x5, %ecx
jae 0x10fec
testl %r12d, %r12d
setne %al
orb %r14b, %al
je 0x11109
incq %rbp
jmp 0x10f65
cmpl $0x5b, %eax
jle 0x10faf
cmpl $0x5c, %eax
je 0x10fd4
cmpl $0x7d, %eax
je 0x10fc0
cmpl $0x7b, %eax
jne 0x10f85
leal 0x1(%r12), %eax
testl %r12d, %r12d
cmovel %r12d, %eax
movl %eax, %r12d
jmp 0x10f85
cmpl $0x20, %eax
je 0x10f76
cmpl $0x22, %eax
jne 0x10f85
testb %r15b, %r15b
jne 0x10f85
jmp 0x11005
testl %r12d, %r12d
je 0x10f85
cmpl $0x1, %r12d
je 0x11084
decl %r12d
jmp 0x10f85
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x42c0
movslq 0xc(%rsp), %rax
addq %rax, %rbp
decq %rbp
jmp 0x10f85
testl %eax, %eax
jne 0x10f85
testl %r12d, %r12d
je 0x11100
leaq 0x6400(%rip), %rsi      # 0x17400
jmp 0x11191
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0x1103a
movq %rbp, %r14
subq %r13, %r14
callq 0x4640
testb %r15b, %r15b
je 0x110fb
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x110fb
movb 0x1(%rbp), %r15b
testb %r15b, %r15b
je 0x1115a
callq 0x4640
movq (%rax), %rax
movl $0x1, %r14d
movq (%rsp), %r12
cmpq $0x14, %r14
ja 0x1107c
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x1107c
movb 0x1(%rbp,%r14), %r15b
incq %r14
testb %r15b, %r15b
jne 0x11059
decl %r14d
jmp 0x11161
leaq 0x1(%rbp), %rbx
movsbq 0x1(%rbp), %r15
testq %r15, %r15
js 0x110b1
movq %rbp, %r14
subq %r13, %r14
callq 0x4640
testb %r15b, %r15b
je 0x110fb
movq (%rax), %rax
movzwl (%rax,%r15,2), %eax
andl $0x2000, %eax           # imm = 0x2000
jne 0x110fb
movb 0x1(%rbp), %r15b
testb %r15b, %r15b
je 0x1119e
callq 0x4640
movq (%rax), %rax
movl $0x1, %r14d
movq (%rsp), %r12
cmpq $0x14, %r14
ja 0x110f3
movsbq %r15b, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x2000, %ecx           # imm = 0x2000
jne 0x110f3
movb 0x1(%rbp,%r14), %r15b
incq %r14
testb %r15b, %r15b
jne 0x110d0
decl %r14d
jmp 0x111a5
movq %rbx, %rbp
jmp 0x1110f
testb %r15b, %r15b
je 0x1118a
movq %rbp, %r14
subq %r13, %r14
movb (%rbp), %bl
testb %bl, %bl
js 0x11139
callq 0x4640
movq (%rax), %rax
movq 0x10(%rsp), %rdx
movzbl %bl, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
je 0x1113e
movb 0x1(%rbp), %bl
incq %rbp
testb %bl, %bl
jns 0x11123
jmp 0x1113e
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rax
movq %r13, (%rax)
movq 0x20(%rsp), %rax
movq %rbp, (%rax)
xorl %eax, %eax
testq %rdx, %rdx
je 0x111ca
movq %r14, (%rdx)
jmp 0x111ca
xorl %r14d, %r14d
movq (%rsp), %r12
movq %r12, %rdi
callq 0x42d0
movq (%r12), %rdi
leaq 0x625f(%rip), %rsi      # 0x173d3
leaq 0x6247(%rip), %r8       # 0x173c2
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45d0
jmp 0x111c5
leaq 0x628c(%rip), %rsi      # 0x1741d
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x44e0
jmp 0x111c5
xorl %r14d, %r14d
movq (%rsp), %r12
movq %r12, %rdi
callq 0x42d0
movq (%r12), %rdi
leaq 0x61e0(%rip), %rsi      # 0x17398
movl %r14d, %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x45d0
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq