// File: /workspace/github_c_makefile_star2_O1/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: jx9CompileIf
static sxi32 jx9CompileIf(jx9_gen_state *pGen)
{
	SyToken *pToken, *pTmp, *pEnd = 0;
	GenBlock *pCondBlock = 0;
	sxu32 nJumpIdx;
	sxu32 nKeyID;
	sxi32 rc;
	/* Jump the 'if' keyword */
	pGen->pIn++;
	pToken = pGen->pIn; 
	/* Create the conditional block */
	rc = GenStateEnterBlock(&(*pGen), GEN_BLOCK_COND, jx9VmInstrLength(pGen->pVm), 0, &pCondBlock);
	if( rc != SXRET_OK ){
		return SXERR_ABORT;
	}
	/* Process as many [if/else if/elseif/else] blocks as we can */
	for(;;){
		if( pToken >= pGen->pEnd || (pToken->nType & JX9_TK_LPAREN) == 0 ){
			/* Syntax error */
			if( pToken >= pGen->pEnd ){
				pToken--;
			}
			rc = jx9GenCompileError(pGen, E_ERROR, pToken->nLine, "if/else/elseif: Missing '('");
			if( rc == SXERR_ABORT ){
				/* Error count limit reached, abort immediately */
				return SXERR_ABORT;
			}
			goto Synchronize;
		}
		/* Jump the left parenthesis '(' */
		pToken++; 
		/* Delimit the condition */
		jx9DelimitNestedTokens(pToken, pGen->pEnd, JX9_TK_LPAREN /* '(' */, JX9_TK_RPAREN /* ')' */, &pEnd);
		if( pToken >= pEnd || (pEnd->nType & JX9_TK_RPAREN) == 0 ){
			/* Syntax error */
			if( pToken >= pGen->pEnd ){
				pToken--;
			}
			rc = jx9GenCompileError(pGen, E_ERROR, pToken->nLine, "if/else/elseif: Missing ')'");
			if( rc == SXERR_ABORT ){
				/* Error count limit reached, abort immediately */
				return SXERR_ABORT;
			}
			goto Synchronize;
		}
		/* Swap token streams */
		SWAP_TOKEN_STREAM(pGen, pToken, pEnd);
		/* Compile the condition */
		rc = jx9CompileExpr(&(*pGen), 0, 0);
		/* Update token stream */
		while(pGen->pIn < pEnd ){
			jx9GenCompileError(&(*pGen), E_ERROR, pGen->pIn->nLine, "Unexpected token '%z'", &pGen->pIn->sData);
			pGen->pIn++;
		}
		pGen->pIn  = &pEnd[1];
		pGen->pEnd = pTmp;
		if( rc == SXERR_ABORT ){
			/* Expression handler request an operation abort [i.e: Out-of-memory] */
			return SXERR_ABORT;
		}
		/* Emit the false jump */
		jx9VmEmitInstr(pGen->pVm, JX9_OP_JZ, 0, 0, 0, &nJumpIdx);
		/* Save the instruction index so we can fix it later when the jump destination is resolved */
		GenStateNewJumpFixup(pCondBlock, JX9_OP_JZ, nJumpIdx);
		/* Compile the body */
		rc = jx9CompileBlock(&(*pGen));
		if( rc == SXERR_ABORT ){
			return SXERR_ABORT;
		}
		if( pGen->pIn >= pGen->pEnd || (pGen->pIn->nType & JX9_TK_KEYWORD) == 0 ){
			break;
		}
		/* Ensure that the keyword ID is 'else if' or 'else' */
		nKeyID = (sxu32)SX_PTR_TO_INT(pGen->pIn->pUserData);
		if( (nKeyID & (JX9_TKWRD_ELSE|JX9_TKWRD_ELIF)) == 0 ){
			break;
		}
		/* Emit the unconditional jump */
		jx9VmEmitInstr(pGen->pVm, JX9_OP_JMP, 0, 0, 0, &nJumpIdx);
		/* Save the instruction index so we can fix it later when the jump destination is resolved */
		GenStateNewJumpFixup(pCondBlock, JX9_OP_JMP, nJumpIdx);
		if( nKeyID & JX9_TKWRD_ELSE ){
			pToken = &pGen->pIn[1];
			if( pToken >= pGen->pEnd || (pToken->nType & JX9_TK_KEYWORD) == 0 ||
				SX_PTR_TO_INT(pToken->pUserData) != JX9_TKWRD_IF ){
					break;
			}
			pGen->pIn++; /* Jump the 'else' keyword */
		}
		pGen->pIn++; /* Jump the 'elseif/if' keyword */
		/* Synchronize cursors */
		pToken = pGen->pIn;
		/* Fix the false jump */
		GenStateFixJumps(pCondBlock, JX9_OP_JZ, jx9VmInstrLength(pGen->pVm));
	} /* For(;;) */
	/* Fix the false jump */
	GenStateFixJumps(pCondBlock, JX9_OP_JZ, jx9VmInstrLength(pGen->pVm));
	if( pGen->pIn < pGen->pEnd && (pGen->pIn->nType & JX9_TK_KEYWORD) &&
		(SX_PTR_TO_INT(pGen->pIn->pUserData) & JX9_TKWRD_ELSE) ){
			/* Compile the else block */
			pGen->pIn++;
			rc = jx9CompileBlock(&(*pGen));
			if( rc == SXERR_ABORT ){
				
				return SXERR_ABORT;
			}
	}
	nJumpIdx = jx9VmInstrLength(pGen->pVm);
	/* Fix all unconditional jumps now the destination is resolved */
	GenStateFixJumps(pCondBlock, JX9_OP_JMP, nJumpIdx);
	/* Release the conditional 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 $0x18, %rsp
movq %rdi, %rbx
leaq -0x38(%rbp), %r8
movq $0x0, (%r8)
movq (%rdi), %rax
movq 0x158(%rdi), %r12
addq $0x20, %r12
movq %r12, 0x158(%rdi)
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl $0x4, %esi
xorl %ecx, %ecx
callq 0x4cf52
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
testl %eax, %eax
je 0x4b129
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x160(%rbx), %r13
movq $-0x20, %rax
cmpq %r13, %r12
jae 0x4b42e
leaq 0x2cb26(%rip), %r15     # 0x77c6d
testb $0x2, 0x11(%r12)
je 0x4b426
leaq 0x20(%r12), %rax
movl $0x20, %r14d
cmpq %r13, %rax
jae 0x4b195
movl $0x1, %ecx
movl 0x10(%r12,%r14), %edx
btl $0x9, %edx
jb 0x4b182
btl $0xa, %edx
jae 0x4b184
cmpl $0x2, %ecx
jl 0x4b195
decl %ecx
jmp 0x4b184
incl %ecx
leaq (%r12,%r14), %rdx
addq $0x20, %rdx
addq $0x20, %r14
cmpq %r13, %rdx
jb 0x4b168
cmpq $0x21, %r14
jl 0x4b3e8
addq %r12, %r14
testb $0x4, 0x11(%r14)
je 0x4b3e8
movq %rax, 0x158(%rbx)
movq %r14, 0x160(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4a7a9
movl %eax, %r12d
movq 0x158(%rbx), %r8
cmpq %r14, %r8
jae 0x4b200
movl 0x14(%r8), %edx
movq %rbx, %rdi
movl $0x1, %esi
movq %r15, %rcx
xorl %eax, %eax
callq 0x49434
movq 0x158(%rbx), %r8
addq $0x20, %r8
movq %r8, 0x158(%rbx)
jmp 0x4b1d1
addq $0x20, %r14
movq %r14, 0x158(%rbx)
movq %r13, 0x160(%rbx)
cmpl $-0xa, %r12d
je 0x4b41b
movq (%rbx), %rdi
movl $0x9, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
leaq -0x2c(%rbp), %r12
movq %r12, %r9
callq 0x493c1
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl $0x9, -0x40(%rbp)
movl %eax, -0x3c(%rbp)
addq $0x18, %rdi
leaq -0x40(%rbp), %r13
movq %r13, %rsi
callq 0x336fc
movq %rbx, %rdi
callq 0x4d031
cmpl $-0xa, %eax
je 0x4b41b
movq 0x158(%rbx), %rax
cmpq 0x160(%rbx), %rax
jae 0x4b349
testb $0x4, 0x10(%rax)
je 0x4b349
movl 0x18(%rax), %r14d
testl $0xc000000, %r14d       # imm = 0xC000000
je 0x4b349
movq (%rbx), %rdi
movl $0x8, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %r12, %r9
callq 0x493c1
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movl $0x8, -0x40(%rbp)
movl %eax, -0x3c(%rbp)
addq $0x18, %rdi
movq %r13, %rsi
callq 0x336fc
btl $0x1b, %r14d
jae 0x4b2fa
movq 0x158(%rbx), %rcx
leaq 0x20(%rcx), %rax
cmpq 0x160(%rbx), %rax
jae 0x4b349
testb $0x4, 0x30(%rcx)
je 0x4b349
cmpl $0x3, 0x38(%rcx)
jne 0x4b349
movq %rax, 0x158(%rbx)
movq (%rbx), %rax
movq 0x158(%rbx), %r12
addq $0x20, %r12
movq %r12, 0x158(%rbx)
movq -0x38(%rbp), %rdi
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl $0x9, %esi
callq 0x4d152
movq 0x160(%rbx), %r13
cmpq %r13, %r12
jb 0x4b147
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
movq $-0x20, %rax
jmp 0x4b42e
movq -0x38(%rbp), %rdi
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl $0x9, %esi
callq 0x4d152
movq 0x158(%rbx), %rax
cmpq 0x160(%rbx), %rax
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
jae 0x4b3a2
testb $0x4, 0x10(%rax)
je 0x4b3a2
testb $0x8, 0x1b(%rax)
je 0x4b3a2
addq $0x20, %rax
movq %rax, 0x158(%rbx)
movq %rbx, %rdi
callq 0x4d031
cmpl $-0xa, %eax
je 0x4b117
movq (%rbx), %rax
movq 0xf0(%rax), %rax
movl 0x10(%rax), %edx
movl %edx, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x4d152
movq 0xc8(%rbx), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x4b117
movq 0x8(%rdi), %rax
movq %rax, 0xc8(%rbx)
callq 0x4955d
jmp 0x4b117
cmpq %r13, %rax
cmovbq %rax, %r12
movl 0x14(%r12), %edx
leaq 0x2c337(%rip), %rcx     # 0x77732
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
je 0x4b117
jmp 0x4b452
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
jmp 0x4b117
xorl %eax, %eax
movl $0xfffffff6, %r14d      # imm = 0xFFFFFFF6
movl 0x14(%r12,%rax), %edx
leaq 0x2c2dc(%rip), %rcx     # 0x77716
movq %rbx, %rdi
movl $0x1, %esi
xorl %eax, %eax
callq 0x49434
cmpl $-0xa, %eax
je 0x4b117
movq 0x158(%rbx), %rcx
movq 0x160(%rbx), %rax
xorl %r14d, %r14d
cmpq %rax, %rcx
jae 0x4b117
addq $0x20, %rcx
testl $0x40040, -0x10(%rcx)   # imm = 0x40040
jne 0x4b117
movq %rcx, 0x158(%rbx)
leaq 0x20(%rcx), %rdx
cmpq %rax, %rcx
movq %rdx, %rcx
jb 0x4b470
jmp 0x4b117