// File: /workspace/github_c_makefile_star2_O1/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 $0x18, %rsp
cmpl $0x1, %esi
jg 0x55a31
movq 0x8(%rdi), %rbx
jmp 0x55ac5
movq %rdx, %r12
movl %esi, %r13d
movq %rdi, -0x38(%rbp)
movq (%rdx), %rdi
leaq -0x2c(%rbp), %rbx
movq %rbx, %rsi
callq 0x30b04
movq %rax, %r14
movq 0x8(%r12), %rdi
leaq -0x30(%rbp), %rsi
callq 0x30b04
movq %rax, %r15
movl (%rbx), %eax
testl %eax, %eax
jle 0x55abd
movl -0x30(%rbp), %ecx
testl %ecx, %ecx
jle 0x55abd
cmpl %eax, %ecx
jg 0x55abd
cmpl $0x2, %r13d
je 0x55b04
movq 0x10(%r12), %rbx
movq %rbx, %rdi
callq 0x3fd5f
movq (%rbx), %rax
testl %eax, %eax
js 0x55a94
movl -0x2c(%rbp), %ecx
subl %eax, %ecx
jge 0x55af3
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x309f2
movq $0x0, (%rbx)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%rbx), %eax
orl $0x2, %eax
movl %eax, 0x8(%rbx)
xorl %eax, %eax
jmp 0x55b00
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x309f2
movq $0x0, (%rbx)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%rbx), %eax
orl $0x2, %eax
movl %eax, 0x8(%rbx)
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %rax, %r14
movl %ecx, -0x2c(%rbp)
movb $0x1, %al
testb %al, %al
je 0x55ae2
movslq -0x2c(%rbp), %rbx
addq %r14, %rbx
cmpl $0x4, %r13d
jl 0x55b76
movq 0x18(%r12), %r12
movq %r12, %rdi
callq 0x3fd5f
movq (%r12), %rax
testl %eax, %eax
setns %cl
cmpl %eax, -0x2c(%rbp)
setge %r13b
andb %cl, %r13b
je 0x55b43
movl %eax, -0x2c(%rbp)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
addq %r14, %rax
movq %rax, %rbx
jmp 0x55b6d
movq -0x38(%rbp), %rax
movq 0x8(%rax), %r12
movq %r12, %rdi
callq 0x309f2
movq $0x0, (%r12)
movl $0xfffffe90, %eax       # imm = 0xFFFFFE90
andl 0x8(%r12), %eax
orl $0x2, %eax
movl %eax, 0x8(%r12)
testb %r13b, %r13b
je 0x55ae2
xorl %r13d, %r13d
leaq -0x3c(%rbp), %r12
movl %ebx, %esi
subl %r14d, %esi
movl -0x30(%rbp), %ecx
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %r8
callq 0x5b678
testl %eax, %eax
jne 0x55ba8
incl %r13d
movl -0x30(%rbp), %eax
addl -0x3c(%rbp), %eax
addq %rax, %r14
cmpq %rbx, %r14
jb 0x55b7d
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rbx
movq %rbx, %rdi
callq 0x309f2
movslq %r13d, %rax
movq %rax, (%rbx)
jmp 0x55ad4