// File: /workspace/github_c_makefile_star2_O3/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
movq %rdi, %rbx
cmpl $0x2, %esi
jg 0x57707
movq 0x8(%rbx), %rdi
callq 0x30465
jmp 0x57a4c
movq %rdx, %r14
movq 0x60(%rbx), %rax
movaps 0x1bf1b(%rip), %xmm0    # 0x73630
movups %xmm0, -0x78(%rbp)
movq %rax, -0x88(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x80(%rbp)
movq %rcx, -0x68(%rbp)
movl %ecx, -0x44(%rbp)
movl %ecx, -0x48(%rbp)
movq $0x10, -0x40(%rbp)
movq %rax, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
movq %rcx, -0x38(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0xa0(%rbp)
movq %rax, -0xa8(%rbp)
movl %ecx, -0x90(%rbp)
movq $-0x4, %rax
movl $0x0, -0xdc(%rbp,%rax)
addq $0x4, %rax
cmpq $0x1c, %rax
jb 0x57762
movq %rbx, -0xc8(%rbp)
leaq -0x88(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0x10(%r14), %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq 0x30577
movl (%r15), %edx
testl %edx, %edx
jle 0x577e9
leaq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x359ad
movq (%r14), %rdi
testb $0x40, 0x8(%rdi)
jne 0x57800
movq %r15, %rsi
callq 0x30577
movl (%r15), %ecx
testl %ecx, %ecx
jle 0x57a40
leaq -0xb8(%rbp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
leaq -0x88(%rbp), %rdi
callq 0x33094
jmp 0x57816
movq 0x8(%rbx), %rdi
leaq 0x1fe7e(%rip), %rsi     # 0x77672
xorl %edx, %edx
callq 0x302f6
jmp 0x57a4c
movq (%rdi), %rdi
leaq 0x4162(%rip), %rsi      # 0x5b96c
leaq -0xe0(%rbp), %rdx
callq 0x5134a
movq 0x8(%r14), %rdi
movl 0x8(%rdi), %eax
movl %eax, -0x5c(%rbp)
testb $0x40, %al
jne 0x5784b
leaq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x30577
leaq -0xb8(%rbp), %rsi
movq %rax, (%rsi)
movl (%r14), %eax
movl %eax, 0x8(%rsi)
leaq -0x58(%rbp), %rdi
callq 0x33094
jmp 0x57869
leaq -0x58(%rbp), %rax
leaq -0xe0(%rbp), %rdx
movq %rax, 0x10(%rdx)
movq (%rdi), %rdi
leaq 0x4108(%rip), %rsi      # 0x5b96c
callq 0x5134a
xorl %r15d, %r15d
movl %r15d, -0x6c(%rbp)
movl %r15d, -0x3c(%rbp)
leaq 0x1fdf7(%rip), %rax     # 0x77672
movq %rax, -0xb8(%rbp)
movl %r15d, -0xb0(%rbp)
movq %rbx, -0xc0(%rbp)
movq (%rbx), %rax
movq 0x8(%rax), %rdi
leaq 0x1f4e6(%rip), %rsi     # 0x76d84
movl $0xc, %edx
callq 0x59d90
testl %eax, %eax
leaq 0x28e2(%rip), %rax      # 0x5a193
leaq 0x25d8(%rip), %r13      # 0x59e90
cmoveq %rax, %r13
movl -0x78(%rbp), %eax
testl %eax, %eax
je 0x579a7
movq -0x80(%rbp), %rbx
movl -0x70(%rbp), %r12d
imull %r15d, %r12d
incl %r15d
movl %r15d, -0x6c(%rbp)
cmpl $0x0, 0x8(%rbx,%r12)
je 0x5799e
testb $0x40, -0x5c(%rbp)
jne 0x57903
movl -0x48(%rbp), %r14d
testl %r14d, %r14d
je 0x5792a
decl %r14d
imull -0x40(%rbp), %r14d
addq -0x50(%rbp), %r14
jmp 0x5791c
movl -0x3c(%rbp), %ecx
cmpl -0x48(%rbp), %ecx
jae 0x57923
movl -0x40(%rbp), %r14d
imull %ecx, %r14d
addq -0x50(%rbp), %r14
incl %ecx
movl %ecx, -0x3c(%rbp)
testq %r14, %r14
jne 0x57931
jmp 0x5792a
movl $0x0, -0x3c(%rbp)
leaq -0xb8(%rbp), %r14
movl $0x0, -0x2c(%rbp)
movl -0x98(%rbp), %esi
testl %esi, %esi
je 0x5799e
xorl %r15d, %r15d
movl %r15d, %edi
addq -0xa0(%rbp), %rdi
subl %r15d, %esi
movq (%rbx,%r12), %rdx
movl 0x8(%rbx,%r12), %ecx
leaq -0x2c(%rbp), %r8
callq *%r13
testl %eax, %eax
jne 0x57997
movl -0x2c(%rbp), %esi
addl %r15d, %esi
movl 0x8(%rbx,%r12), %edx
movq (%r14), %rcx
movl 0x8(%r14), %r8d
leaq -0xa8(%rbp), %rdi
callq 0x5ba10
addl -0x2c(%rbp), %r15d
addl 0x8(%r14), %r15d
movl -0x98(%rbp), %esi
cmpl %esi, %r15d
jb 0x57945
movl -0x78(%rbp), %eax
movl -0x6c(%rbp), %r15d
cmpl %eax, %r15d
jb 0x578c7
movl $0x0, -0x6c(%rbp)
movq -0xa0(%rbp), %r14
movl -0x98(%rbp), %edx
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x302f6
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x579e8
movq -0x80(%rbp), %rsi
testq %rsi, %rsi
je 0x579e8
callq 0x3490d
movq $0x0, -0x80(%rbp)
xorl %ebx, %ebx
movl %ebx, -0x78(%rbp)
movl %ebx, -0x6c(%rbp)
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x57a0f
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
je 0x57a0f
callq 0x3490d
movq $0x0, -0x50(%rbp)
movl %ebx, -0x48(%rbp)
movl %ebx, -0x3c(%rbp)
testb $0x6, -0x90(%rbp)
jne 0x57a4c
cmpl $0x0, -0x94(%rbp)
je 0x57a4c
movq -0xa8(%rbp), %rdi
movq %r14, %rsi
callq 0x3490d
jmp 0x57a4c
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x307f4
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq