// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9Builtin_strtok
static int jx9Builtin_strtok(jx9_context *pCtx, int nArg, jx9_value **apArg)
{
	strtok_aux_data *pAux;
	const char *zMask;
	SyString sToken; 
	int nMasklen;
	sxi32 rc;
	if( nArg < 2 ){
		/* Extract top aux data */
		pAux = (strtok_aux_data *)jx9_context_peek_aux_data(pCtx);
		if( pAux == 0 ){
			/* No aux data, return FALSE */
			jx9_result_bool(pCtx, 0);
			return JX9_OK;
		}
		nMasklen = 0;
		zMask = ""; /* cc warning */
		if( nArg > 0 ){
			/* Extract the mask */
			zMask = jx9_value_to_string(apArg[0], &nMasklen);
		}
		if( nMasklen < 1 ){
			/* Invalid mask, return FALSE */
			jx9_context_free_chunk(pCtx, (void *)pAux->zDup);
			jx9_context_free_chunk(pCtx, pAux);
			(void)jx9_context_pop_aux_data(pCtx);
			jx9_result_bool(pCtx, 0);
			return JX9_OK;
		}
		/* Extract the token */
		rc = ExtractToken(&pAux->zIn, pAux->zEnd, zMask, nMasklen, &sToken);
		if( rc != SXRET_OK ){
			/* EOF , discard the aux data */
			jx9_context_free_chunk(pCtx, (void *)pAux->zDup);
			jx9_context_free_chunk(pCtx, pAux);
			(void)jx9_context_pop_aux_data(pCtx);
			jx9_result_bool(pCtx, 0);
		}else{
			/* Return the extracted token */
			jx9_result_string(pCtx, sToken.zString, (int)sToken.nByte);
		}
	}else{
		const char *zInput, *zCur;
		char *zDup;
		int nLen;
		/* Extract the raw input */
		zCur = zInput = jx9_value_to_string(apArg[0], &nLen);
		if( nLen < 1 ){
			/* Empty input, return FALSE */
			jx9_result_bool(pCtx, 0);
			return JX9_OK;
		}
		/* Extract the mask */
		zMask = jx9_value_to_string(apArg[1], &nMasklen);
		if( nMasklen < 1 ){
			/* Set a default mask */
#define TOK_MASK " \n\t\r\f" 
			zMask = TOK_MASK;
			nMasklen = (int)sizeof(TOK_MASK) - 1;
#undef TOK_MASK
		}
		/* Extract a single token */
		rc = ExtractToken(&zInput, &zInput[nLen], zMask, nMasklen, &sToken);
		if( rc != SXRET_OK ){
			/* Empty input */
			jx9_result_bool(pCtx, 0);
			return JX9_OK;
		}else{
			/* Return the extracted token */
			jx9_result_string(pCtx, sToken.zString, (int)sToken.nByte);
		}
		/* Create our auxilliary data and copy the input */
		pAux = (strtok_aux_data *)jx9_context_alloc_chunk(pCtx, sizeof(strtok_aux_data), TRUE, FALSE);
		if( pAux ){
			nLen -= (int)(zInput-zCur);
			if( nLen < 1 ){
				jx9_context_free_chunk(pCtx, pAux);
				return JX9_OK;
			}
			/* Duplicate input */
			zDup = (char *)jx9_context_alloc_chunk(pCtx, (unsigned int)(nLen+1), TRUE, FALSE);
			if( zDup  ){
				SyMemcpy(zInput, zDup, (sxu32)nLen);
				/* Register the aux data */
				pAux->zDup = pAux->zIn = zDup;
				pAux->zEnd = &zDup[nLen];
				jx9_context_push_aux_data(pCtx, pAux);
			}
		}
	}
	return JX9_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdx, %r15
movq %rdi, %rbx
cmpl $0x1, %esi
jg 0x4b558
movl %esi, %r12d
movq %rbx, %rdi
callq 0x2a1fd
testq %rax, %rax
je 0x4b5b8
movq %rax, %r14
andl $0x0, -0x28(%rbp)
cmpl $0x1, %r12d
jne 0x4b538
movq (%r15), %rdi
leaq -0x28(%rbp), %r15
movq %r15, %rsi
callq 0x297ed
movl (%r15), %ecx
testl %ecx, %ecx
jle 0x4b538
movq %r14, %rdi
addq $0x8, %rdi
movq 0x10(%r14), %rsi
leaq -0x40(%rbp), %r8
movq %rax, %rdx
callq 0x4eeea
testl %eax, %eax
je 0x4b657
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x2a404
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a404
movq %rbx, %rdi
callq 0x2a22b
jmp 0x4b5b8
movq (%r15), %rdi
leaq -0x24(%rbp), %r12
movq %r12, %rsi
callq 0x297ed
movq %rax, -0x30(%rbp)
cmpl $0x0, (%r12)
jle 0x4b5b8
movq %rax, %r14
movq 0x8(%r15), %rdi
leaq -0x28(%rbp), %r15
movq %r15, %rsi
callq 0x297ed
movl (%r15), %ecx
testl %ecx, %ecx
jg 0x4b59d
movl $0x5, -0x28(%rbp)
leaq 0x1b9d2(%rip), %rax     # 0x66f6c
pushq $0x5
popq %rcx
movslq -0x24(%rbp), %rsi
addq %r14, %rsi
leaq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %r8
movq %rax, %rdx
callq 0x4eeea
testl %eax, %eax
je 0x4b5d1
movq %rbx, %rdi
xorl %esi, %esi
callq 0x298b2
xorl %eax, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rsi
movl -0x38(%rbp), %edx
movq %rbx, %rdi
callq 0x29903
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2a356
testq %rax, %rax
je 0x4b5c2
movq %rax, %r15
movq -0x30(%rbp), %r12
subl %r12d, %r14d
addl -0x24(%rbp), %r14d
movl %r14d, -0x24(%rbp)
testl %r14d, %r14d
jle 0x4b66b
incl %r14d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0x2a356
testq %rax, %rax
je 0x4b5c2
movq %rax, %r14
movl -0x24(%rbp), %edx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2d3b0
movq %r14, 0x8(%r15)
movq %r14, (%r15)
movslq -0x24(%rbp), %rax
addq %r14, %rax
movq %rax, 0x10(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a1d2
jmp 0x4b5c2
movq -0x40(%rbp), %rsi
movl -0x38(%rbp), %edx
movq %rbx, %rdi
callq 0x29903
jmp 0x4b5c2
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a404
jmp 0x4b5c2