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

	source = checkstring(J, 0);
	needle = js_tostring(J, 1);

	s = strstr(source, needle);
	if (!s) {
		js_copy(J, 0);
		return;
	}
	n = strlen(needle);

	if (js_iscallable(J, 2)) {
		js_copy(J, 2);
		js_pushundefined(J);
		js_pushlstring(J, s, n); /* arg 1: substring that matched */
		js_pushnumber(J, s - source); /* arg 2: offset within search string */
		js_copy(J, 0); /* arg 3: search string */
		js_call(J, 3);
		r = js_tostring(J, -1);
		js_putm(J, &sb, source, s);
		js_puts(J, &sb, r);
		js_puts(J, &sb, s + n);
		js_putc(J, &sb, 0);
		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, s, s + n); break;
				case '`': js_putm(J, &sb, source, s); break;
				case '\'': js_puts(J, &sb, s + n); break;
				default: js_putc(J, &sb, '$'); js_putc(J, &sb, *r); break;
				}
				++r;
			} else {
				js_putc(J, &sb, *r++);
			}
		}
		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 $0x18, %rsp
movq %rdi, %rbx
andq $0x0, (%rsp)
callq 0x1dda8
movq %rax, %r12
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xf64d
movq %rax, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x3260
testq %rax, %rax
je 0x1e2ca
movq %rax, %r13
movq %r12, 0x8(%rsp)
movq %r15, %rdi
movq %rax, %r15
callq 0x3200
movq %rax, %r14
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xf950
testl %eax, %eax
je 0x1e2d9
movl %r14d, %ebp
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xeee2
movq %rbx, %rdi
callq 0x7336
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
callq 0xe918
movq %r13, %rax
subq %r12, %rax
cvtsi2sd %rax, %xmm0
movq %rbx, %rdi
callq 0x3f9a
xorl %esi, %esi
movq %rbx, %rdi
callq 0xeee2
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x110b0
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xf64d
movq %rax, %r12
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
movq %r15, %rcx
callq 0xaa49
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0xaa1e
movslq %ebp, %rdx
addq %r13, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaa1e
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xa986
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x3f3d
jmp 0x1e3d5
movq %rbx, %rdi
xorl %esi, %esi
callq 0xeee2
jmp 0x1e41a
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0xf64d
movq %rax, %rbp
movq %rsp, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rcx
movq 0x8(%rsp), %rdx
movq %rdx, %r15
movq %rcx, 0x10(%rsp)
callq 0xaa49
movslq %r14d, %rax
addq %rax, %r13
movzbl (%rbp), %eax
cmpl $0x24, %eax
je 0x1e327
testl %eax, %eax
je 0x1e3b7
movsbl %al, %edx
movq %rbx, %rdi
movq %r12, %rsi
jmp 0x1e372
leaq 0x1(%rbp), %r14
movzbl 0x1(%rbp), %eax
cmpl $0x60, %eax
je 0x1e399
cmpl $0x24, %eax
je 0x1e366
cmpl $0x26, %eax
je 0x1e389
cmpl $0x27, %eax
je 0x1e379
testl %eax, %eax
je 0x1e369
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x24
popq %rdx
callq 0xa986
movsbl (%r14), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xa986
jmp 0x1e3ac
movq %r14, %rbp
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x24
popq %rdx
callq 0xa986
jmp 0x1e3af
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xaa1e
jmp 0x1e3ac
movq %rbx, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq %r13, %rcx
jmp 0x1e3a7
movq %r15, %rdx
movq %rbx, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rcx
callq 0xaa49
movq %r14, %rbp
incq %rbp
jmp 0x1e30b
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0xaa1e
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xa986
movq %rbx, %rdi
callq 0xee71
movq %rax, %rdi
callq 0x3100
movq (%rsp), %r14
testl %eax, %eax
jne 0x1e429
leaq 0x8(%r14), %rax
testq %r14, %r14
leaq 0xaf96(%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 $0x18, %rsp
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