// File: /workspace/github_c_makefile_star2_O1/haxlang[P]hax/haxCmdMZ.c
// Function: Hax_RegsubCmd
int
Hax_RegsubCmd(
    ClientData dummy,			/* Not used. */
    Hax_Interp *interp,			/* Current interpreter. */
    int argc,				/* Number of arguments. */
    char **argv				/* Argument strings. */)
{
    Interp *iPtr = (Interp *) interp;
    Hax_Memoryp *memoryp = iPtr->memoryp;
    int noCase = 0, all = 0;
    regexp *regexpPtr;
    char *string, *p, *firstChar, *newValue, **argPtr;
    int match, result, flags;
    char *src, c;

    if (argc < 5) {
	wrongNumArgs:
	Hax_AppendResult(interp, "wrong # args: should be \"", argv[0],
		" ?-nocase? ?-all? exp string subSpec varName\"", (char *) NULL);
	return HAX_ERROR;
    }
    argPtr = argv+1;
    argc--;
    while (argPtr[0][0] == '-') {
	if (strcmp(argPtr[0], "-nocase") == 0) {
	    argPtr++;
	    argc--;
	    noCase = 1;
	} else if (strcmp(argPtr[0], "-all") == 0) {
	    argPtr++;
	    argc--;
	    all = 1;
	} else {
	    break;
	}
    }
    if (argc != 4) {
	goto wrongNumArgs;
    }
    regexpPtr = HaxCompileRegexp(interp, argPtr[0]);
    if (regexpPtr == NULL) {
	return HAX_ERROR;
    }

    /*
     * Convert the string to lower case, if desired.
     */

    if (noCase) {
	char *dst;

	string = (char *) ckalloc(memoryp, (unsigned) (strlen(argPtr[1]) + 1));
	for (src = argPtr[1], dst = string; *src != 0; src++, dst++) {
	    if (isupper(*src)) {
		*dst = tolower(*src);
	    } else {
		*dst = *src;
	    }
	}
	*dst = 0;
    } else {
	string = argPtr[1];
    }

    /*
     * The following loop is to handle multiple matches within the
     * same source string;  each iteration handles one match and its
     * corresponding substitution.  If "-all" hasn't been specified
     * then the loop body only gets executed once.
     */

    flags = 0;
    for (p = string; *p != 0; ) {
	iPtr->haxRegexpError = NULL;
	match = RegExec(interp, regexpPtr, p);
	if (iPtr->haxRegexpError != NULL) {
	    Hax_AppendResult(interp, "error while matching pattern: ",
		    iPtr->haxRegexpError, (char *) NULL);
	    result = HAX_ERROR;
	    goto done;
	}
	if (!match) {
	    break;
	}

	/*
	 * Copy the portion of the source string before the match to the
	 * result variable.
	 */

	src = argPtr[1] + (regexpPtr->startp[0] - string);
	c = *src;
	*src = 0;
	newValue = Hax_SetVar(interp, argPtr[3], argPtr[1] + (p - string),
		flags);
	*src = c;
	flags = HAX_APPEND_VALUE;
	if (newValue == NULL) {
	    cantSet:
	    Hax_AppendResult(interp, "couldn't set variable \"",
		    argPtr[3], "\"", (char *) NULL);
	    result = HAX_ERROR;
	    goto done;
	}

	/*
	 * Append the subSpec argument to the variable, making appropriate
	 * substitutions.  This code is a bit hairy because of the backslash
	 * conventions and because the code saves up ranges of characters in
	 * subSpec to reduce the number of calls to Hax_SetVar.
	 */

	for (src = firstChar = argPtr[2], c = *src; c != 0; src++, c = *src) {
	    int index;

	    if (c == '&') {
		index = 0;
	    } else if (c == '\\') {
		c = src[1];
		if ((c >= '0') && (c <= '9')) {
		    index = c - '0';
		} else if ((c == '\\') || (c == '&')) {
		    *src = c;
		    src[1] = 0;
		    newValue = Hax_SetVar(interp, argPtr[3], firstChar,
			    HAX_APPEND_VALUE);
		    *src = '\\';
		    src[1] = c;
		    if (newValue == NULL) {
			goto cantSet;
		    }
		    firstChar = src+2;
		    src++;
		    continue;
		} else {
		    continue;
		}
	    } else {
		continue;
	    }
	    if (firstChar != src) {
		c = *src;
		*src = 0;
		newValue = Hax_SetVar(interp, argPtr[3], firstChar,
			HAX_APPEND_VALUE);
		*src = c;
		if (newValue == NULL) {
		    goto cantSet;
		}
	    }
	    if ((index < NSUBEXP) && (regexpPtr->startp[index] != NULL)
		    && (regexpPtr->endp[index] != NULL)) {
		char *first, *last, saved;

		first = argPtr[1] + (regexpPtr->startp[index] - string);
		last = argPtr[1] + (regexpPtr->endp[index] - string);
		saved = *last;
		*last = 0;
		newValue = Hax_SetVar(interp, argPtr[3], first,
			HAX_APPEND_VALUE);
		*last = saved;
		if (newValue == NULL) {
		    goto cantSet;
		}
	    }
	    if (*src == '\\') {
		src++;
	    }
	    firstChar = src+1;
	}
	if (firstChar != src) {
	    if (Hax_SetVar(interp, argPtr[3], firstChar,
		    HAX_APPEND_VALUE) == NULL) {
		goto cantSet;
	    }
	}
	p = regexpPtr->endp[0];
	if (!all) {
	    break;
	}
    }

    /*
     * If there were no matches at all, then return a "0" result.
     */

    if (p == string) {
	interp->result = (char *) "0";
	result = HAX_OK;
	goto done;
    }

    /*
     * Copy the portion of the source string after the last match to the
     * result variable.
     */

    if (*p != 0) {
	if (Hax_SetVar(interp, argPtr[3], p, HAX_APPEND_VALUE) == NULL) {
	    goto cantSet;
	}
    }
    interp->result = (char *) "1";
    result = HAX_OK;

    done:
    if (string != argPtr[1]) {
	ckfree(memoryp, string);
    }
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
cmpl $0x5, %edx
jl 0xa74e
movl %edx, %ebp
movq 0x280(%r14), %rax
movq %rax, 0x20(%rsp)
decl %ebp
movq 0x8(%rcx), %r12
movb $0x1, %r13b
cmpb $0x2d, (%r12)
jne 0xa438
movq %r14, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0xbef2(%rip), %r13      # 0x162da
movq %rcx, 0x18(%rsp)
movq %rcx, %r15
xorl %ebx, %ebx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4310
testl %eax, %eax
je 0xa41b
movq %r12, %rdi
leaq 0xbf72(%rip), %rsi      # 0x1637d
callq 0x4310
movl $0x1, %ecx
testl %eax, %eax
je 0xa423
jmp 0xa43f
movl $0x1, %r14d
movl %ebx, %ecx
decl %ebp
movq 0x10(%r15), %r12
addq $0x8, %r15
cmpb $0x2d, (%r12)
movl %ecx, %ebx
je 0xa3f2
jmp 0xa441
movq %rcx, %r15
movb $0x1, %bl
jmp 0xa457
movl %ebx, %ecx
testl %r14d, %r14d
sete %bl
testl %ecx, %ecx
sete %r13b
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rcx
cmpl $0x4, %ebp
jne 0xa74e
movq %r14, %rdi
movq %r12, %rsi
callq 0x40b0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0xa76c
movb %r13b, 0x7(%rsp)
movq 0x10(%r15), %rbp
testb %bl, %bl
movq %r14, 0x8(%rsp)
jne 0xa510
movq %rbp, %rdi
callq 0x41d0
leal 0x1(%rax), %esi
leaq 0xbe41(%rip), %rdx      # 0x162e2
movq 0x20(%rsp), %rdi
movl $0x104, %ecx            # imm = 0x104
callq 0x44c0
movq 0x10(%r15), %r14
movb (%r14), %bpl
movq %rax, 0x10(%rsp)
movq %rax, %r13
testb %bpl, %bpl
je 0xa501
callq 0x4640
movq %rax, %rbx
incq %r14
movq 0x10(%rsp), %r13
movq (%rbx), %rax
movsbq %bpl, %r12
testb $0x1, 0x1(%rax,%r12,2)
je 0xa4ef
callq 0x4630
movq (%rax), %rax
movb (%rax,%r12,4), %bpl
movb %bpl, (%r13)
incq %r13
movb (%r14), %bpl
incq %r14
testb %bpl, %bpl
jne 0xa4d4
movb $0x0, (%r13)
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbp
xorl %ebx, %ebx
movq %rbp, %r13
movq %rbp, 0x10(%rsp)
cmpb $0x0, (%r13)
je 0xa710
movq $0x0, 0x290(%r14)
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x4570
movq 0x290(%r14), %rdx
testq %rdx, %rdx
jne 0xa7ce
testl %eax, %eax
je 0xa710
movq %r14, %rdi
movq 0x10(%r15), %r14
movq 0x18(%rsp), %rax
movq (%rax), %r12
subq %rbp, %r12
movq %rbp, %rax
movb (%r14,%r12), %bpl
movb $0x0, (%r14,%r12)
movq 0x20(%r15), %rsi
subq %rax, %r13
addq 0x10(%r15), %r13
movq %r13, %rdx
movl %ebx, %ecx
callq 0x4450
movb %bpl, (%r14,%r12)
testq %rax, %rax
je 0xa782
movq 0x18(%r15), %r13
movq %r13, %rbx
movzbl (%rbx), %ebp
xorl %r14d, %r14d
cmpl $0x26, %ebp
je 0xa5d4
cmpl $0x5c, %ebp
je 0xa5bc
testl %ebp, %ebp
jne 0xa6b8
jmp 0xa6c9
movb 0x1(%rbx), %r12b
movzbl %r12b, %r14d
leal -0x30(%r14), %eax
cmpb $0x9, %al
ja 0xa66d
andl $0xf, %r14d
cmpq %rbx, %r13
je 0xa5fe
movb $0x0, (%rbx)
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x4450
movb %bpl, (%rbx)
testq %rax, %rax
je 0xa6b5
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rdx
testq %rdx, %rdx
je 0xa656
movq 0x18(%rsp), %rax
movq 0x50(%rax,%r14,8), %r14
testq %r14, %r14
je 0xa656
movq 0x10(%r15), %r12
movq 0x10(%rsp), %rax
subq %rax, %rdx
addq %r12, %rdx
subq %rax, %r14
movb (%r12,%r14), %bpl
movb $0x0, (%r12,%r14)
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
movl $0x2, %ecx
callq 0x4450
movb %bpl, (%r12,%r14)
testq %rax, %rax
sete %r14b
je 0xa6b8
xorl %eax, %eax
cmpb $0x5c, (%rbx)
sete %al
leaq (%rbx,%rax), %r13
incq %r13
addq %rax, %rbx
xorl %r14d, %r14d
jmp 0xa6b8
cmpl $0x5c, %r14d
je 0xa679
cmpl $0x26, %r14d
jne 0xa668
movb %r12b, (%rbx)
movb $0x0, 0x1(%rbx)
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x4450
movb $0x5c, (%rbx)
movb %r12b, 0x1(%rbx)
testq %rax, %rax
je 0xa6b5
leaq 0x1(%rbx), %rax
addq $0x2, %rbx
xorl %r14d, %r14d
movq %rbx, %r13
movq %rax, %rbx
jmp 0xa6b8
movb $0x1, %r14b
incq %rbx
testb %r14b, %r14b
je 0xa59f
jmp 0xa782
cmpq %rbx, %r13
je 0xa6ed
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x4450
testq %rax, %rax
je 0xa782
movq 0x18(%rsp), %rax
movq 0x50(%rax), %r13
movl $0x2, %ebx
cmpb $0x0, 0x7(%rsp)
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %rbp
je 0xa51a
cmpq %rbp, %r13
je 0xa7e8
cmpb $0x0, (%r13)
je 0xa73b
movq 0x20(%r15), %rsi
movq 0x8(%rsp), %rdi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x4450
testq %rax, %rax
je 0xa782
leaq 0xbbeb(%rip), %rax      # 0x1632d
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
jmp 0xa7a8
movq (%rcx), %rdx
leaq 0xb11c(%rip), %rsi      # 0x15874
leaq 0xbbf0(%rip), %rcx      # 0x1634f
movq %r14, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4500
movl $0x1, %ebx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x20(%r15), %rdx
leaq 0xbbaa(%rip), %rsi      # 0x16337
leaq 0xab1b(%rip), %rcx      # 0x152af
movq 0x8(%rsp), %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4500
movl $0x1, %ebx
movq 0x10(%rsp), %rbp
cmpq 0x10(%r15), %rbp
je 0xa771
leaq 0xbb28(%rip), %rdx      # 0x162e2
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movl $0x1a0, %ecx            # imm = 0x1A0
callq 0x4280
jmp 0xa771
leaq 0xbb18(%rip), %rsi      # 0x162ed
movq %r14, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x4500
movl $0x1, %ebx
jmp 0xa7ad
leaq 0xb79e(%rip), %rax      # 0x15f8d
movq %rax, (%r14)
xorl %ebx, %ebx
jmp 0xa7ad