// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_substr_count
static int jx9Builtin_substr_count(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	const char *zText, *zPattern, *zEnd;
	int nTextlen, nPatlen;
	int iCount = 0;
	sxu32 nOfft;
	sxi32 rc;
	if( nArg < 2 ){
		/* Missing arguments */
		jx9_result_int(pCtx, 0);
		return JX9_OK;
	}
	/* Point to the haystack */
	zText = jx9_value_to_string(apArg[0], &nTextlen);
	/* Point to the neddle */
	zPattern = jx9_value_to_string(apArg[1], &nPatlen);
	if( nTextlen < 1 || nPatlen < 1 || nPatlen > nTextlen ){
		/* NOOP, return zero */
		jx9_result_int(pCtx, 0);
		return JX9_OK;
	}
	if( nArg > 2 ){
		int nOfft;
		/* Extract the offset */
		nOfft = jx9_value_to_int(apArg[2]);
		if( nOfft < 0 || nOfft > nTextlen ){
			/* Invalid offset, return zero */
			jx9_result_int(pCtx, 0);
			return JX9_OK;
		}
		/* Point to the desired offset */
		zText = &zText[nOfft];
		/* Adjust length */
		nTextlen -= nOfft;
	}
	/* Point to the end of the string */
	zEnd = &zText[nTextlen];
	if( nArg > 3 ){
		int nLen;
		/* Extract the length */
		nLen = jx9_value_to_int(apArg[3]);
		if( nLen < 0 || nLen > nTextlen ){
			/* Invalid length, return 0 */
			jx9_result_int(pCtx, 0);
			return JX9_OK;
		}
		/* Adjust pointer */
		nTextlen = nLen;
		zEnd = &zText[nTextlen];
	}
	/* Perform the search */
	for(;;){
		rc = SyBlobSearch((const void *)zText, (sxu32)(zEnd-zText), (const void *)zPattern, nPatlen, &nOfft);
		if( rc != SXRET_OK ){
			/* Pattern not found, break immediately */
			break;
		}
		/* Increment counter and update the offset */
		iCount++;
		zText += nOfft + nPatlen;
		if( zText >= zEnd ){
			break;
		}
	}
	/* Pattern count */
	jx9_result_int(pCtx, iCount);
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r15
cmpl $0x1, %esi
jg 0x54365
movq 0x8(%r15), %rbx
movq %rbx, %rdi
callq 0x30465
movq $0x0, (%rbx)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%rbx), %eax
orl $0x2, %eax
movl %eax, 0x8(%rbx)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movl %esi, %r13d
movq (%rdx), %rdi
leaq -0x2c(%rbp), %rbx
movq %rbx, %rsi
callq 0x30577
movq %rax, %r14
movq 0x8(%r12), %rdi
leaq -0x30(%rbp), %rsi
callq 0x30577
movq %rax, -0x48(%rbp)
movslq (%rbx), %rbx
testq %rbx, %rbx
jle 0x54333
movl -0x30(%rbp), %eax
testl %eax, %eax
jle 0x54333
cmpl %ebx, %eax
jg 0x54333
cmpl $0x2, %r13d
jne 0x54401
movq %r15, -0x38(%rbp)
addq %r14, %rbx
xorl %r15d, %r15d
movl -0x30(%rbp), %r12d
leaq -0x3c(%rbp), %r13
movl %ebx, %esi
subl %r14d, %esi
movq %r14, %rdi
movq -0x48(%rbp), %rdx
movl %r12d, %ecx
movq %r13, %r8
callq 0x59e90
testl %eax, %eax
jne 0x543e6
incl %r15d
movl -0x3c(%rbp), %eax
addl %r12d, %eax
addq %rax, %r14
cmpq %rbx, %r14
jb 0x543ba
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x30465
movslq %r15d, %rax
movq %rax, (%rbx)
jmp 0x54346
movq 0x10(%r12), %rbx
movq %rbx, %rdi
callq 0x3f362
movq (%rbx), %rax
testl %eax, %eax
js 0x54333
movl -0x2c(%rbp), %ecx
subl %eax, %ecx
jl 0x54333
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
movl %ecx, -0x2c(%rbp)
cmpl $0x4, %r13d
jb 0x54468
movq 0x18(%r12), %rbx
movq %rbx, %rdi
callq 0x3f362
movq (%rbx), %rbx
testl %ebx, %ebx
js 0x54333
cmpl %ebx, -0x2c(%rbp)
jl 0x54333
movq %r15, -0x38(%rbp)
movl %ebx, -0x2c(%rbp)
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
jmp 0x543ac
movq %r15, -0x38(%rbp)
movslq %ecx, %rbx
jmp 0x543ac