// File: /workspace/github_c_makefile_star2_O0/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
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
cmpl $0x3, -0x14(%rbp)
jge 0x964a1
movq -0x10(%rbp), %rdi
callq 0x57120
movl $0x0, -0x4(%rbp)
jmp 0x96857
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
leaq -0xe8(%rbp), %rdi
movl $0x10, %edx
callq 0x5b470
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
leaq -0xc0(%rbp), %rdi
movl $0x10, %edx
callq 0x5b470
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rsi
leaq -0x98(%rbp), %rdi
callq 0x58bb0
leaq -0x78(%rbp), %rdi
movl $0x20, %esi
callq 0x54280
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0xe8(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0xec(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x56e20
movq %rax, -0x50(%rbp)
cmpl $0x1, -0xf0(%rbp)
jge 0x96551
movq -0x10(%rbp), %rdi
leaq 0x2f60d(%rip), %rsi     # 0xc5b4b
xorl %edx, %edx
callq 0x57180
movl $0x0, -0x4(%rbp)
jmp 0x96857
movq -0x50(%rbp), %rsi
movl -0xf0(%rbp), %edx
leaq -0x98(%rbp), %rdi
callq 0x5d9d0
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x57610
cmpl $0x0, %eax
je 0x96591
movq -0x20(%rbp), %rax
movq (%rax), %rdi
leaq 0x5f7a(%rip), %rsi      # 0x9c500
leaq -0x78(%rbp), %rdx
callq 0x57880
jmp 0x965ef
movq -0x20(%rbp), %rax
movq (%rax), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x56e20
movq %rax, -0x50(%rbp)
cmpl $0x1, -0xf0(%rbp)
jge 0x965ce
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x572c0
movl $0x0, -0x4(%rbp)
jmp 0x96857
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x5a7b0
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x57610
cmpl $0x0, %eax
je 0x96626
leaq -0xc0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x5ee5(%rip), %rsi      # 0x9c500
leaq -0x78(%rbp), %rdx
callq 0x57880
jmp 0x96669
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x56e20
movq %rax, -0x50(%rbp)
movl $0x1, -0xec(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x5a7b0
leaq -0xe8(%rbp), %rdi
callq 0x7bf40
leaq -0xc0(%rbp), %rdi
callq 0x7bf40
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
leaq 0x2f4b3(%rip), %rax     # 0xc5b4b
movq %rax, -0x30(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x584d0
movq %rax, -0x58(%rbp)
leaq 0x2929(%rip), %rax      # 0x98fe0
movq %rax, -0x48(%rbp)
movq -0x58(%rbp), %rdi
leaq 0x2eb9f(%rip), %rsi     # 0xc5265
movl $0xc, %edx
callq 0x98d80
cmpl $0x0, %eax
jne 0x966e0
leaq 0x32b4(%rip), %rax      # 0x99990
movq %rax, -0x48(%rbp)
jmp 0x966e2
leaq -0xe8(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x7bf60
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x96816
movq -0x38(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jae 0x9670a
jmp 0x966e2
cmpl $0x0, -0xec(%rbp)
je 0x96725
leaq -0xc0(%rbp), %rdi
callq 0x70f20
movq %rax, -0x40(%rbp)
jmp 0x96747
leaq -0xc0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7bf60
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x96745
movq $0x0, -0x40(%rbp)
jmp 0x96747
cmpq $0x0, -0x40(%rbp)
jne 0x96756
leaq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0xf8(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xf8(%rbp), %eax
cmpl -0x88(%rbp), %eax
jb 0x9677d
jmp 0x96811
movq -0x48(%rbp), %rax
movq -0x90(%rbp), %rdi
movl -0xf8(%rbp), %ecx
addq %rcx, %rdi
movl -0x88(%rbp), %esi
subl -0xf8(%rbp), %esi
movq -0x38(%rbp), %rcx
movq (%rcx), %rdx
movq -0x38(%rbp), %rcx
movl 0x8(%rcx), %ecx
leaq -0xfc(%rbp), %r8
callq *%rax
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
je 0x967c5
jmp 0x96811
movl -0xf8(%rbp), %esi
addl -0xfc(%rbp), %esi
movq -0x38(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq -0x40(%rbp), %rax
movl 0x8(%rax), %r8d
leaq -0x98(%rbp), %rdi
callq 0x9c5d0
movl -0xfc(%rbp), %eax
movq -0x40(%rbp), %rcx
addl 0x8(%rcx), %eax
addl -0xf8(%rbp), %eax
movl %eax, -0xf8(%rbp)
jmp 0x9676a
jmp 0x966e2
movq -0x10(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl -0x88(%rbp), %edx
callq 0x57180
leaq -0xe8(%rbp), %rdi
callq 0x66980
leaq -0xc0(%rbp), %rdi
callq 0x66980
leaq -0x98(%rbp), %rdi
callq 0x58c00
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)