// File: /workspace/github_c_makefile_star2_O3/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileFor
static sxi32 jx9CompileFor(jx9_gen_state *pGen)
{
	SyToken *pTmp, *pPostStart, *pEnd = 0;
	GenBlock *pForBlock = 0;
	sxu32 nFalseJump;
	sxu32 nLine;
	sxi32 rc;
	nLine = pGen->pIn->nLine;
	/* Jump the 'for' keyword */
	pGen->pIn++;    
	if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_LPAREN) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, nLine, "Expected '(' after 'for' keyword");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		return SXRET_OK;
	}
	/* Jump the left parenthesis '(' */
	pGen->pIn++; 
	/* Delimit the init-expr;condition;post-expr */
	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, "for: Invalid 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;
	}
	/* Swap token streams */
	pTmp = pGen->pEnd;
	pGen->pEnd = pEnd;
	/* Compile initialization expressions if available */
	rc = jx9CompileExpr(&(*pGen), 0, 0);
	/* Pop operand lvalues */
	if( rc == SXERR_ABORT ){
		/* Expression handler request an operation abort [i.e: Out-of-memory] */
		return SXERR_ABORT;
	}else if( rc != SXERR_EMPTY ){
		jx9VmEmitInstr(pGen->pVm, JX9_OP_POP, 1, 0, 0, 0);
	}
	if( (pGen->pIn->nType & JX9_TK_SEMI) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, 
			"for: Expected ';' after initialization expressions");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		return SXRET_OK;
	}
	/* Jump the trailing ';' */
	pGen->pIn++;
	/* Create the loop block */
	rc = GenStateEnterBlock(&(*pGen), GEN_BLOCK_LOOP, jx9VmInstrLength(pGen->pVm), 0, &pForBlock);
	if( rc != SXRET_OK ){
		return SXERR_ABORT;
	}
	/* Deffer continue jumps */
	pForBlock->bPostContinue = TRUE;
	/* Compile the condition */
	rc = jx9CompileExpr(&(*pGen), 0, 0);
	if( rc == SXERR_ABORT ){
		/* Expression handler request an operation abort [i.e: Out-of-memory] */
		return SXERR_ABORT;
	}else if( rc != SXERR_EMPTY ){
		/* Emit the false jump */
		jx9VmEmitInstr(pGen->pVm, JX9_OP_JZ, 0, 0, 0, &nFalseJump);
		/* Save the instruction index so we can fix it later when the jump destination is resolved */
		GenStateNewJumpFixup(pForBlock, JX9_OP_JZ, nFalseJump);
	}
	if( (pGen->pIn->nType & JX9_TK_SEMI) == 0 ){
		/* Syntax error */
		rc = jx9GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, 
			"for: Expected ';' after conditionals expressions");
		if( rc == SXERR_ABORT ){
			/* Error count limit reached, abort immediately */
			return SXERR_ABORT;
		}
		return SXRET_OK;
	}
	/* Jump the trailing ';' */
	pGen->pIn++;
	/* Save the post condition stream */
	pPostStart = pGen->pIn;
	/* Compile the loop body */
	pGen->pIn  = &pEnd[1]; /* Jump the trailing parenthesis ')' */
	pGen->pEnd = pTmp;
	rc = jx9CompileBlock(&(*pGen));
	if( rc == SXERR_ABORT ){
		return SXERR_ABORT;
	}
	/* Fix post-continue jumps */
	if( SySetUsed(&pForBlock->aPostContFix) > 0 ){
		JumpFixup *aPost;
		VmInstr *pInstr;
		sxu32 nJumpDest;
		sxu32 n;
		aPost = (JumpFixup *)SySetBasePtr(&pForBlock->aPostContFix);
		nJumpDest = jx9VmInstrLength(pGen->pVm);
		for( n = 0 ; n < SySetUsed(&pForBlock->aPostContFix) ; ++n ){
			pInstr = jx9VmGetInstr(pGen->pVm, aPost[n].nInstrIdx);
			if( pInstr ){
				/* Fix jump */
				pInstr->iP2 = nJumpDest;
			}
		}
	}
	/* compile the post-expressions if available */
	while( pPostStart < pEnd && (pPostStart->nType & JX9_TK_SEMI) ){
		pPostStart++;
	}
	if( pPostStart < pEnd ){
		SyToken *pTmpIn, *pTmpEnd;
		SWAP_DELIMITER(pGen, pPostStart, pEnd);
		rc = jx9CompileExpr(&(*pGen), 0, 0);
		if( pGen->pIn < pGen->pEnd ){
			/* Syntax error */
			rc = jx9GenCompileError(pGen, E_ERROR, pGen->pIn->nLine, "for: Expected ')' after post-expressions");
			if( rc == SXERR_ABORT ){
				/* Error count limit reached, abort immediately */
				return SXERR_ABORT;
			}
			return SXRET_OK;
		}
		RE_SWAP_DELIMITER(pGen);
		if( rc == SXERR_ABORT ){
			/* Expression handler request an operation abort [i.e: Out-of-memory] */
			return SXERR_ABORT;
		}else if( rc != SXERR_EMPTY){
			/* Pop operand lvalue */
			jx9VmEmitInstr(pGen->pVm, JX9_OP_POP, 1, 0, 0, 0);
		}
	}
	/* Emit the unconditional jump to the start of the loop */
	jx9VmEmitInstr(pGen->pVm, JX9_OP_JMP, 0, pForBlock->nFirstInstr, 0, 0);
	/* Fix all jumps now the destination is resolved */
	GenStateFixJumps(pForBlock, -1, jx9VmInstrLength(pGen->pVm));
	/* Release the loop block */
	GenStateLeaveBlock(pGen, 0);
	/* Statement successfully compiled */
	return SXRET_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq $0x0, -0x30(%rbp)
movq 0x158(%rdi), %rsi
movq 0x160(%rdi), %r13
movl 0x14(%rsi), %edx
leaq 0x20(%rsi), %rax
movq %rax, 0x158(%rdi)
cmpq %r13, %rax
jae 0x4acbc
testb $0x2, 0x31(%rsi)
jne 0x4acee
leaq 0x2ad33(%rip), %rcx     # 0x759f6
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
cmpl $-0xa, %eax
cmovel %eax, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x40(%rsi), %r12
movq %r12, 0x158(%rbx)
cmpq %r13, %r12
jae 0x4ad9a
addq $0x40, %rsi
movl $0x1, %ecx
xorl %eax, %eax
movq %rsi, %r12
movl 0x10(%r12), %esi
btl $0x9, %esi
jb 0x4ad2a
btl $0xa, %esi
jae 0x4ad2c
cmpl $0x2, %ecx
jl 0x4ad95
decl %ecx
jmp 0x4ad2c
incl %ecx
addq $-0x20, %rax
addq $0x20, %r12
cmpq %r13, %r12
jb 0x4ad10
cmpq %r13, %r12
jae 0x4ad9a
movq %r12, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49789
movl %eax, %r14d
cmpl $-0xa, %eax
je 0x4acdc
cmpl $-0x3, %r14d
je 0x4ad79
movq (%rbx), %rdi
movl $0xb, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x48365
movq 0x158(%rbx), %rax
testb $0x4, 0x12(%rax)
jne 0x4ade2
movl 0x14(%rax), %edx
leaq 0x2ac9f(%rip), %rcx     # 0x75a2f
jmp 0x4acc3
testq %rax, %rax
jne 0x4ad39
leaq 0x2ac76(%rip), %rcx     # 0x75a17
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x483d1
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
cmpl $-0xa, %eax
je 0x4acdc
movq %r12, 0x158(%rbx)
cmpq 0x160(%rbx), %r12
jae 0x4adda
addq $0x20, %r12
movq %r12, 0x158(%rbx)
xorl %r14d, %r14d
jmp 0x4acdc
addq $0x20, %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
jne 0x4acdc
movq -0x30(%rbp), %rax
movb $0x1, 0x48(%rax)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49789
cmpl $-0xa, %eax
je 0x4aff5
cmpl $-0x3, %eax
je 0x4ae75
movq (%rbx), %rdi
leaq -0x34(%rbp), %r15
movl $0x9, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x48365
movq -0x30(%rbp), %rdi
movl (%r15), %eax
leaq -0x3c(%rbp), %rsi
movl $0x9, (%rsi)
movl %eax, 0x4(%rsi)
addq $0x18, %rdi
callq 0x33094
movq 0x158(%rbx), %r15
testb $0x4, 0x12(%r15)
jne 0x4ae93
movl 0x14(%r15), %edx
leaq 0x2abd4(%rip), %rcx     # 0x75a62
jmp 0x4acc3
leaq 0x20(%r12), %rax
movq %rax, 0x158(%rbx)
movq %r13, 0x160(%rbx)
movq %rbx, %rdi
callq 0x4bed4
cmpl $-0xa, %eax
je 0x4acdc
movq -0x30(%rbp), %rcx
movl 0x60(%rcx), %eax
testq %rax, %rax
je 0x4aefb
movq 0x58(%rcx), %rcx
movq (%rbx), %rdx
movq 0xf0(%rdx), %rdx
movl 0x10(%rdx), %esi
xorl %edi, %edi
movl 0x4(%rcx,%rdi,8), %r8d
cmpl %r8d, %esi
jbe 0x4aef3
movq 0x8(%rdx), %r9
testq %r9, %r9
je 0x4aef3
imull 0x18(%rdx), %r8d
movl %esi, 0x8(%r9,%r8)
incq %rdi
cmpq %rdi, %rax
jne 0x4aed6
addq $0x20, %r15
cmpq %r12, %r15
jae 0x4af96
leaq 0x20(%r15), %rax
testb $0x4, 0x12(%r15)
movq %rax, %r15
jne 0x4aeff
movq 0x158(%rbx), %r15
movq 0x160(%rbx), %r13
addq $-0x20, %rax
movq %rax, 0x158(%rbx)
movq %r12, 0x160(%rbx)
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x49789
movq 0x158(%rbx), %rcx
cmpq 0x160(%rbx), %rcx
jae 0x4af64
movl 0x14(%rcx), %edx
leaq 0x2ab34(%rip), %rcx     # 0x75a93
jmp 0x4acc6
movq %r15, 0x158(%rbx)
movq %r13, 0x160(%rbx)
cmpl $-0xa, %eax
je 0x4aff5
cmpl $-0x3, %eax
je 0x4af96
movq (%rbx), %rdi
movl $0xb, %esi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x48365
movq (%rbx), %rdi
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
xorl %r14d, %r14d
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 0x4acdc
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x484ee
jmp 0x4acdc
movl %eax, %r14d
jmp 0x4acdc