// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileForeach
static sxi32 jx9CompileForeach(jx9_gen_state *pGen)
{ 
	SyToken *pCur, *pTmp, *pEnd = 0;
	GenBlock *pForeachBlock = 0;
	jx9_foreach_info *pInfo;
	sxu32 nFalseJump;
	VmInstr *pInstr;
	sxu32 nLine;
	sxi32 rc;
	nLine = pGen->pIn->nLine;
	/* Jump the 'foreach' keyword */
	pGen->pIn++;    
	if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_LPAREN) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "foreach: Expected '('");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Jump the left parenthesis '(' */
	pGen->pIn++; 
	/* Create the loop block */
	rc = GenStateEnterBlock(&(*pGen), GEN_BLOCK_LOOP, jx9VmInstrLength(pGen->pVm), 0, &pForeachBlock);
	if( rc != SXRET_OK ){
		return SXERR_ABORT;
	}
	/* Delimit the expression */
	jx9DelimitNestedTokens(pGen->pIn, pGen->pEnd, JX9_TK_LPAREN /* '(' */, JX9_TK_RPAREN /* ')' */, &pEnd);
	if( pGen->pIn == pEnd || pEnd >= pGen->pEnd ){
		/* Empty expression */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "foreach: Missing expression");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		/* Synchronize */
		pGen->pIn = pEnd;
		if( pGen->pIn < pGen->pEnd ){
			pGen->pIn++;
		}
		return SXRET_OK;
	}
	/* Compile the array expression */
	pCur = pGen->pIn;
	while( pCur < pEnd ){
		if( pCur->nType & JX9_TK_KEYWORD ){
			sxi32 nKeywrd = SX_PTR_TO_INT(pCur->pUserData);
			if( nKeywrd == JX9_TKWRD_AS ){
				/* Break with the first 'as' found */
				break;
			}
		}
		/* Advance the stream cursor */
		pCur++;
	}
	if( pCur <= pGen->pIn ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, 
			"foreach: Missing array/object expression");
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Swap token streams */
	pTmp = pGen->pEnd;
	pGen->pEnd = pCur;
	rc = jx9CompileExpr(&(*pGen), 0, 0);
	if( rc == SXERR_ABORT ){
		/* Expression handler request an operation abort [i.e: Out-of-memory] */
		return SXERR_ABORT;
	}
	/* Update token stream */
	while(pGen->pIn < pCur ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Unexpected token '%z'", &pGen->pIn->sData);
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		pGen->pIn++;
	}
	pCur++; /* Jump the 'as' keyword */
	pGen->pIn = pCur; 
	if( pGen->pIn >= pEnd ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $key => $value pair");
		if( rc == SXERR_ABORT ){
			return SXERR_ABORT;
		}
	}
	/* Create the foreach context */
	pInfo = (jx9_foreach_info *)SyMemBackendAlloc(&pGen->pVm->sAllocator, sizeof(jx9_foreach_info));
	if( pInfo == 0 ){
		jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Fatal, JX9 engine is running out-of-memory");
		return SXERR_ABORT;
	}
	/* Zero the structure */
	SyZero(pInfo, sizeof(jx9_foreach_info));
	/* Initialize structure fields */
	SySetInit(&pInfo->aStep, &pGen->pVm->sAllocator, sizeof(jx9_foreach_step *));
	/* Check if we have a key field */
	while( pCur < pEnd && (pCur->nType & JX9_TK_COMMA) == 0 ){
		pCur++;
	}
	if( pCur < pEnd ){
		/* Compile the expression holding the key name */
		if( pGen->pIn >= pCur ){
			rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $key");
			if( rc == SXERR_ABORT ){
				/* Don't worry about freeing memory, everything will be released shortly */
				return SXERR_ABORT;
			}
		}else{
			pGen->pEnd = pCur;
			rc = jx9CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
			if( rc == SXERR_ABORT ){
				/* Don't worry about freeing memory, everything will be released shortly */
				return SXERR_ABORT;
			}
			pInstr = jx9VmPopInstr(pGen->pVm);
			if( pInstr->p3 ){
				/* Record key name */
				SyStringInitFromBuf(&pInfo->sKey, pInstr->p3, SyStrlen((const char *)pInstr->p3));
			}
			pInfo->iFlags |= JX9_4EACH_STEP_KEY;
		}
		pGen->pIn = &pCur[1]; /* Jump the arrow */
	}
	pGen->pEnd = pEnd;
	if( pGen->pIn >= pEnd ){
		rc = jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "foreach: Missing $value");
		if( rc == SXERR_ABORT ){
			/* Don't worry about freeing memory, everything will be released shortly */
			return SXERR_ABORT;
		}
		goto Synchronize;
	}
	/* Compile the expression holding the value name */
	rc = jx9CompileExpr(&(*pGen), 0, GenStateForEachNodeValidator);
	if( rc == SXERR_ABORT ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return SXERR_ABORT;
	}
	pInstr = jx9VmPopInstr(pGen->pVm);
	if( pInstr->p3 ){
		/* Record value name */
		SyStringInitFromBuf(&pInfo->sValue, pInstr->p3, SyStrlen((const char *)pInstr->p3));
	}
	/* Emit the 'FOREACH_INIT' instruction */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_FOREACH_INIT, 0, 0, pInfo, &nFalseJump);
	/* Save the instruction index so we can fix it later when the jump destination is resolved */
	GenStateNewJumpFixup(pForeachBlock, JX9_OP_FOREACH_INIT, nFalseJump);
	/* Record the first instruction to execute */
	pForeachBlock->nFirstInstr = jx9VmInstrLength(pGen->pVm);
	/* Emit the FOREACH_STEP instruction */
    jx9VmEmitInstr(pGen->pVm, JX9_OP_FOREACH_STEP, 0, 0, pInfo, &nFalseJump);
	/* Save the instruction index so we can fix it later when the jump destination is resolved */
	GenStateNewJumpFixup(pForeachBlock, JX9_OP_FOREACH_STEP, nFalseJump);
	/* Compile the loop body */
	pGen->pIn = &pEnd[1];
	pGen->pEnd = pTmp;
	rc = jx9CompileBlock(&(*pGen));
	if( rc == SXERR_ABORT ){
		/* Don't worry about freeing memory, everything will be released shortly */
		return SXERR_ABORT;
	}
	/* Emit the unconditional jump to the start of the loop */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_JMP, 0, pForeachBlock->nFirstInstr, 0, 0);
	/* Fix all jumps now the destination is resolved */
	GenStateFixJumps(pForeachBlock, -1,jx9VmInstrLength(pGen->pVm));
	/* Release the loop block */
	GenStateLeaveBlock(pGen, 0);
	/* Statement successfully compiled */
	return SXRET_OK;
Synchronize:
	/* Synchronize with the first semi-colon ';' so we can avoid 
	 * compiling this erroneous block.
	 */
	while( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & (JX9_TK_SEMI|JX9_TK_OCB)) == 0 ){
		pGen->pIn++;
	}
	return SXRET_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq $0x0, -0x30(%rbp)
movq 0x158(%rdi), %rax
movl 0x14(%rax), %r12d
leaq 0x20(%rax), %rcx
movq %rcx, 0x158(%rdi)
cmpq 0x160(%rdi), %rcx
jae 0x4a4ed
testb $0x2, 0x31(%rax)
jne 0x4a549
leaq 0x2b384(%rip), %rcx     # 0x75878
movq %rbx, %rdi
movl $0x1, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x483d1
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
cmpl $-0xa, %eax
je 0x4a57e
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
xorl %r14d, %r14d
cmpq %rax, %rcx
jae 0x4a57e
addq $0x20, %rcx
testl $0x40040, -0x10(%rcx)   # imm = 0x40040
jne 0x4a57e
movq %rcx, 0x158(%rbx)
leaq 0x20(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jb 0x4a52b
jmp 0x4a57e
addq $0x40, %rax
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
leaq -0x30(%rbp), %r8
movq %rbx, %rdi
movl $0x1, %esi
xorl %ecx, %ecx
callq 0x4bdf9
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
testl %eax, %eax
je 0x4a590
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x158(%rbx), %r15
movq 0x160(%rbx), %rdx
cmpq %rdx, %r15
jae 0x4a73a
movl $0x1, %eax
xorl %r13d, %r13d
movl 0x10(%r15,%r13), %ecx
btl $0x9, %ecx
jb 0x4a5cd
btl $0xa, %ecx
jae 0x4a5cf
cmpl $0x2, %eax
jl 0x4a79e
decl %eax
jmp 0x4a5cf
incl %eax
leaq (%r15,%r13), %rcx
addq $0x20, %rcx
addq $0x20, %r13
cmpq %rdx, %rcx
jb 0x4a5af
addq %r15, %r13
cmpq %rdx, %r13
jae 0x4a737
cmpq %r13, %r15
jae 0x4a77f
movq %r15, %r12
testb $0x4, 0x10(%r12)
je 0x4a608
cmpl $0xb, 0x18(%r12)
je 0x4a611
addq $0x20, %r12
cmpq %r13, %r12
jb 0x4a5f8
cmpq %r15, %r12
jbe 0x4a77f
movq %rdx, -0x50(%rbp)
movq %r12, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49789
cmpl $-0xa, %eax
je 0x4a57e
movq 0x158(%rbx), %r8
cmpq %r12, %r8
jae 0x4a683
leaq 0x2b286(%rip), %r15     # 0x758d3
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4a57e
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
cmpq %r12, %r8
jb 0x4a64d
leaq 0x20(%r12), %r15
movq %r15, 0x158(%rbx)
cmpq %r13, %r15
jb 0x4a6b8
movl 0x34(%r12), %edx
leaq 0x2b252(%rip), %rcx     # 0x758f2
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4a57e
movq (%rbx), %rdi
movl $0x50, %esi
callq 0x33d86
testq %rax, %rax
je 0x4a7a9
movq $-0x4, %rcx
movl $0x0, 0x4(%rax,%rcx)
addq $0x4, %rcx
cmpq $0x4c, %rcx
jb 0x4a6d5
movq (%rbx), %rdx
movabsq $0x800000000, %rcx      # imm = 0x800000000
movq %rcx, 0x3c(%rax)
xorl %ecx, %ecx
movl %ecx, 0x38(%rax)
movl %ecx, 0x44(%rax)
movq %rdx, 0x28(%rax)
movq %rcx, 0x30(%rax)
movq %rcx, 0x48(%rax)
cmpq %r13, %r15
jae 0x4a86d
addq $0x40, %r12
testb $0x2, -0xe(%r12)
jne 0x4a7ce
leaq 0x20(%r12), %rcx
cmpq %r13, %r12
movq %rcx, %r12
jb 0x4a719
jmp 0x4a86d
movq %r13, %r15
leaq 0x2b14d(%rip), %rcx     # 0x7588e
movq %rbx, %rdi
movl $0x1, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4a57e
movq %r15, 0x158(%rbx)
cmpq 0x160(%rbx), %r15
jae 0x4a777
addq $0x20, %r15
movq %r15, 0x158(%rbx)
xorl %r14d, %r14d
jmp 0x4a57e
movl 0x14(%r15), %edx
leaq 0x2b120(%rip), %rcx     # 0x758aa
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
jmp 0x4a50c
testq %r13, %r13
jne 0x4a5e0
jmp 0x4a73a
movq 0x158(%rbx), %rax
movl 0x14(%rax), %edx
leaq 0x2b15d(%rip), %rcx     # 0x75917
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
jmp 0x4a57e
movq %rax, %r15
movq 0x158(%rbx), %rax
leaq -0x20(%r12), %rcx
cmpq %rcx, %rax
jae 0x4a841
movq %rcx, 0x160(%rbx)
leaq 0x1ec9(%rip), %rdx      # 0x4c6b9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x49789
cmpl $-0xa, %eax
je 0x4a57e
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %ecx
movq 0x8(%rax), %rdx
decl %ecx
movl %ecx, 0x10(%rax)
imull 0x18(%rax), %ecx
movq 0x10(%rdx,%rcx), %rdi
testq %rdi, %rdi
je 0x4a83a
movq %r15, -0x40(%rbp)
movq %rdi, (%r15)
callq 0x2fa53
movq -0x40(%rbp), %rcx
movl %eax, 0x8(%rcx)
orb $0x1, 0x20(%r15)
jmp 0x4a863
movl 0x14(%rax), %edx
leaq 0x2b0f7(%rip), %rcx     # 0x75942
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
je 0x4a57e
movq %r12, 0x158(%rbx)
movq %r15, %rax
movq %r13, 0x160(%rbx)
movq 0x158(%rbx), %rcx
cmpq %r13, %rcx
jae 0x4a9e0
movq %rax, %r15
leaq 0x1e2b(%rip), %rdx      # 0x4c6b9
movq %rbx, %rdi
xorl %esi, %esi
callq 0x49789
cmpl $-0xa, %eax
je 0x4a57e
movq (%rbx), %rsi
movq 0xf0(%rsi), %rax
movl 0x10(%rax), %ecx
movq 0x8(%rax), %rdx
decl %ecx
movl %ecx, 0x10(%rax)
imull 0x18(%rax), %ecx
movq 0x10(%rdx,%rcx), %rdi
testq %rdi, %rdi
je 0x4a8da
movq %rdi, 0x10(%r15)
movq %rsi, -0x48(%rbp)
callq 0x2fa53
movq -0x48(%rbp), %rsi
movl %eax, 0x18(%r15)
leaq -0x54(%rbp), %r12
movq %rsi, %rdi
movl $0x43, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x48365
movq -0x30(%rbp), %rdi
movl (%r12), %eax
leaq -0x38(%rbp), %rsi
movl $0x43, (%rsi)
movl %eax, 0x4(%rsi)
addq $0x18, %rdi
callq 0x33094
movq (%rbx), %rdi
movq 0xf0(%rdi), %rax
movl 0x10(%rax), %eax
movq -0x30(%rbp), %rcx
movl %eax, 0x10(%rcx)
movl $0x44, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x48365
movq -0x30(%rbp), %rdi
movl (%r12), %eax
leaq -0x38(%rbp), %rcx
movl $0x44, (%rcx)
movl %eax, 0x4(%rcx)
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
callq 0x33094
addq $0x20, %r13
movq %r13, 0x158(%rbx)
movq -0x50(%rbp), %rax
movq %rax, 0x160(%rbx)
movq %rbx, %rdi
callq 0x4bed4
cmpl $-0xa, %eax
je 0x4a57e
movq (%rbx), %rdi
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
movl $0x8, %esi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x48365
movq -0x30(%rbp), %rdi
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4bff5
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x4a777
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x484ee
jmp 0x4a777
movl 0x14(%rcx), %edx
leaq 0x2af6e(%rip), %rcx     # 0x75958
jmp 0x4a78a