// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %r12
andl $0x0, -0x34(%rbp)
cmpl $0x4, %esi
jl 0x8606
movq %rsi, %rbx
pushq $0x1
popq %r13
movl $0x0, -0x38(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl %ebx, %r13d
jge 0x856d
movslq %r13d, %r15
movq (%r14,%r15,8), %rdi
callq 0x7340
cmpb $0x2d, (%rax)
jne 0x856d
movq (%r14,%r15,8), %rsi
movq %r12, %rdi
leaq 0x28855(%rip), %rdx     # 0x30d40
leaq -0x68(%rbp), %rcx
leaq 0x1e5cc(%rip), %r8      # 0x26ac2
pushq $0x3
popq %r9
callq 0x7b44
testl %eax, %eax
jne 0x8a38
movl -0x68(%rbp), %eax
cmpq $0x5, %rax
ja 0x8562
leaq 0x1ab15(%rip), %rcx     # 0x2302c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
orl $0x2, -0x3c(%rbp)
jmp 0x8562
pushq $0x1
popq %rax
movl %eax, -0x38(%rbp)
jmp 0x8562
pushq $0x1
popq %rax
movl %eax, -0x4c(%rbp)
jmp 0x8562
incl %r13d
cmpl %ebx, %r13d
je 0x8606
movslq %r13d, %rax
movq (%r14,%rax,8), %rsi
movq %r12, %rdi
leaq -0x34(%rbp), %rdx
callq 0x7d46
testl %eax, %eax
je 0x8562
jmp 0x8a38
orl $0x1, -0x3c(%rbp)
incl %r13d
jmp 0x84bf
incl %r13d
subl %r13d, %ebx
movq %rbx, -0x80(%rbp)
leal -0x3(%rbx), %eax
cmpl $0x1, %eax
ja 0x8606
movslq %r13d, %r15
movq (%r14,%r15,8), %rsi
movq %r12, %rbx
movq %r12, %rdi
callq 0x8a95
movq %rax, %r13
incl 0x10(%rax)
movq %r12, %rdi
movq %rax, %rsi
movl -0x3c(%rbp), %edx
callq 0x7ee6
movq %rax, -0x90(%rbp)
testq %rax, %rax
je 0x8624
movq (%r14,%r15,8), %rdi
callq 0x7340
movq %rax, -0x88(%rbp)
movq 0x8(%r14,%r15,8), %rdi
leaq -0x50(%rbp), %rsi
callq 0x7fc7
movq %r15, %rcx
movq %rax, %r15
movq %rcx, -0x30(%rbp)
movq 0x10(%r14,%rcx,8), %r12
cmpl $0x0, -0x38(%rbp)
je 0x8648
movq %rbx, %rdi
movq %r12, %rsi
callq 0x8b3c
testl %eax, %eax
je 0x86a3
movq %r13, -0x58(%rbp)
incl 0x10(%r12)
xorl %r13d, %r13d
jmp 0x865e
leaq 0x1e51c(%rip), %rcx     # 0x26b29
pushq $0x1
popq %r13
movq %r12, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0x72a5
jmp 0x8a3c
movl 0x10(%r13), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r13)
cmpl $0x1, %eax
jg 0x8a38
movq %rbx, %rdi
movq %r13, %rsi
callq 0x73bf
jmp 0x8a38
movq %r13, -0x58(%rbp)
leaq -0x6c(%rbp), %rsi
movq %r12, %rdi
callq 0x7fc7
movq %rax, %r13
xorl %r12d, %r12d
movq -0x30(%rbp), %rax
movq 0x18(%r14,%rax,8), %rax
movq %rax, -0x78(%rbp)
leaq 0x20fcf(%rip), %rsi     # 0x29641
movq $0x0, -0x60(%rbp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0x7363
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %ecx
movl $0x0, %r14d
testl %ecx, %ecx
je 0x8722
movl -0x50(%rbp), %eax
js 0x86fe
movl %ecx, %r14d
jmp 0x8709
leaq 0x1e4aa(%rip), %rsi     # 0x26b54
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x7363
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 0x86d4
movq %rbx, %rdi
callq 0x73bf
movq %r14, (%rbx)
movl 0x10(%r13), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r13)
cmpl $0x1, %eax
pushq $0x1
popq %r12
jg 0x86f6
movq %rbx, %rdi
movq %r13, %rsi
callq 0x73bf
movl %r12d, %r13d
jmp 0x8a3c
leal (%rcx,%rax), %r14d
incl %r14d
movl %r14d, -0x34(%rbp)
cmpl %eax, %r14d
jle 0x8716
movl %eax, -0x34(%rbp)
movl %eax, %r14d
jmp 0x8722
testl %r14d, %r14d
jns 0x8722
andl $0x0, -0x34(%rbp)
xorl %r14d, %r14d
movq %r12, -0x48(%rbp)
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
movl %r14d, %ecx
callq 0x823a
movl -0x50(%rbp), %ecx
subl %r14d, %ecx
movslq %r14d, %rax
movl %ecx, %r14d
addq %rax, %r15
pushq $0x1
popq %r12
xorl %r8d, %r8d
movq -0x90(%rbp), %rdi
movq %r15, %rsi
pushq $0x32
popq %rdx
leaq -0x230(%rbp), %rcx
callq 0x7ff7
cmpl $0x2, %eax
jae 0x8a10
cmpl $0x1, %eax
je 0x8966
movl %r14d, -0x64(%rbp)
movl -0x230(%rbp), %ecx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
callq 0x823a
cmpl $0x0, -0x38(%rbp)
je 0x88c5
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x8a95
movq %rax, %r14
xorl %r12d, %r12d
cmpq $0x32, %r12
je 0x87ed
movslq -0x230(%rbp,%r12,8), %rax
cmpq $-0x1, %rax
je 0x87ed
movq %rax, %rsi
addq %r15, %rsi
movl -0x22c(%rbp,%r12,8), %edx
subl %eax, %edx
movq %rbx, %rdi
callq 0x7363
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x74ad
incq %r12
jmp 0x87ae
incl 0x10(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8b54
movl %eax, %r12d
movl 0x10(%r14), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r14)
cmpl $0x1, %eax
jg 0x881a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73bf
testl %r12d, %r12d
jne 0x8a7d
movq (%rbx), %rdi
callq 0x7340
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
pushq $-0x1
popq %rcx
callq 0x823a
pushq $0x1
popq %r12
movq -0x60(%rbp), %rax
incl %eax
movq %rax, -0x60(%rbp)
movslq -0x22c(%rbp), %rax
addq %rax, %r15
cmpl $0x0, -0x4c(%rbp)
je 0x8966
movl -0x64(%rbp), %r14d
subl %eax, %r14d
je 0x8966
movq -0x88(%rbp), %rcx
movb (%rcx), %cl
testb $0x1, -0x3c(%rbp)
jne 0x8884
cmpb $0x5e, %cl
je 0x8966
testb %cl, %cl
jne 0x88a6
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r15, %rdx
movl %r12d, %ecx
callq 0x823a
incq %r15
decl %r14d
movl -0x22c(%rbp), %eax
xorl %r8d, %r8d
cmpl -0x230(%rbp), %eax
sete %r8b
shll $0x4, %r8d
testl %r14d, %r14d
jne 0x874e
jmp 0x8966
xorl %r14d, %r14d
movl -0x6c(%rbp), %eax
cmpl %eax, %r14d
jge 0x8841
movslq %r14d, %rdx
movzbl (%r13,%rdx), %ecx
cmpl $0x26, %ecx
je 0x88ff
cmpl $0x5c, %ecx
jne 0x8925
leaq 0x1(%rdx), %r14
movzbl 0x1(%r13,%rdx), %ecx
leal -0x30(%rcx), %esi
cmpb $0x9, %sil
ja 0x8934
andl $0xf, %ecx
jmp 0x8901
xorl %ecx, %ecx
movslq -0x230(%rbp,%rcx,8), %rax
cmpq $-0x1, %rax
je 0x895e
movl -0x22c(%rbp,%rcx,8), %ecx
cmpl $-0x1, %ecx
je 0x895e
movq %rax, %rdx
addq %r15, %rdx
subl %eax, %ecx
jmp 0x8952
addq %r13, %rdx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movl %r12d, %ecx
jmp 0x8959
addq %r13, %rdx
incq %rdx
cmpq $0x5c, %rcx
je 0x8928
cmpl $0x26, %ecx
je 0x8928
decq %rdx
xorl %ecx, %ecx
cmpl %eax, %r14d
setne %cl
incl %ecx
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x823a
incl %r14d
jmp 0x88c8
pushq $-0x1
popq %rcx
movq %rbx, %rdi
movq -0x30(%rbp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x823a
cmpl $0x4, -0x80(%rbp)
jne 0x89ac
movq %rbx, %rdi
movq -0x78(%rbp), %rsi
movq %r14, %rdx
callq 0x8306
testl %eax, %eax
movq -0x58(%rbp), %r15
je 0x8a51
movq %rbx, %rdi
movq %r14, %rsi
callq 0x73bf
movl %r12d, %r13d
jmp 0x89d3
incl 0x10(%r14)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
movq -0x58(%rbp), %r15
jg 0x89cd
movq %rbx, %rdi
callq 0x73bf
movq %r14, (%rbx)
xorl %r13d, %r13d
movq -0x48(%rbp), %rsi
cmpl $0x0, -0x38(%rbp)
je 0x89f3
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jg 0x89f3
movq %rbx, %rdi
callq 0x73bf
movl 0x10(%r15), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%r15)
cmpl $0x1, %eax
jg 0x8a3c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x73bf
jmp 0x8a3c
leaq -0x2a0(%rbp), %r14
pushq $0x64
popq %rcx
movl %eax, %edi
movq %r14, %rdx
callq 0x81d2
leaq 0x1e0d4(%rip), %rsi     # 0x26aff
movq %rbx, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x68a7
pushq $0x1
popq %r13
movl %r13d, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movslq -0x60(%rbp), %rsi
movq %rbx, %rdi
callq 0x8212
movq %rax, %r14
incl 0x10(%rax)
movq (%rbx), %rsi
movl 0x10(%rsi), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0x10(%rsi)
cmpl $0x1, %eax
jle 0x89c5
jmp 0x89cd
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
callq 0x73bf
movl %r12d, %r13d
movq -0x58(%rbp), %r15
jmp 0x89d3