// File: /workspace/github_c_makefile_star2_O1/ccxvii[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 $0x148, %rsp            # imm = 0x148
movq %rdi, %rbx
movq $0x0, 0x18(%rsp)
callq 0x2925f
movq %rax, %r13
movq %rbx, %rdi
movl $0x1, %esi
callq 0x15720
movq %rax, %r15
movq (%rax), %rsi
leaq 0x40(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rdx
xorl %r8d, %r8d
callq 0x292cf
testl %eax, %eax
je 0x293ab
movq %rbx, %rdi
xorl %esi, %esi
callq 0x14073
jmp 0x298a5
movq %r15, 0x20(%rsp)
movw $0x0, 0x12(%r15)
leaq 0x18(%rsp), %r14
movq %r13, %r15
movq 0x48(%rsp), %rbp
movq 0x50(%rsp), %rax
subq %rbp, %rax
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x14e17
testl %eax, %eax
movq %r13, 0x8(%rsp)
je 0x29573
movl $0x2, %r13d
movq %rbx, %rdi
movl %r13d, %esi
callq 0x14073
movq %rbx, %rdi
callq 0x89b6
movq 0x48(%rsp), %rcx
testq %rcx, %rcx
je 0x2944a
movl $0x2, %edx
leaq 0x58(%rsp), %rax
movq %rcx, %rsi
movl %edx, %r15d
movq %rax, %r12
movl -0x8(%rax), %edx
subl %esi, %edx
movq %rbx, %rdi
callq 0x13802
movq (%r12), %rcx
incl %r15d
addq $0x10, %r12
movq %r12, %rax
movl %r15d, %r13d
movl %r15d, %edx
movq %rcx, %rsi
testq %rcx, %rcx
jne 0x2941b
movq %rbp, %rax
movq 0x8(%rsp), %r15
subq %r15, %rax
cvtsi2sd %rax, %xmm0
movq %rbx, %rdi
callq 0x4184
movq %rbx, %rdi
xorl %esi, %esi
callq 0x14073
movq %rbx, %rdi
movl %r13d, %esi
callq 0x16c25
movslq 0x140(%rbx), %rax
leaq 0x16f73(%rip), %rsi     # 0x403f8
testq %rax, %rax
jle 0x2949d
movq 0x148(%rbx), %rcx
shlq $0x4, %rax
leaq (%rcx,%rax), %rsi
addq $-0x10, %rsi
movq %rbx, %rdi
callq 0x1533b
movq %rax, %r13
cmpq %rbp, %r15
jae 0x294cc
movq %r15, %rax
movsbl (%rax), %edx
movq %rax, %r15
incq %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movq %r15, %rax
cmpq %rbp, %r15
jne 0x294b0
movb (%r13), %cl
testb %cl, %cl
je 0x294f2
incq %r13
movsbl %cl, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movb (%r13), %dl
incq %r13
movl %edx, %ecx
testb %dl, %dl
jne 0x294d7
movl 0x140(%rbx), %ecx
movl 0x144(%rbx), %eax
movl %ecx, %edx
decl %edx
movl %edx, 0x140(%rbx)
cmpl %eax, %ecx
jle 0x298b7
movq 0x20(%rsp), %rax
testb $0x1, 0x10(%rax)
je 0x2980b
movq 0x50(%rsp), %r13
movq 0x10(%rsp), %rcx
testl %ecx, %ecx
jne 0x29548
movsbl (%r13), %edx
testl %edx, %edx
je 0x2980b
incq %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movq %r13, %rdx
leaq 0x40(%rsp), %rcx
movl $0x4, %r8d
callq 0x292cf
testl %eax, %eax
je 0x293bc
jmp 0x2980b
movl 0x144(%rbx), %eax
leaq 0x16e78(%rip), %rsi     # 0x403f8
cmpl $-0x2, %eax
jl 0x295a7
addl $0x2, %eax
leaq 0x16e69(%rip), %rsi     # 0x403f8
cmpl 0x140(%rbx), %eax
jge 0x295a7
movl %eax, %eax
shlq $0x4, %rax
addq 0x148(%rbx), %rax
movq %rax, %rsi
movq %rbx, %rdi
callq 0x1533b
movq %rax, %r13
cmpq %rbp, %r15
jae 0x295d8
movq 0x8(%rsp), %rax
movsbl (%rax), %edx
movq %rax, %r15
incq %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movq %r15, %rax
cmpq %rbp, %r15
jne 0x295bc
movq 0x10(%rsp), %rcx
movslq %ecx, %rcx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x38(%rsp)
addq %rbp, %rcx
incq %rcx
movq %rcx, 0x28(%rsp)
movq %r13, %rax
leaq 0x27c1(%rip), %r13      # 0x2bdc0
movzbl (%rax), %ecx
cmpl $0x24, %ecx
je 0x29628
testl %ecx, %ecx
je 0x29510
movsbl %cl, %edx
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
incq %r12
movq %r12, %rax
jmp 0x295ff
leaq 0x1(%rax), %r12
movzbl 0x1(%rax), %ecx
movl %ecx, %edx
addl $-0x24, %edx
cmpl $0x15, %edx
ja 0x2972c
movslq (%r13,%rdx,4), %rdx
addq %r13, %rdx
jmpq *%rdx
movsbl %cl, %ecx
movl %ecx, %edx
addl $-0x30, %ecx
movsbl 0x2(%rax), %esi
addq $0x2, %rax
movl %esi, %edi
movb %sil, %r8b
leal -0x30(%r8), %esi
cmpb $0xa, %sil
leal (%rdx,%rdx,4), %edx
leal -0x210(%rdi,%rdx,2), %r15d
cmovbq %rax, %r12
cmovael %ecx, %r15d
testl %r15d, %r15d
jle 0x296c9
cmpl 0x40(%rsp), %r15d
jge 0x296c9
movl %r15d, %r15d
movl %r15d, %eax
movq %rax, %rcx
shlq $0x4, %rcx
leaq 0x48(%rsp), %rdx
movq (%rdx,%rcx), %rax
movq 0x8(%rdx,%rcx), %r15
cmpq %r15, %rax
jae 0x29620
movq %rax, %r13
movsbl (%rax), %edx
incq %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movq %r13, %rax
cmpq %r15, %r13
jne 0x296a8
jmp 0x2978e
movq %rbx, %rdi
movq %r14, %rsi
movl $0x24, %edx
callq 0xe0bc
cmpl $0xb, %r15d
jl 0x29720
movl %r15d, %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rcx, %rax
shrq $0x23, %rax
movl %eax, %r13d
leal 0x30(%rax), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
addl %r13d, %r13d
movl %r13d, %eax
leaq 0x26b3(%rip), %r13      # 0x2bdc0
leal (%rax,%rax,4), %eax
movl %r15d, %ecx
negl %eax
leal (%rcx,%rax), %edx
addl $0x30, %edx
jmp 0x29615
addl $0x30, %r15d
movl %r15d, %edx
jmp 0x29615
cmpl $0x60, %ecx
je 0x297dc
testl %ecx, %ecx
jne 0x29743
movl $0x24, %edx
jmp 0x29612
movq %rbx, %rdi
movq %r14, %rsi
movl $0x24, %edx
callq 0xe0bc
movsbl (%r12), %edx
jmp 0x29615
movq %rbp, %r13
movq 0x38(%rsp), %rbp
testq %rbp, %rbp
jle 0x2978b
xorl %eax, %eax
movq %rax, %r15
movsbl (%r13,%rax), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
incq %r15
movq %r15, %rax
cmpq %r15, %rbp
jne 0x2976c
movq %r13, %rbp
leaq 0x262b(%rip), %r13      # 0x2bdc0
jmp 0x29620
movl %ecx, %edx
jmp 0x29615
movq 0x30(%rsp), %rax
movb (%rbp,%rax), %cl
testb %cl, %cl
je 0x29620
movq 0x28(%rsp), %rax
movq %rax, %r15
movsbl %cl, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movb (%r15), %dl
incq %r15
movq %r15, %rax
movl %edx, %ecx
testb %dl, %dl
jne 0x297b7
jmp 0x29620
movq 0x8(%rsp), %rax
cmpq %rbp, %rax
jae 0x29620
movsbl (%rax), %edx
movq %rax, %r15
incq %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movq %r15, %rax
cmpq %rbp, %r15
jne 0x297ea
jmp 0x29620
movq 0x10(%rsp), %rax
cltq
movb (%rbp,%rax), %cl
testb %cl, %cl
je 0x29845
addq %rbp, %rax
incq %rax
leaq 0x18(%rsp), %r14
movq %rax, %r15
movsbl %cl, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe0bc
movb (%r15), %dl
incq %r15
movq %r15, %rax
movl %edx, %ecx
testb %dl, %dl
jne 0x29825
leaq 0x18(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xe0bc
movq %rbx, %rdi
callq 0x13fbb
movq %rax, %rdi
callq 0x3100
movq (%r14), %r14
testl %eax, %eax
jne 0x298ce
leaq 0x8(%r14), %rax
testq %r14, %r14
leaq 0xab0a(%rip), %rsi      # 0x34386
cmovneq %rax, %rsi
movq %rbx, %rdi
callq 0x13665
movl 0x8198(%rbx), %eax
testl %eax, %eax
je 0x298e1
decl %eax
movl %eax, 0x8198(%rbx)
movq (%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x10(%rbx)
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, 0x140(%rbx)
leaq 0x9eaa(%rip), %rsi      # 0x3376e
xorl %eax, %eax
movq %rbx, %rdi
callq 0xc939
movq (%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq *0x10(%rbx)
movq %rbx, %rdi
callq 0x8ec6
leaq 0x9f21(%rip), %rsi      # 0x33809
jmp 0x298c4