// File: /workspace/github_c_makefile_star2_O3/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 $0x278, %rsp            # imm = 0x278
movq %rdx, %r15
movq %rdi, %rbx
movl $0x0, -0x34(%rbp)
cmpl $0x4, %esi
jl 0x8ad1
movq %rsi, %r13
movl $0x1, %r14d
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x0, -0x38(%rbp)
movq %rbx, -0x68(%rbp)
movslq %r14d, %rbx
movq (%r15,%rbx,8), %r12
movq (%r12), %rax
testq %rax, %rax
jne 0x8957
movq 0x8(%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
movq (%r12), %rax
cmpb $0x2d, (%rax)
jne 0x8a01
movq (%r15,%rbx,8), %rsi
movq -0x68(%rbp), %rbx
movq %rbx, %rdi
leaq 0x2c32e(%rip), %rdx     # 0x34ca0
leaq -0x74(%rbp), %rcx
leaq 0x220f5(%rip), %r8      # 0x2aa72
movl $0x3, %r9d
callq 0x7eac
movl $0x1, %r12d
testl %eax, %eax
jne 0x8aee
movl -0x74(%rbp), %eax
cmpq $0x5, %rax
ja 0x89f3
leaq 0x1e6e6(%rip), %rcx     # 0x2708c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
orl $0x2, -0x3c(%rbp)
jmp 0x89f3
movl $0x1, -0x38(%rbp)
jmp 0x89f3
movl $0x1, -0x54(%rbp)
jmp 0x89f3
incl %r14d
cmpl %r13d, %r14d
je 0x8ad1
movslq %r14d, %rax
movq (%r15,%rax,8), %rsi
movq %rbx, %rdi
leaq -0x34(%rbp), %rdx
callq 0x80ce
testl %eax, %eax
je 0x89f3
jmp 0x8aee
orl $0x1, -0x3c(%rbp)
incl %r14d
cmpl %r13d, %r14d
jl 0x8938
jmp 0x8a05
movq -0x68(%rbp), %rbx
subl %r14d, %r13d
movq %r13, -0x90(%rbp)
leal -0x3(%r13), %eax
cmpl $0x1, %eax
ja 0x8ad1
movslq %r14d, %r12
movq (%r15,%r12,8), %rsi
movq %rbx, %rdi
callq 0x8fc2
movq %rax, %r13
incl 0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
movl -0x3c(%rbp), %edx
callq 0x827b
movq %rax, -0x98(%rbp)
testq %rax, %rax
je 0x8bc1
movq (%r15,%r12,8), %r14
movq (%r14), %rax
movq %rax, -0x80(%rbp)
testq %rax, %rax
jne 0x8a70
movq 0x8(%r14), %rax
movq %r14, %rdi
callq *0x18(%rax)
movq (%r14), %rax
movq %rax, -0x80(%rbp)
movq %r12, %r14
movq 0x8(%r15,%r12,8), %r12
movq (%r12), %rax
testq %rax, %rax
jne 0x8a90
movq 0x8(%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
movq (%r12), %rax
movq %rax, -0x48(%rbp)
movl 0x14(%r12), %eax
movq %rax, -0x50(%rbp)
movq %r14, -0x30(%rbp)
movq 0x10(%r15,%r14,8), %r12
cmpl $0x0, -0x38(%rbp)
je 0x8b03
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe2fe
cmpl $0x0, 0x20(%r12)
je 0x8b8b
movq %r13, -0x60(%rbp)
incl 0x10(%r12)
xorl %r13d, %r13d
jmp 0x8b2a
leaq 0x22001(%rip), %rcx     # 0x2aad9
movl $0x1, %r12d
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rdx
callq 0x7446
movl %r12d, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, -0x60(%rbp)
movq (%r12), %r13
testq %r13, %r13
jne 0x8b1f
movq 0x8(%r12), %rax
movq %r12, %rdi
callq *0x18(%rax)
movq (%r12), %r13
movl 0x14(%r12), %eax
movl %eax, -0x40(%rbp)
xorl %r12d, %r12d
movq -0x30(%rbp), %rax
movq 0x18(%r15,%rax,8), %rax
movq %rax, -0x88(%rbp)
movq %rbx, %rdi
callq 0xae38
leaq 0x2e74f(%rip), %rcx     # 0x37298
movq %rcx, (%rax)
movl $0x0, 0x14(%rax)
movq %rax, -0x30(%rbp)
movq $0x0, 0x8(%rax)
movl -0x34(%rbp), %eax
movq $0x0, -0x70(%rbp)
movl $0x0, %r15d
testl %eax, %eax
je 0x8c11
js 0x8be6
movl %eax, %r15d
movq -0x50(%rbp), %rcx
jmp 0x8bf5
incl %r14d
jmp 0x8a05
leaq 0x21f72(%rip), %rsi     # 0x2ab04
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x7508
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 0x8bbe
movq %rbx, %rdi
callq 0x758c
movq %r14, (%rbx)
movl 0x10(%r13), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r13)
movl $0x1, %r12d
cmpl $0x1, %eax
jg 0x8aee
movq %rbx, %rdi
movq %r13, %rsi
jmp 0x8f12
movq -0x50(%rbp), %rcx
leal (%rcx,%rax), %r15d
incl %r15d
movl %r15d, -0x34(%rbp)
cmpl %ecx, %r15d
jle 0x8c02
movl %ecx, -0x34(%rbp)
movl %ecx, %r15d
jmp 0x8c11
testl %r15d, %r15d
jns 0x8c11
movl $0x0, -0x34(%rbp)
xorl %r15d, %r15d
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq -0x48(%rbp), %r14
movq %r14, %rdx
movl %r15d, %ecx
callq 0x86a0
movq -0x50(%rbp), %rax
subl %r15d, %eax
movslq %r15d, %r15
addq %r14, %r15
movq %rax, %r14
xorl %r8d, %r8d
movq %r12, -0x48(%rbp)
movl $0x32, %edx
movq -0x98(%rbp), %rdi
movq %r15, %rsi
leaq -0x230(%rbp), %rcx
callq 0x83c0
cmpl $0x2, %eax
jae 0x8f1c
cmpl $0x1, %eax
je 0x8e5c
movq %r14, -0x50(%rbp)
movl -0x230(%rbp), %ecx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x86a0
cmpl $0x0, -0x38(%rbp)
je 0x8d44
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8fc2
movq %rax, %r14
xorl %r12d, %r12d
movslq -0x230(%rbp,%r12,8), %rax
cmpq $-0x1, %rax
je 0x8cdc
movq %rax, %rsi
addq %r15, %rsi
movl -0x22c(%rbp,%r12,8), %edx
subl %eax, %edx
movq %rbx, %rdi
callq 0x7508
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x7683
incq %r12
cmpq $0x32, %r12
jne 0x8c9f
incl 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x908a
movl %eax, %r12d
movl 0x10(%r14), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r14)
cmpl $0x1, %eax
jg 0x8d09
movq %rbx, %rdi
movq %r14, %rsi
callq 0x758c
testl %r12d, %r12d
jne 0x8fad
movq (%rbx), %r14
movq (%r14), %rdx
testq %rdx, %rdx
jne 0x8d2a
movq 0x8(%r14), %rax
movq %r14, %rdi
callq *0x18(%rax)
movq (%r14), %rdx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x86a0
movq -0x48(%rbp), %r12
jmp 0x8dea
cmpl $0x0, -0x40(%rbp)
jle 0x8dea
xorl %r14d, %r14d
movslq %r14d, %rdx
movzbl (%r13,%rdx), %eax
cmpl $0x26, %eax
je 0x8d7b
cmpl $0x5c, %eax
jne 0x8da1
leaq 0x1(%rdx), %r14
movzbl 0x1(%r13,%rdx), %eax
leal -0x30(%rax), %ecx
cmpb $0x9, %cl
ja 0x8db2
andl $0xf, %eax
jmp 0x8d7d
xorl %eax, %eax
movslq -0x230(%rbp,%rax,8), %rsi
cmpq $-0x1, %rsi
je 0x8ddd
movl -0x22c(%rbp,%rax,8), %ecx
cmpl $-0x1, %ecx
je 0x8ddd
movq %rsi, %rdx
addq %r15, %rdx
subl %esi, %ecx
jmp 0x8dd1
addq %r13, %rdx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movl $0x1, %ecx
jmp 0x8dd8
addq %r13, %rdx
incq %rdx
cmpq $0x5c, %rax
je 0x8da4
cmpl $0x26, %eax
je 0x8da4
decq %rdx
xorl %ecx, %ecx
cmpl -0x40(%rbp), %r14d
setne %cl
incl %ecx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x86a0
incl %r14d
cmpl -0x40(%rbp), %r14d
jl 0x8d51
movq -0x70(%rbp), %rax
incl %eax
movq %rax, -0x70(%rbp)
movslq -0x22c(%rbp), %rax
addq %rax, %r15
cmpl $0x0, -0x54(%rbp)
je 0x8e5c
movq -0x50(%rbp), %r14
subl %eax, %r14d
je 0x8e5c
movq -0x80(%rbp), %rcx
movb (%rcx), %cl
testb $0x1, -0x3c(%rbp)
jne 0x8e1e
cmpb $0x5e, %cl
je 0x8e5c
testb %cl, %cl
jne 0x8e42
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq 0x86a0
incq %r15
decl %r14d
movl -0x22c(%rbp), %eax
xorl %r8d, %r8d
cmpl -0x230(%rbp), %eax
sete %r8b
shll $0x4, %r8d
testl %r14d, %r14d
jne 0x8c3e
movq %rbx, %rdi
movq -0x30(%rbp), %r14
movq %r14, %rsi
movq %r15, %rdx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x86a0
cmpl $0x4, -0x90(%rbp)
jne 0x8ead
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
movq %r14, %rdx
callq 0x876e
testl %eax, %eax
movq -0x60(%rbp), %r15
je 0x8f65
movq %rbx, %rdi
movq %r14, %rsi
callq 0x758c
movl $0x1, %r12d
jmp 0x8ed4
incl 0x10(%r14)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
movq -0x60(%rbp), %r15
jg 0x8ece
movq %rbx, %rdi
callq 0x758c
movq %r14, (%rbx)
xorl %r12d, %r12d
cmpl $0x0, -0x38(%rbp)
je 0x8ef8
movq -0x48(%rbp), %rdx
movl 0x10(%rdx), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rdx)
cmpl $0x1, %eax
jg 0x8ef8
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x758c
movl 0x10(%r15), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r15)
cmpl $0x1, %eax
jg 0x8aee
movq %rbx, %rdi
movq %r15, %rsi
callq 0x758c
jmp 0x8aee
movl %eax, %eax
leaq 0x2c9fb(%rip), %rcx     # 0x35920
movq (%rcx,%rax,8), %rcx
leaq 0x227f0(%rip), %rdx     # 0x2b720
leaq -0x2a0(%rbp), %r14
movl $0x64, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x61e0
leaq 0x21b62(%rip), %rsi     # 0x2aaaf
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x6923
movl $0x1, %r12d
jmp 0x8aee
movslq -0x70(%rbp), %rbx
movq -0x68(%rbp), %rdi
callq 0xae38
movq %rax, %r14
leaq 0x2c374(%rip), %rax     # 0x352f0
movq %rax, 0x8(%r14)
movq $0x0, (%r14)
movq %rbx, 0x18(%r14)
movq -0x68(%rbp), %rbx
incl 0x10(%r14)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jle 0x8ec6
jmp 0x8ece
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x758c
movq -0x60(%rbp), %r15
jmp 0x8ed4