// File: /workspace/github_c_makefile_star2_O1/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 0x58ed4
movq 0x8(%rbx), %rdi
callq 0x309f2
jmp 0x5923e
movq %rdx, %r14
movq 0x60(%rbx), %rax
movaps 0x1c6ee(%rip), %xmm0    # 0x755d0
movups %xmm0, -0x48(%rbp)
movq %rax, -0x58(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x50(%rbp)
movq %rcx, -0x38(%rbp)
movl %ecx, -0x6c(%rbp)
movl %ecx, -0x70(%rbp)
movq $0x10, -0x68(%rbp)
movq %rax, -0x80(%rbp)
movq %rcx, -0x78(%rbp)
movq %rcx, -0x60(%rbp)
leaq -0xa0(%rbp), %rdx
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movq %rax, -0x8(%rdx)
movl %ecx, 0x10(%rdx)
movq $-0x4, %rax
movl $0x0, -0xdc(%rbp,%rax)
addq $0x4, %rax
cmpq $0x1c, %rax
jb 0x58f29
movq %rbx, -0xc8(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq 0x10(%r14), %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq 0x30b04
movl (%r15), %edx
testl %edx, %edx
jle 0x58faa
leaq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x360ae
movq (%r14), %rdi
testb $0x40, 0x8(%rdi)
jne 0x58fc1
movq %r15, %rsi
callq 0x30b04
movl (%r15), %ecx
testl %ecx, %ecx
jle 0x59252
leaq -0xb8(%rbp), %rsi
movq %rax, (%rsi)
movl %ecx, 0x8(%rsi)
leaq -0x58(%rbp), %rdi
callq 0x336fc
jmp 0x58fd7
movq 0x8(%rbx), %rdi
leaq 0x206b2(%rip), %rsi     # 0x79667
xorl %edx, %edx
callq 0x3087c
jmp 0x5923e
movq (%rdi), %rdi
leaq 0x42e2(%rip), %rsi      # 0x5d2ad
leaq -0xe0(%rbp), %rdx
callq 0x52ab1
movq 0x8(%r14), %rdi
movl 0x8(%rdi), %eax
movl %eax, -0x84(%rbp)
testb $0x40, %al
jne 0x5900f
leaq -0x30(%rbp), %r14
movq %r14, %rsi
callq 0x30b04
leaq -0xb8(%rbp), %rsi
movq %rax, (%rsi)
movl (%r14), %eax
movl %eax, 0x8(%rsi)
leaq -0x80(%rbp), %rdi
callq 0x336fc
jmp 0x5902d
leaq -0x80(%rbp), %rax
leaq -0xe0(%rbp), %rdx
movq %rax, 0x10(%rdx)
movq (%rdi), %rdi
leaq 0x4285(%rip), %rsi      # 0x5d2ad
callq 0x52ab1
xorl %r14d, %r14d
movl %r14d, -0x3c(%rbp)
movl %r14d, -0x64(%rbp)
leaq 0x20628(%rip), %rax     # 0x79667
movq %rax, -0xb8(%rbp)
movl %r14d, -0xb0(%rbp)
movq %rbx, -0xc0(%rbp)
movq (%rbx), %rax
movq 0x8(%rax), %rdi
leaq 0x1fd17(%rip), %rsi     # 0x78d79
movl $0xc, %edx
callq 0x5b530
testl %eax, %eax
leaq 0x2acd(%rip), %rax      # 0x5bb42
leaq 0x25fc(%rip), %rbx      # 0x5b678
cmoveq %rax, %rbx
movl -0x3c(%rbp), %eax
movq -0x50(%rbp), %r13
movl -0x40(%rbp), %ecx
cmpl -0x48(%rbp), %eax
leal 0x1(%rax), %edx
cmovael %r14d, %edx
movl %edx, -0x3c(%rbp)
jae 0x5919d
imull %eax, %ecx
addq %rcx, %r13
cmpl $0x0, 0x8(%r13)
je 0x59175
movl -0x68(%rbp), %eax
testb $0x40, -0x84(%rbp)
jne 0x590d6
movl -0x70(%rbp), %ecx
leal -0x1(%rcx), %edx
imull %edx, %eax
addq -0x78(%rbp), %rax
cmpl $0x1, %ecx
movl $0x0, %r12d
cmovaeq %rax, %r12
jmp 0x590f8
movl -0x64(%rbp), %ecx
imull %ecx, %eax
addq -0x78(%rbp), %rax
leal 0x1(%rcx), %edx
cmpl -0x70(%rbp), %ecx
movl $0x0, %ecx
cmovael %ecx, %edx
movl %edx, -0x64(%rbp)
cmovaeq %rcx, %rax
movq %rax, %r12
testq %r12, %r12
movq %r12, %r15
leaq -0xb8(%rbp), %rax
cmoveq %rax, %r15
movl $0x0, -0x2c(%rbp)
movl -0x98(%rbp), %esi
testl %esi, %esi
je 0x59175
testq %r12, %r12
cmoveq %rax, %r12
xorl %r14d, %r14d
movl %r14d, %edi
addq -0xa0(%rbp), %rdi
subl %r14d, %esi
movq (%r13), %rdx
movl 0x8(%r13), %ecx
leaq -0x2c(%rbp), %r8
callq *%rbx
testl %eax, %eax
jne 0x59175
movl -0x2c(%rbp), %esi
addl %r14d, %esi
movl 0x8(%r13), %edx
movq (%r15), %rcx
movl 0x8(%r12), %r8d
leaq -0xa8(%rbp), %rdi
callq 0x5d356
addl -0x2c(%rbp), %r14d
addl 0x8(%r12), %r14d
movl -0x98(%rbp), %esi
cmpl %esi, %r14d
jb 0x59124
movl -0x3c(%rbp), %eax
movl -0x40(%rbp), %ecx
imull %eax, %ecx
addq -0x50(%rbp), %rcx
cmpl -0x48(%rbp), %eax
cmovbq %rcx, %r13
leal 0x1(%rax), %eax
movl $0x0, %ecx
cmovael %ecx, %eax
movl %eax, -0x3c(%rbp)
jb 0x590a3
movq -0xa0(%rbp), %r14
movl -0x98(%rbp), %edx
movq -0xc0(%rbp), %rax
movq 0x8(%rax), %rdi
movq %r14, %rsi
callq 0x3087c
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x591d4
movq -0x50(%rbp), %rsi
testq %rsi, %rsi
je 0x591d4
callq 0x3503f
movq $0x0, -0x50(%rbp)
xorl %ebx, %ebx
movl %ebx, -0x48(%rbp)
movl %ebx, -0x3c(%rbp)
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x591fb
movq -0x78(%rbp), %rsi
testq %rsi, %rsi
je 0x591fb
callq 0x3503f
movq $0x0, -0x78(%rbp)
movl %ebx, -0x70(%rbp)
movl %ebx, -0x64(%rbp)
testb $0x6, -0x90(%rbp)
jne 0x5922a
cmpl $0x0, -0x94(%rbp)
je 0x5922a
movq -0xa8(%rbp), %rdi
movq %r14, %rsi
callq 0x3503f
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %rax
movups %xmm0, (%rax)
movl $0x0, 0x10(%rax)
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0x30d83
jmp 0x5923e