// File: /workspace/github_c_makefile_star2_O3/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 0xa300
movl %edx, %ebp
movq %rdi, 0x18(%rsp)
movq 0x280(%rdi), %rax
movq %rax, 0x20(%rsp)
decl %ebp
movq 0x8(%rcx), %r12
movb $0x1, %r14b
cmpb $0x2d, (%r12)
jne 0xa019
xorl %r14d, %r14d
leaq 0xb11d(%rip), %r13      # 0x150e6
movq %rcx, 0x10(%rsp)
movq %rcx, %r15
xorl %ebx, %ebx
movq %r12, %rdi
movq %r13, %rsi
callq 0x4320
testl %eax, %eax
je 0x9ffc
movq %r12, %rdi
leaq 0xb19d(%rip), %rsi      # 0x15189
callq 0x4320
movl $0x1, %ecx
testl %eax, %eax
je 0xa004
jmp 0xa020
movl $0x1, %r14d
movl %ebx, %ecx
decl %ebp
movq 0x10(%r15), %r12
addq $0x8, %r15
cmpb $0x2d, (%r12)
movl %ecx, %ebx
je 0x9fd3
jmp 0xa022
movq %rcx, %r15
movb $0x1, %bl
jmp 0xa033
movl %ebx, %ecx
testl %r14d, %r14d
sete %bl
testl %ecx, %ecx
sete %r14b
movq 0x10(%rsp), %rcx
cmpl $0x4, %ebp
movq 0x18(%rsp), %rdi
jne 0xa300
movq %r12, %rsi
callq 0x40b0
testq %rax, %rax
je 0xa31b
movb %r14b, 0x7(%rsp)
movq 0x10(%r15), %r13
testb %bl, %bl
movq %rax, 0x10(%rsp)
jne 0xa0e8
movq %r13, %rdi
callq 0x41d0
leal 0x1(%rax), %esi
leaq 0xb074(%rip), %rdx      # 0x150ee
movq 0x20(%rsp), %rdi
movl $0x104, %ecx            # imm = 0x104
callq 0x44d0
movq %rax, %rbp
movq 0x10(%r15), %r14
movb (%r14), %r13b
movq %rax, 0x8(%rsp)
testb %r13b, %r13b
je 0xa0da
callq 0x4650
movq %rax, %rbx
incq %r14
movq 0x8(%rsp), %rbp
movq (%rbx), %rax
movsbq %r13b, %r12
testb $0x1, 0x1(%rax,%r12,2)
je 0xa0c8
callq 0x4640
movq (%rax), %rax
movb (%rax,%r12,4), %r13b
movb %r13b, (%rbp)
incq %rbp
movb (%r14), %r13b
incq %r14
testb %r13b, %r13b
jne 0xa0ad
movb $0x0, (%rbp)
movq 0x8(%rsp), %r13
movq 0x10(%rsp), %rax
xorl %ebx, %ebx
movq %r13, 0x8(%rsp)
movq 0x18(%rsp), %r12
cmpb $0x0, (%r13)
je 0xa2c9
movq $0x0, 0x290(%r12)
movq %r12, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x4580
movq 0x290(%r12), %rdx
testq %rdx, %rdx
jne 0xa378
testl %eax, %eax
je 0xa2c9
movq 0x10(%r15), %r14
movq 0x10(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rsp), %rax
subq %rax, %r12
movb (%r14,%r12), %bpl
movb $0x0, (%r14,%r12)
movq 0x20(%r15), %rsi
subq %rax, %r13
addq 0x10(%r15), %r13
movq 0x18(%rsp), %rdi
movq %r13, %rdx
movl %ebx, %ecx
callq 0x4460
movb %bpl, (%r14,%r12)
movq 0x18(%rsp), %r12
testq %rax, %rax
je 0xa331
movq 0x18(%r15), %rdx
movq %rdx, %rbx
movzbl (%rbx), %r14d
cmpl $0x26, %r14d
je 0xa1ba
cmpl $0x5c, %r14d
je 0xa1a0
testl %r14d, %r14d
jne 0xa249
jmp 0xa291
movb 0x1(%rbx), %bpl
movzbl %bpl, %r13d
leal -0x30(%r13), %eax
cmpb $0x9, %al
ja 0xa251
andl $0xf, %r13d
jmp 0xa1bd
xorl %r13d, %r13d
cmpq %rbx, %rdx
je 0xa1e2
movb $0x0, (%rbx)
movq 0x20(%r15), %rsi
movq %r12, %rdi
movl $0x2, %ecx
callq 0x4460
movb %r14b, (%rbx)
testq %rax, %rax
je 0xa331
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rdx
testq %rdx, %rdx
je 0xa236
movq 0x50(%rax,%r13,8), %r13
testq %r13, %r13
je 0xa236
movq 0x10(%r15), %r14
movq 0x8(%rsp), %rax
subq %rax, %rdx
addq %r14, %rdx
subq %rax, %r13
movb (%r14,%r13), %bpl
movb $0x0, (%r14,%r13)
movq 0x20(%r15), %rsi
movq %r12, %rdi
movl $0x2, %ecx
callq 0x4460
movb %bpl, (%r14,%r13)
testq %rax, %rax
je 0xa331
movb (%rbx), %r14b
xorl %eax, %eax
cmpb $0x5c, %r14b
sete %al
leaq (%rbx,%rax), %rdx
incq %rdx
addq %rax, %rbx
incq %rbx
jmp 0xa182
cmpl $0x5c, %r13d
je 0xa25d
cmpl $0x26, %r13d
jne 0xa249
movb %bpl, (%rbx)
movb $0x0, 0x1(%rbx)
movq 0x20(%r15), %rsi
movq %r12, %rdi
movl $0x2, %ecx
callq 0x4460
movb $0x5c, (%rbx)
movb %bpl, 0x1(%rbx)
testq %rax, %rax
je 0xa331
movq %rbx, %rdx
incq %rbx
addq $0x2, %rdx
jmp 0xa249
cmpq %rbx, %rdx
je 0xa2b0
movq 0x20(%r15), %rsi
movq %r12, %rdi
movl $0x2, %ecx
callq 0x4460
testq %rax, %rax
je 0xa331
movq 0x10(%rsp), %rax
movq 0x50(%rax), %r13
movl $0x2, %ebx
cmpb $0x0, 0x7(%rsp)
je 0xa0f4
cmpq 0x8(%rsp), %r13
je 0xa38d
cmpb $0x0, (%r13)
je 0xa2f4
movq 0x20(%r15), %rsi
movq %r12, %rdi
movq %r13, %rdx
movl $0x2, %ecx
callq 0x4460
testq %rax, %rax
je 0xa331
leaq 0xae3e(%rip), %rax      # 0x15139
jmp 0xa394
movq (%rcx), %rdx
leaq 0xa556(%rip), %rsi      # 0x14860
leaq 0xae4a(%rip), %rcx      # 0x1515b
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4510
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 0xae07(%rip), %rsi      # 0x15143
leaq 0x9f6c(%rip), %rcx      # 0x142af
movq %r12, %rdi
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x4510
movl $0x1, %ebx
movq 0x8(%rsp), %rsi
cmpq 0x10(%r15), %rsi
je 0xa320
leaq 0xad87(%rip), %rdx      # 0x150ee
movq 0x20(%rsp), %rdi
movl $0x1a0, %ecx            # imm = 0x1A0
callq 0x4280
jmp 0xa320
leaq 0xad7a(%rip), %rsi      # 0x150f9
movq %r12, %rdi
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x4510
jmp 0xa350
leaq 0xaa55(%rip), %rax      # 0x14de9
movq %rax, (%r12)
xorl %ebx, %ebx
jmp 0xa355