// File: /workspace/github_c_makefile_star2_O2/ArtifexSoftware[P]mujs/./jsstring.c
// Function: Sp_replace_regexp
static void Sp_replace_regexp(js_State *J)
{
	js_Regexp *re;
	const char *source, *s, *r;
	js_Buffer *sb = NULL;
	int n, x;
	Resub m;

	source = checkstring(J, 0);
	re = js_toregexp(J, 1);

	if (js_doregexec(J, re->prog, source, &m, 0)) {
		js_copy(J, 0);
		return;
	}

	re->last = 0;

loop:
	s = m.sub[0].sp;
	n = m.sub[0].ep - m.sub[0].sp;

	if (js_iscallable(J, 2)) {
		js_copy(J, 2);
		js_pushundefined(J);
		for (x = 0; m.sub[x].sp; ++x) /* arg 0..x: substring and subexps that matched */
			js_pushlstring(J, m.sub[x].sp, m.sub[x].ep - m.sub[x].sp);
		js_pushnumber(J, s - source); /* arg x+2: offset within search string */
		js_copy(J, 0); /* arg x+3: search string */
		js_call(J, 2 + x);
		r = js_tostring(J, -1);
		js_putm(J, &sb, source, s);
		js_puts(J, &sb, r);
		js_pop(J, 1);
	} else {
		r = js_tostring(J, 2);
		js_putm(J, &sb, source, s);
		while (*r) {
			if (*r == '$') {
				switch (*(++r)) {
				case 0: --r; /* end of string; back up */
				/* fallthrough */
				case '$': js_putc(J, &sb, '$'); break;
				case '`': js_putm(J, &sb, source, s); break;
				case '\'': js_puts(J, &sb, s + n); break;
				case '&':
					js_putm(J, &sb, s, s + n);
					break;
				case '0': case '1': case '2': case '3': case '4':
				case '5': case '6': case '7': case '8': case '9':
					x = *r - '0';
					if (r[1] >= '0' && r[1] <= '9')
						x = x * 10 + *(++r) - '0';
					if (x > 0 && x < m.nsub) {
						js_putm(J, &sb, m.sub[x].sp, m.sub[x].ep);
					} else {
						js_putc(J, &sb, '$');
						if (x > 10) {
							js_putc(J, &sb, '0' + x / 10);
							js_putc(J, &sb, '0' + x % 10);
						} else {
							js_putc(J, &sb, '0' + x);
						}
					}
					break;
				default:
					js_putc(J, &sb, '$');
					js_putc(J, &sb, *r);
					break;
				}
				++r;
			} else {
				js_putc(J, &sb, *r++);
			}
		}
	}

	if (re->flags & JS_REGEXP_G) {
		source = m.sub[0].ep;
		if (n == 0) {
			if (*source)
				js_putc(J, &sb, *source++);
			else
				goto end;
		}
		if (!js_doregexec(J, re->prog, source, &m, REG_NOTBOL))
			goto loop;
	}

end:
	js_puts(J, &sb, s + n);
	js_putc(J, &sb, 0);

	if (js_try(J)) {
		js_free(J, sb);
		js_throw(J);
	}
	js_pushstring(J, sb ? sb->s : "");
	js_endtry(J);
	js_free(J, sb);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
andq $0x0, 0x8(%rsp)
callq 0x1dda8
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xfee4
movq %rax, %r15
movq (%rax), %rsi
leaq 0x30(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x1ddd3
testl %eax, %eax
je 0x1de58
movq %rbx, %rdi
xorl %esi, %esi
callq 0xeee2
jmp 0x1e190
andw $0x0, 0x12(%r15)
movq %r15, 0x28(%rsp)
movq %r13, %r12
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %r13
subq %r14, %r13
movq %rbx, %rdi
pushq $0x2
popq %rbp
movl %ebp, %esi
callq 0xf950
testl %eax, %eax
movq %r13, 0x20(%rsp)
movq %r12, %r15
movq %r14, 0x10(%rsp)
movq %rbx, %rdi
je 0x1df98
movl %ebp, %esi
callq 0xeee2
movq %rbx, %rdi
callq 0x7336
leaq 0x40(%rsp), %r13
movq -0x8(%r13), %rsi
testq %rsi, %rsi
je 0x1decb
movl (%r13), %edx
subl %esi, %edx
movq %rbx, %rdi
callq 0xe918
incl %ebp
addq $0x10, %r13
jmp 0x1deac
movq 0x10(%rsp), %r14
movq %r14, %rax
subq %r15, %rax
cvtsi2sd %rax, %xmm0
movq %rbx, %rdi
callq 0x3f9a
movq %rbx, %rdi
xorl %esi, %esi
callq 0xeee2
movq %rbx, %rdi
movl %ebp, %esi
callq 0x110b0
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xf64d
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %rsi, %r14
callq 0xaa49
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0xaa1e
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0x3f3d
movq 0x28(%rsp), %r15
testb $0x1, 0x10(%r15)
movq 0x10(%rsp), %r14
je 0x1e122
movq 0x40(%rsp), %r13
movq 0x20(%rsp), %rcx
testl %ecx, %ecx
jne 0x1df74
movsbl (%r13), %edx
testl %edx, %edx
je 0x1e122
incq %r13
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0xa986
movq (%r15), %rsi
movq %rbx, %rdi
movq %r13, %rdx
leaq 0x30(%rsp), %rcx
pushq $0x4
popq %r8
callq 0x1ddd3
testl %eax, %eax
je 0x1de63
jmp 0x1e122
movl %ebp, %esi
callq 0xf64d
movq %rax, %rbp
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0xaa49
movslq %r13d, %rax
addq %r14, %rax
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %r14
leaq 0x2e00(%rip), %r12      # 0x20dcc
movzbl (%rbp), %eax
cmpl $0x24, %eax
je 0x1dff0
testl %eax, %eax
je 0x1df34
movsbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa986
jmp 0x1e0cf
leaq 0x1(%rbp), %r13
movzbl 0x1(%rbp), %eax
movl %eax, %ecx
addl $-0x24, %ecx
cmpl $0x15, %ecx
ja 0x1e0a5
movslq (%r12,%rcx,4), %rcx
addq %r12, %rcx
jmpq *%rcx
movsbl 0x2(%rbp), %ecx
addq $0x2, %rbp
movl %ecx, %edx
movb %cl, %sil
leal -0x30(%rsi), %ecx
imull $0xa, %eax, %esi
addl $-0x30, %eax
cmpb $0xa, %cl
movl %esi, %ecx
leal -0x210(%rcx,%rdx), %ecx
cmovbq %rbp, %r13
movl %ecx, %ebp
cmovael %eax, %ebp
testl %ebp, %ebp
jle 0x1e067
cmpl 0x30(%rsp), %ebp
jge 0x1e067
movl %ebp, %ebp
movl %ebp, %eax
shlq $0x4, %rax
leaq 0x38(%rsp), %rcx
movq (%rcx,%rax), %rdx
movq 0x8(%rcx,%rax), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaa49
jmp 0x1e0cc
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x24
popq %rdx
callq 0xa986
cmpl $0xb, %ebp
jl 0x1e09e
movzbl %bpl, %eax
movb $0xa, %cl
divb %cl
movzbl %ah, %ebp
addb $0x30, %al
movzbl %al, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa986
movl %ebp, %eax
orb $0x30, %al
movzbl %al, %edx
jmp 0x1e0c1
movl %ebp, %edx
addl $0x30, %edx
jmp 0x1e0c1
cmpl $0x60, %eax
je 0x1e10f
testl %eax, %eax
je 0x1e0ef
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x24
popq %rdx
callq 0xa986
movsbl (%r13), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa986
movq %r13, %rbp
incq %rbp
jmp 0x1dfcc
movq %rbx, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
jmp 0x1e060
movq %r13, %rbp
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x24
popq %rdx
jmp 0x1dfe6
movq %rbx, %rdi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0xaa1e
jmp 0x1e0cc
movq %r15, %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rcx
jmp 0x1e060
movq 0x20(%rsp), %rax
cltq
movq %r14, %rdx
addq %rax, %rdx
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaa1e
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xa986
movq %rbx, %rdi
callq 0xee71
movq %rax, %rdi
callq 0x3100
movq (%r14), %r14
testl %eax, %eax
jne 0x1e1a2
leaq 0x8(%r14), %rax
testq %r14, %r14
leaq 0xb220(%rip), %rsi      # 0x29391
cmovneq %rax, %rsi
movq %rbx, %rdi
callq 0xe7f7
movq %rbx, %rdi
callq 0xf5a7
movq (%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x10(%rbx)
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x10(%rbx)
movq %rbx, %rdi
callq 0x7714