// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_str_replace
static int jx9Builtin_str_replace(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	SyString sTemp, *pSearch, *pReplace;
	ProcStringMatch xMatch;
	const char *zIn, *zFunc;
	str_replace_data sRep;
	SyBlob sWorker;
	SySet sReplace;
	SySet sSearch;
	int rep_str;
	int nByte;
	sxi32 rc;
	if( nArg < 3 ){
		/* Missing/Invalid arguments, return null */
		jx9_result_null(pCtx);
		return JX9_OK;
	}
	/* Initialize fields */
	SySetInit(&sSearch, &pCtx->pVm->sAllocator, sizeof(SyString));
	SySetInit(&sReplace, &pCtx->pVm->sAllocator, sizeof(SyString));
	SyBlobInit(&sWorker, &pCtx->pVm->sAllocator);
	SyZero(&sRep, sizeof(str_replace_data));
	sRep.pCtx = pCtx;
	sRep.pCollector = &sSearch;
	rep_str = 0;
	/* Extract the subject */
	zIn = jx9_value_to_string(apArg[2], &nByte);
	if( nByte < 1 ){
		/* Nothing to replace, return the empty string */
		jx9_result_string(pCtx, "", 0);
		return JX9_OK;
	}
	/* Copy the subject */
	SyBlobAppend(&sWorker, (const void *)zIn, (sxu32)nByte);
	/* Search string */
	if( jx9_value_is_json_array(apArg[0]) ){
		/* Collect search string */
		jx9_array_walk(apArg[0], StrReplaceWalker, &sRep);
	}else{
		/* Single pattern */
		zIn = jx9_value_to_string(apArg[0], &nByte);
		if( nByte < 1 ){
			/* Return the subject untouched since no search string is available */
			jx9_result_value(pCtx, apArg[2]/* Subject as thrird argument*/);
			return JX9_OK;
		}
		SyStringInitFromBuf(&sTemp, zIn, nByte);
		/* Save for later processing */
		SySetPut(&sSearch, (const void *)&sTemp);
	}
	/* Replace string */
	if( jx9_value_is_json_array(apArg[1]) ){
		/* Collect replace string */
		sRep.pCollector = &sReplace;
		jx9_array_walk(apArg[1], StrReplaceWalker, &sRep);
	}else{
		/* Single needle */
		zIn = jx9_value_to_string(apArg[1], &nByte);
		rep_str = 1;
		SyStringInitFromBuf(&sTemp, zIn, nByte);
		/* Save for later processing */
		SySetPut(&sReplace, (const void *)&sTemp);
	}
	/* Reset loop cursors */
	SySetResetCursor(&sSearch);
	SySetResetCursor(&sReplace);
	pReplace = pSearch = 0; /* cc warning */
	SyStringInitFromBuf(&sTemp, "", 0);
	/* Extract function name */
	zFunc = jx9_function_name(pCtx);
	/* Set the default pattern match routine */
	xMatch = SyBlobSearch;
	if( SyStrncmp(zFunc, "str_ireplace", sizeof("str_ireplace") - 1) ==  0 ){
		/* Case insensitive pattern match */
		xMatch = iPatternMatch;
	}
	/* Start the replace process */
	while( SXRET_OK == SySetGetNextEntry(&sSearch, (void **)&pSearch) ){
		sxu32 nCount, nOfft;
		if( pSearch->nByte <  1 ){
			/* Empty string, ignore */
			continue;
		}
		/* Extract the replace string */
		if( rep_str ){
			pReplace = (SyString *)SySetPeek(&sReplace);
		}else{
			if( SXRET_OK != SySetGetNextEntry(&sReplace, (void **)&pReplace) ){
				/* Sepecial case when 'replace set' has fewer values than the search set.
				 * An empty string is used for the rest of replacement values
				 */
				pReplace = 0;
			}
		}
		if( pReplace == 0 ){
			/* Use an empty string instead */
			pReplace = &sTemp;
		}
		nOfft = nCount = 0;
		for(;;){
			if( nCount >= SyBlobLength(&sWorker) ){
				break;
			}
			/* Perform a pattern lookup */
			rc = xMatch(SyBlobDataAt(&sWorker, nCount), SyBlobLength(&sWorker) - nCount, (const void *)pSearch->zString, 
				pSearch->nByte, &nOfft);
			if( rc != SXRET_OK ){
				/* Pattern not found */
				break;
			}
			/* Perform the replace operation */
			StringReplace(&sWorker, nCount+nOfft, (int)pSearch->nByte, pReplace->zString, (int)pReplace->nByte);
			/* Increment offset counter */
			nCount += nOfft + pReplace->nByte;
		}
	}
	/* All done, clean-up the mess left behind */
	jx9_result_string(pCtx, (const char *)SyBlobData(&sWorker), (int)SyBlobLength(&sWorker));
	SySetRelease(&sSearch);
	SySetRelease(&sReplace);
	SyBlobRelease(&sWorker);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
cmpl $0x2, %esi
jg 0x4b8b7
callq 0x298e8
jmp 0x4bb5b
movq %rdx, %r14
movq 0x60(%rdi), %rax
movaps 0x17d4b(%rip), %xmm0    # 0x63610
leaq -0xc0(%rbp), %rbx
movups %xmm0, 0x10(%rbx)
movq %rax, (%rbx)
andq $0x0, 0x8(%rbx)
andq $0x0, 0x20(%rbx)
movq $0x10, -0x50(%rbp)
movq %rax, -0x68(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x60(%rbp)
andq $0x0, -0x48(%rbp)
movups %xmm0, -0x80(%rbp)
movq %rax, -0x88(%rbp)
andl $0x0, -0x70(%rbp)
leaq -0xe0(%rbp), %r15
pushq $0x20
popq %rsi
movq %rdi, %r12
movq %r15, %rdi
callq 0x27aea
movq %r12, 0x18(%r15)
movq %rbx, 0x10(%r15)
movq 0x10(%r14), %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq 0x297ed
movl (%r15), %edx
testl %edx, %edx
jle 0x4b982
movq %r12, -0x38(%rbp)
leaq -0x88(%rbp), %rdi
movq %rax, %rsi
callq 0x2e250
movq (%r14), %rdi
testb $0x40, 0x8(%rdi)
jne 0x4b998
movq %r15, %rsi
callq 0x297ed
movl (%r15), %ecx
testl %ecx, %ecx
jle 0x4bb6f
leaq -0x98(%rbp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
leaq -0xc0(%rbp), %rdi
callq 0x2be00
jmp 0x4b9ab
leaq 0x1bcc9(%rip), %rsi     # 0x67652
movq %r12, %rdi
xorl %edx, %edx
callq 0x29903
jmp 0x4bb5b
leaq 0x3644(%rip), %rsi      # 0x4efe3
leaq -0xe0(%rbp), %rdx
callq 0x29c1e
movq 0x8(%r14), %rdi
movl 0x8(%rdi), %eax
movl %eax, -0x3c(%rbp)
testb $0x40, %al
jne 0x4b9e0
leaq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x297ed
leaq -0x98(%rbp), %rsi
movq %rax, (%rsi)
movl (%r14), %eax
movl %eax, 0x8(%rsi)
leaq -0x68(%rbp), %rdi
callq 0x2be00
jmp 0x4b9fb
leaq -0x68(%rbp), %rax
leaq -0xe0(%rbp), %rdx
movq %rax, 0x10(%rdx)
leaq 0x35ed(%rip), %rsi      # 0x4efe3
callq 0x29c1e
andl $0x0, -0xa4(%rbp)
andl $0x0, -0x4c(%rbp)
leaq 0x1bc45(%rip), %rax     # 0x67652
leaq -0x98(%rbp), %rcx
movq %rax, (%rcx)
andl $0x0, 0x8(%rcx)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x1b337(%rip), %rsi     # 0x66d64
pushq $0xc
popq %rdx
callq 0x4d6c7
testl %eax, %eax
leaq 0x207e(%rip), %rax      # 0x4dabc
leaq 0x1d95(%rip), %rbx      # 0x4d7da
cmoveq %rax, %rbx
xorl %eax, %eax
cmpl -0xb0(%rbp), %eax
jae 0x4bb1e
movq -0xb8(%rbp), %r13
movl -0xa8(%rbp), %r12d
imull %eax, %r12d
incl %eax
movl %eax, -0xa4(%rbp)
cmpl $0x0, 0x8(%r13,%r12)
je 0x4ba4b
testb $0x40, -0x3c(%rbp)
jne 0x4ba96
movl -0x58(%rbp), %r15d
testl %r15d, %r15d
je 0x4baba
decl %r15d
imull -0x50(%rbp), %r15d
addq -0x60(%rbp), %r15
jmp 0x4baaf
movl -0x4c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x4bab6
movl -0x50(%rbp), %r15d
imull %eax, %r15d
addq -0x60(%rbp), %r15
incl %eax
movl %eax, -0x4c(%rbp)
testq %r15, %r15
jne 0x4bac1
jmp 0x4baba
andl $0x0, -0x4c(%rbp)
leaq -0x98(%rbp), %r15
andl $0x0, -0x2c(%rbp)
xorl %r14d, %r14d
movl -0x78(%rbp), %esi
subl %r14d, %esi
jbe 0x4bb13
movl %r14d, %edi
addq -0x80(%rbp), %rdi
movq (%r13,%r12), %rdx
movl 0x8(%r13,%r12), %ecx
leaq -0x2c(%rbp), %r8
callq *%rbx
testl %eax, %eax
jne 0x4bb13
movl -0x2c(%rbp), %esi
addl %r14d, %esi
movl 0x8(%r13,%r12), %edx
movq (%r15), %rcx
movl 0x8(%r15), %r8d
leaq -0x88(%rbp), %rdi
callq 0x4f072
addl -0x2c(%rbp), %r14d
addl 0x8(%r15), %r14d
jmp 0x4bac8
movl -0xa4(%rbp), %eax
jmp 0x4ba4b
leaq -0xc0(%rbp), %r14
andl $0x0, 0x1c(%r14)
leaq -0x88(%rbp), %r15
movq 0x8(%r15), %rsi
movl 0x10(%r15), %edx
movq -0x38(%rbp), %rdi
callq 0x29903
movq %r14, %rdi
callq 0x31feb
leaq -0x68(%rbp), %rdi
callq 0x31feb
movq %r15, %rdi
callq 0x2a72f
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rsi
movq -0x38(%rbp), %rdi
callq 0x299d3
jmp 0x4bb5b