// File: /workspace/github_c_makefile_star2_O2/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, %rdi
cmpl $0x5, %edx
jl 0x990a
movl %edx, %ebp
movq %rdi, 0x10(%rsp)
movq 0x280(%rdi), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
movb $0x1, %dl
movq %rcx, 0x18(%rsp)
movq %rcx, %r14
movb %dl, 0xf(%rsp)
movl %r13d, %ecx
movl %ebp, %r12d
movl %ecx, %r13d
movq %r14, %r15
movq 0x8(%r14), %rbx
cmpb $0x2d, (%rbx)
jne 0x987e
leal -0x1(%r12), %ebp
leaq 0x8(%r15), %r14
movq %rbx, %rdi
leaq 0xa85d(%rip), %rsi      # 0x140ba
callq 0x4330
pushq $0x1
popq %rcx
testl %eax, %eax
je 0x9838
movq %rbx, %rdi
leaq 0xa8ea(%rip), %rsi      # 0x1415d
callq 0x4330
xorl %edx, %edx
testl %eax, %eax
je 0x9831
cmpl $0x5, %r12d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
jne 0x990a
movq %rbx, %rsi
callq 0x40b0
testq %rax, %rax
je 0x9925
movq %rax, %r12
movq 0x10(%r15), %rbp
testl %r13d, %r13d
je 0x993e
movq %rbp, %rdi
callq 0x41d0
leal 0x1(%rax), %esi
leaq 0xa801(%rip), %rdx      # 0x140c2
movq 0x20(%rsp), %rdi
movl $0x104, %ecx            # imm = 0x104
callq 0x44e0
movq %rax, %rbp
movq 0x10(%r15), %r13
xorl %ebx, %ebx
movsbq (%r13,%rbx), %r14
testq %r14, %r14
je 0x9939
callq 0x4660
movq (%rax), %rax
testb $0x1, 0x1(%rax,%r14,2)
je 0x9900
callq 0x4650
movq (%rax), %rax
movb (%rax,%r14,4), %r14b
movb %r14b, (%rbp,%rbx)
incq %rbx
jmp 0x98d9
movq (%rcx), %rdx
leaq 0x9f4c(%rip), %rsi      # 0x13860
leaq 0xa814(%rip), %rcx      # 0x1412f
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4520
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x0, (%rbp,%rbx)
xorl %r14d, %r14d
movq %rbp, %r13
movq %rbp, 0x18(%rsp)
cmpb $0x0, (%r13)
je 0x9b07
movq 0x10(%rsp), %rbx
andq $0x0, 0x290(%rbx)
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4590
movq 0x290(%rbx), %rdx
testq %rdx, %rdx
jne 0x9b94
testl %eax, %eax
je 0x9b07
movq %r14, %rcx
movq 0x10(%r15), %r14
movq %rbp, %rax
movq (%r12), %rbp
subq %rax, %rbp
movb (%r14,%rbp), %bl
movb $0x0, (%r14,%rbp)
movq 0x20(%r15), %rsi
subq %rax, %r13
addq 0x10(%r15), %r13
movq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x4470
movb %bl, (%r14,%rbp)
testq %rax, %rax
je 0x9b43
movq 0x18(%r15), %rdx
movq %rdx, %rbx
movzbl (%rbx), %r13d
cmpl $0x26, %r13d
je 0x9a03
cmpl $0x5c, %r13d
je 0x99eb
testl %r13d, %r13d
jne 0x9a8f
jmp 0x9ad1
movb 0x1(%rbx), %r14b
movzbl %r14b, %ebp
leal -0x30(%rbp), %eax
cmpb $0x9, %al
ja 0x9a97
andl $0xf, %ebp
jmp 0x9a05
xorl %ebp, %ebp
cmpq %rbx, %rdx
je 0x9a2a
movb $0x0, (%rbx)
movq 0x20(%r15), %rsi
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rcx
callq 0x4470
movb %r13b, (%rbx)
testq %rax, %rax
je 0x9b43
movq (%r12,%rbp,8), %rdx
testq %rdx, %rdx
je 0x9a7c
movq 0x50(%r12,%rbp,8), %r14
testq %r14, %r14
je 0x9a7c
movq 0x10(%r15), %r13
movq 0x18(%rsp), %rax
subq %rax, %rdx
addq %r13, %rdx
subq %rax, %r14
movb (%r13,%r14), %bpl
movb $0x0, (%r13,%r14)
movq 0x20(%r15), %rsi
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rcx
callq 0x4470
movb %bpl, (%r13,%r14)
testq %rax, %rax
je 0x9b43
movb (%rbx), %r13b
xorl %eax, %eax
cmpb $0x5c, %r13b
sete %al
leaq (%rbx,%rax), %rdx
incq %rdx
addq %rax, %rbx
incq %rbx
jmp 0x99cd
cmpl $0x5c, %ebp
je 0x9aa1
cmpl $0x26, %ebp
jne 0x9a8f
movb %r14b, (%rbx)
movb $0x0, 0x1(%rbx)
movq 0x20(%r15), %rsi
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rcx
callq 0x4470
movb $0x5c, (%rbx)
movb %r14b, 0x1(%rbx)
testq %rax, %rax
je 0x9b43
movq %rbx, %rdx
incq %rbx
addq $0x2, %rdx
jmp 0x9a8f
cmpq %rbx, %rdx
je 0x9aec
movq 0x20(%r15), %rsi
movq 0x10(%rsp), %rdi
pushq $0x2
popq %rcx
callq 0x4470
testq %rax, %rax
je 0x9b43
movq 0x50(%r12), %r13
pushq $0x2
popq %rax
movl %eax, %r14d
testb $0x1, 0xf(%rsp)
movq 0x18(%rsp), %rbp
je 0x9949
cmpq %rbp, %r13
je 0x9bae
cmpb $0x0, (%r13)
je 0x9b30
movq 0x20(%r15), %rsi
pushq $0x2
popq %rcx
movq 0x10(%rsp), %rdi
movq %r13, %rdx
callq 0x4470
testq %rax, %rax
je 0x9b43
leaq 0xa5d6(%rip), %rax      # 0x1410d
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
jmp 0x9b67
movq 0x20(%r15), %rdx
leaq 0xa5c9(%rip), %rsi      # 0x14117
leaq 0x975a(%rip), %rcx      # 0x132af
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4520
pushq $0x1
popq %rbx
movq 0x18(%rsp), %rbp
cmpq 0x10(%r15), %rbp
je 0x9928
leaq 0xa545(%rip), %rdx      # 0x140c2
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movl $0x1a0, %ecx            # imm = 0x1A0
callq 0x4280
jmp 0x9928
leaq 0xa532(%rip), %rsi      # 0x140cd
movq 0x10(%rsp), %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x4520
pushq $0x1
popq %rbx
jmp 0x9b6c
leaq 0xa234(%rip), %rax      # 0x13de9
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
jmp 0x9b6c