// File: /workspace/github_c_makefile_star2_O1/msteveb[P]autosetup/./jimsh0.c
// Function: Jim_RegsubCmd
int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
    int regcomp_flags = 0;
    int regexec_flags = 0;
    int opt_all = 0;
    int opt_command = 0;
    int offset = 0;
    regex_t *regex;
    const char *p;
    int result = JIM_OK;
    regmatch_t pmatch[MAX_SUB_MATCHES + 1];
    int num_matches = 0;

    int i, j, n;
    Jim_Obj *varname;
    Jim_Obj *resultObj;
    Jim_Obj *cmd_prefix = NULL;
    Jim_Obj *regcomp_obj = NULL;
    const char *source_str;
    int source_len;
    const char *replace_str = NULL;
    int replace_len;
    const char *pattern;
    int option;
    enum {
        OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_COMMAND, OPT_END
    };
    static const char * const options[] = {
        "-nocase", "-line", "-all", "-start", "-command", "--", NULL
    };

    if (argc < 4) {
      wrongNumArgs:
        Jim_WrongNumArgs(interp, 1, argv,
            "?-switch ...? exp string subSpec ?varName?");
        return JIM_ERR;
    }

    for (i = 1; i < argc; i++) {
        const char *opt = Jim_String(argv[i]);

        if (*opt != '-') {
            break;
        }
        if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
            return JIM_ERR;
        }
        if (option == OPT_END) {
            i++;
            break;
        }
        switch (option) {
            case OPT_NOCASE:
                regcomp_flags |= REG_ICASE;
                break;

            case OPT_LINE:
                regcomp_flags |= REG_NEWLINE;
                break;

            case OPT_ALL:
                opt_all = 1;
                break;

            case OPT_START:
                if (++i == argc) {
                    goto wrongNumArgs;
                }
                if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) {
                    return JIM_ERR;
                }
                break;

            case OPT_COMMAND:
                opt_command = 1;
                break;
        }
    }
    if (argc - i != 3 && argc - i != 4) {
        goto wrongNumArgs;
    }


    regcomp_obj = Jim_DuplicateObj(interp, argv[i]);
	Jim_IncrRefCount(regcomp_obj);
    regex = SetRegexpFromAny(interp, regcomp_obj, regcomp_flags);
    if (!regex) {
		Jim_DecrRefCount(interp, regcomp_obj);
        return JIM_ERR;
    }
    pattern = Jim_String(argv[i]);

    source_str = Jim_GetString(argv[i + 1], &source_len);
    if (opt_command) {
        cmd_prefix = argv[i + 2];
        if (Jim_ListLength(interp, cmd_prefix) == 0) {
            Jim_SetResultString(interp, "command prefix must be a list of at least one element", -1);
			Jim_DecrRefCount(interp, regcomp_obj);
            return JIM_ERR;
        }
        Jim_IncrRefCount(cmd_prefix);
    }
    else {
        replace_str = Jim_GetString(argv[i + 2], &replace_len);
    }
    varname = argv[i + 3];


    resultObj = Jim_NewStringObj(interp, "", 0);

    if (offset) {
        if (offset < 0) {
            offset += source_len + 1;
        }
        if (offset > source_len) {
            offset = source_len;
        }
        else if (offset < 0) {
            offset = 0;
        }
    }

    offset = utf8_index(source_str, offset);


    Jim_AppendString(interp, resultObj, source_str, offset);


    n = source_len - offset;
    p = source_str + offset;
    do {
        int match = jim_regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags);

        if (match >= REG_BADPAT) {
            char buf[100];

            jim_regerror(match, regex, buf, sizeof(buf));
            Jim_SetResultFormatted(interp, "error while matching pattern: %s", buf);
            return JIM_ERR;
        }
        if (match == REG_NOMATCH) {
            break;
        }

        num_matches++;

        Jim_AppendString(interp, resultObj, p, pmatch[0].rm_so);

        if (opt_command) {

            Jim_Obj *cmdListObj = Jim_DuplicateObj(interp, cmd_prefix);
            for (j = 0; j < MAX_SUB_MATCHES; j++) {
                if (pmatch[j].rm_so == -1) {
                    break;
                }
                else {
                    Jim_Obj *srcObj = Jim_NewStringObj(interp, p + pmatch[j].rm_so, pmatch[j].rm_eo - pmatch[j].rm_so);
                    Jim_ListAppendElement(interp, cmdListObj, srcObj);
                }
            }
            Jim_IncrRefCount(cmdListObj);

            result = Jim_EvalObj(interp, cmdListObj);
            Jim_DecrRefCount(interp, cmdListObj);
            if (result != JIM_OK) {
                goto cmd_error;
            }
            Jim_AppendString(interp, resultObj, Jim_String(Jim_GetResult(interp)), -1);
        }
        else {

            for (j = 0; j < replace_len; j++) {
                int idx;
                int c = replace_str[j];

                if (c == '&') {
                    idx = 0;
                }
                else if (c == '\\' && j < replace_len) {
                    c = replace_str[++j];
                    if ((c >= '0') && (c <= '9')) {
                        idx = c - '0';
                    }
                    else if ((c == '\\') || (c == '&')) {
                        Jim_AppendString(interp, resultObj, replace_str + j, 1);
                        continue;
                    }
                    else {
                        Jim_AppendString(interp, resultObj, replace_str + j - 1, (j == replace_len) ? 1 : 2);
                        continue;
                    }
                }
                else {
                    Jim_AppendString(interp, resultObj, replace_str + j, 1);
                    continue;
                }
                if ((idx < MAX_SUB_MATCHES) && pmatch[idx].rm_so != -1 && pmatch[idx].rm_eo != -1) {
                    Jim_AppendString(interp, resultObj, p + pmatch[idx].rm_so,
                        pmatch[idx].rm_eo - pmatch[idx].rm_so);
                }
            }
        }

        p += pmatch[0].rm_eo;
        n -= pmatch[0].rm_eo;


        if (!opt_all || n == 0) {
            break;
        }


        if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') {
            break;
        }


        if (pattern[0] == '\0' && n) {

            Jim_AppendString(interp, resultObj, p, 1);
            p++;
            n--;
        }

        if (pmatch[0].rm_eo == pmatch[0].rm_so) {

            regexec_flags = REG_NOTBOL;
        }
        else {
            regexec_flags = 0;
        }

    } while (n);

    Jim_AppendString(interp, resultObj, p, -1);

cmd_error:
    if (result == JIM_OK) {

        if (argc - i == 4) {
            result = Jim_SetVariable(interp, varname, resultObj);

            if (result == JIM_OK) {
                Jim_SetResultInt(interp, num_matches);
            }
            else {
                Jim_FreeObj(interp, resultObj);
            }
        }
        else {
            Jim_SetResult(interp, resultObj);
            result = JIM_OK;
        }
    }
    else {
        Jim_FreeObj(interp, resultObj);
    }

    if (opt_command) {
        Jim_DecrRefCount(interp, cmd_prefix);
    }

	Jim_DecrRefCount(interp, regcomp_obj);

    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movq %rdx, %r12
movq %rdi, -0x78(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x4, %esi
jl 0x8a85
movq %rsi, %rbx
movl $0x1, %r15d
movl $0x0, -0x40(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x3c(%rbp)
movslq %r15d, %r14
movq (%r12,%r14,8), %r13
cmpq $0x0, (%r13)
jne 0x88f1
movq 0x8(%r13), %rax
movq %r13, %rdi
callq *0x18(%rax)
movq (%r13), %rax
movl $0x3, %r13d
cmpb $0x2d, (%rax)
jne 0x8994
movq (%r12,%r14,8), %rsi
movq -0x78(%rbp), %rdi
leaq 0x2d37d(%rip), %rdx     # 0x35c90
leaq -0x7c(%rbp), %rcx
leaq 0x231ac(%rip), %r8      # 0x2baca
movl $0x3, %r9d
callq 0x7e97
testl %eax, %eax
je 0x8935
movl $0x1, %r13d
jmp 0x8994
movl -0x7c(%rbp), %eax
cmpq $0x5, %rax
ja 0x8991
leaq 0x1f747(%rip), %rcx     # 0x2808c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
orl $0x2, -0x40(%rbp)
jmp 0x8991
movl $0x1, -0x3c(%rbp)
jmp 0x8991
movl $0x1, -0x68(%rbp)
jmp 0x8991
incl %r15d
movl $0x2, %r13d
cmpl %ebx, %r15d
je 0x8994
movslq %r15d, %rax
movq (%r12,%rax,8), %rsi
movq -0x78(%rbp), %rdi
leaq -0x2c(%rbp), %rdx
callq 0x80b2
movl %eax, %r13d
jmp 0x8994
orl $0x1, -0x40(%rbp)
xorl %r13d, %r13d
testl %r13d, %r13d
jne 0x89ac
incl %r15d
cmpl %ebx, %r15d
jl 0x88d9
jmp 0x89c0
incl %r15d
jmp 0x8994
cmpl $0x1, %r13d
je 0x8aa3
cmpl $0x2, %r13d
je 0x8a85
subl %r15d, %ebx
movq %rbx, -0x98(%rbp)
leal -0x3(%rbx), %eax
cmpl $0x1, %eax
ja 0x8a85
movslq %r15d, %r13
movq (%r12,%r13,8), %rsi
movq -0x78(%rbp), %rbx
movq %rbx, %rdi
callq 0x8fa8
movq %rax, %r15
incl 0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
movl -0x40(%rbp), %edx
callq 0x8260
movq %rax, -0xa0(%rbp)
testq %rax, %rax
je 0x8ab8
movq %r15, -0x70(%rbp)
movq (%r12,%r13,8), %r15
cmpq $0x0, (%r15)
jne 0x8a25
movq 0x8(%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movq (%r15), %rax
movq %rax, -0x88(%rbp)
movq 0x8(%r12,%r13,8), %r15
cmpq $0x0, (%r15)
jne 0x8a44
movq 0x8(%r15), %rax
movq %r15, %rdi
callq *0x18(%rax)
movl 0x14(%r15), %eax
movq %rax, -0x50(%rbp)
movq (%r15), %r15
movq 0x10(%r12,%r13,8), %r14
cmpl $0x0, -0x3c(%rbp)
je 0x8ace
movq %rbx, %rdi
movq %r14, -0x60(%rbp)
movq -0x60(%rbp), %rsi
callq 0xe338
movq -0x60(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x8e5b
incl 0x10(%rax)
movq $0x0, -0x58(%rbp)
jmp 0x8af4
leaq 0x230a5(%rip), %rcx     # 0x2bb31
movl $0x1, %r13d
movq -0x78(%rbp), %rdi
movl $0x1, %esi
movq %r12, %rdx
callq 0x7454
movl %r13d, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x10(%r15), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r15)
movl $0x1, %r13d
jmp 0x8f8f
cmpq $0x0, (%r14)
jne 0x8ade
movq 0x8(%r14), %rax
movq %r14, %rdi
callq *0x18(%rax)
movl 0x14(%r14), %eax
movl %eax, -0x44(%rbp)
movq (%r14), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq 0x18(%r12,%r13,8), %rax
movq %rax, -0x90(%rbp)
movq %rbx, %rdi
callq 0xae58
movq %rax, %rsi
leaq 0x2f786(%rip), %rax     # 0x38298
movq %rax, (%rsi)
movl $0x0, 0x14(%rsi)
movq $0x0, 0x8(%rsi)
movl -0x2c(%rbp), %eax
testl %eax, %eax
je 0x8b53
jns 0x8b38
movq -0x50(%rbp), %rcx
addl %ecx, %eax
incl %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl %ecx, %eax
jle 0x8b48
movl %ecx, -0x2c(%rbp)
jmp 0x8b53
testl %eax, %eax
jns 0x8b53
movl $0x0, -0x2c(%rbp)
movslq -0x2c(%rbp), %r12
movq %rbx, %rdi
movq %rsi, -0x38(%rbp)
movq %r15, %rdx
movl %r12d, %ecx
callq 0x8653
movq -0x50(%rbp), %r14
subl %r12d, %r14d
addq %r12, %r15
movl $0x0, -0x64(%rbp)
xorl %r13d, %r13d
movl $0x0, -0x50(%rbp)
movl $0x32, %edx
movq -0xa0(%rbp), %rdi
movq %r15, %rsi
leaq -0x240(%rbp), %rcx
movl -0x50(%rbp), %r8d
callq 0x83a3
cmpl $0x2, %eax
jb 0x8bf0
movl %eax, %eax
leaq 0x2dd6f(%rip), %rcx     # 0x36920
movq (%rcx,%rax,8), %rcx
movl $0x64, %esi
leaq -0x2b0(%rbp), %r12
movq %r12, %rdi
leaq 0x23bb0(%rip), %rdx     # 0x2c77b
xorl %eax, %eax
callq 0x61e0
movq %rbx, %rdi
leaq 0x22f2b(%rip), %rsi     # 0x2bb07
movq %r12, %rdx
xorl %eax, %eax
callq 0x692d
movl $0x1, %ecx
jmp 0x8e05
movl $0x9, %ecx
cmpl $0x1, %eax
je 0x8e05
incl -0x64(%rbp)
movl -0x240(%rbp), %ecx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
callq 0x8653
cmpl $0x0, -0x3c(%rbp)
je 0x8ce5
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
callq 0x8fa8
movq %rax, %r12
xorl %r13d, %r13d
movslq -0x240(%rbp,%r13,8), %rax
cmpq $-0x1, %rax
je 0x8c6f
movq %rax, %rsi
addq %r15, %rsi
movl -0x23c(%rbp,%r13,8), %edx
subl %eax, %edx
movq %rbx, %rdi
callq 0x7516
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x7695
incq %r13
cmpq $0x32, %r13
jne 0x8c32
incl 0x10(%r12)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x9078
movl %eax, %r13d
movl 0x10(%r12), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r12)
cmpl $0x1, %eax
jg 0x8c9f
movq %rbx, %rdi
movq %r12, %rsi
callq 0x759a
movl $0xe, %eax
testl %r13d, %r13d
jne 0x8cd5
movq (%rbx), %r12
cmpq $0x0, (%r12)
jne 0x8cbe
movq 0x8(%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
movq (%r12), %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x8653
xorl %eax, %eax
testl %r13d, %r13d
je 0x8d90
movl %eax, %ecx
jmp 0x8e05
cmpl $0x0, -0x44(%rbp)
jle 0x8d90
xorl %r12d, %r12d
movq -0x58(%rbp), %rdi
movslq %r12d, %rdx
movzbl (%rdi,%rdx), %eax
cmpl $0x26, %eax
je 0x8d1d
cmpl $0x5c, %eax
jne 0x8d43
leaq 0x1(%rdx), %r12
movzbl 0x1(%rdi,%rdx), %eax
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x8d54
andl $0xf, %eax
jmp 0x8d1f
xorl %eax, %eax
movslq -0x240(%rbp,%rax,8), %rsi
cmpq $-0x1, %rsi
je 0x8d83
movl -0x23c(%rbp,%rax,8), %ecx
cmpl $-0x1, %ecx
je 0x8d83
movq %rsi, %rdx
addq %r15, %rdx
subl %esi, %ecx
jmp 0x8d73
addq %rdi, %rdx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %ecx
jmp 0x8d7a
addq %rdi, %rdx
incq %rdx
cmpq $0x5c, %rax
je 0x8d46
cmpl $0x26, %eax
je 0x8d46
decq %rdx
xorl %ecx, %ecx
cmpl -0x44(%rbp), %r12d
setne %cl
incl %ecx
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x8653
movq -0x58(%rbp), %rdi
incl %r12d
cmpl -0x44(%rbp), %r12d
jl 0x8cf6
movslq -0x23c(%rbp), %rax
addq %rax, %r15
subl %eax, %r14d
cmpl $0x0, -0x68(%rbp)
je 0x8e00
testl %r14d, %r14d
movl $0x9, %ecx
je 0x8e05
testb $0x1, -0x40(%rbp)
jne 0x8dbf
movq -0x88(%rbp), %rax
cmpb $0x5e, (%rax)
je 0x8e05
movq -0x88(%rbp), %rax
cmpb $0x0, (%rax)
jne 0x8de5
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq 0x8653
incq %r15
decl %r14d
movl -0x23c(%rbp), %eax
xorl %ecx, %ecx
cmpl -0x240(%rbp), %eax
sete %cl
shll $0x4, %ecx
movl %ecx, -0x50(%rbp)
xorl %ecx, %ecx
jmp 0x8e05
movl $0x9, %ecx
testl %ecx, %ecx
jne 0x8e14
testl %r14d, %r14d
jne 0x8b84
jmp 0x8e2d
cmpl $0x9, %ecx
je 0x8e2d
cmpl $0xe, %ecx
movq -0x70(%rbp), %r15
je 0x8e45
movl $0x1, %r13d
jmp 0x8aa3
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
movq %r15, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x8653
movq -0x70(%rbp), %r15
testl %r13d, %r13d
je 0x8eb5
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0x759a
jmp 0x8f64
leaq 0x22cfa(%rip), %rsi     # 0x2bb5c
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x7516
movq %rax, %r14
incl 0x10(%rax)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jg 0x8e8e
movq %rbx, %rdi
callq 0x759a
movq %r14, (%rbx)
movq -0x70(%rbp), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
movl $0x1, %r13d
cmpl $0x1, %eax
jg 0x8aa3
movq %rbx, %rdi
jmp 0x8f9e
cmpl $0x4, -0x98(%rbp)
jne 0x8eeb
movq %rbx, %rdi
movq -0x90(%rbp), %rsi
movq -0x38(%rbp), %r14
movq %r14, %rdx
callq 0x871b
testl %eax, %eax
je 0x8f14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x759a
movl $0x1, %r13d
jmp 0x8f64
movq -0x38(%rbp), %r14
incl 0x10(%r14)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jg 0x8f0c
movq %rbx, %rdi
callq 0x759a
movq %r14, (%rbx)
xorl %r13d, %r13d
jmp 0x8f64
movslq -0x64(%rbp), %r15
movq %rbx, %rdi
callq 0xae58
movq %rax, %r12
leaq 0x2d3b6(%rip), %rax     # 0x362e0
movq %rax, 0x8(%r12)
movq $0x0, (%r12)
movq %r15, 0x18(%r12)
incl 0x10(%r12)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jg 0x8f5a
movq %rbx, %rdi
callq 0x759a
movq %r12, (%rbx)
xorl %r13d, %r13d
movq -0x70(%rbp), %r15
cmpl $0x0, -0x3c(%rbp)
movq -0x60(%rbp), %rsi
je 0x8f84
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jg 0x8f84
movq %rbx, %rdi
callq 0x759a
movl 0x10(%r15), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r15)
cmpl $0x1, %eax
jg 0x8aa3
movq %rbx, %rdi
movq %r15, %rsi
callq 0x759a
jmp 0x8aa3