// File: /workspace/github_c_makefile_star2_O0/anuwu[P]ERPLAG-Compiler/typeChecker.c
// Function: validateVar
tokenID validateVar (moduleST *baseModule , astNode *varASTNode, varST **searchedVar)
{
	int isValidVarIndex ;
	varST *locSearchedVar ;

	locSearchedVar = searchVar (baseModule, varASTNode->tok->lexeme) ;

	if (searchedVar != NULL)
		*searchedVar = locSearchedVar ;

	if (locSearchedVar == NULL)
	{
		errSemantic () ;
		#ifdef _WIN64
			printf ("In module \"%s\" at line %d, variable \"%s\" is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->tok->lexeme) ;
		#endif
		#if defined __linux__ || defined __MACH__
			printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " is undeclared\n", getParentModuleName (baseModule), varASTNode->tok->lineNumber, varASTNode->tok->lexeme) ;
		#endif
		realBase->semanticError = 1 ;

		if (varASTNode->child != NULL)
			validVarIndex (baseModule, varASTNode->child) ;

		return (tokenID)0 ;
	}
	else if (locSearchedVar->datatype != TK_ARRAY)
	{
		if (varASTNode->child != NULL)
		{
			errSemantic () ;
			#ifdef _WIN64
				printf ("In module \"%s\" at line %d, primitive \"%s\" variable \"%s\" cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->lexeme) ;
			#endif
			#if defined __linux__ || defined __MACH__
				printf ("In module " ANSI_BOLD "%s" ANSI_RESET " at line " ANSI_BOLD ANSI_CYAN "%d" ANSI_RESET ", " ANSI_BOLD ANSI_BLACK "primitive %s" ANSI_RESET " variable " ANSI_BOLD ANSI_RED "%s" ANSI_RESET " cannot be indexed\n", getParentModuleName (baseModule), varASTNode->child->tok->lineNumber, typeIDToString (locSearchedVar->datatype), locSearchedVar->lexeme) ;
			#endif
			realBase->semanticError = 1 ;

			validVarIndex (baseModule, varASTNode->child) ;
		}

		return locSearchedVar->datatype ;
	}
	else if (varASTNode->child != NULL)		// It is array and has an index
	{
		isValidVarIndex = validVarIndex(baseModule, varASTNode->child) ;	// 0 if invalid static/dyanmic, TK_NUM if valid static, TK_ID if valid dynamic

		if (!isValidVarIndex)
			realBase->semanticError = 1 ;
		else if (isVarStaticArr (locSearchedVar) && isValidVarIndex == TK_NUM)		// Static array
		{
			if (!validStaticArrStaticIndex (baseModule, locSearchedVar, varASTNode->child))
				realBase->semanticError = 1 ;
		}
		
		return locSearchedVar->arrayIndices->type ;
	}
	else
		return TK_ARRAY ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
callq 0x10ba0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x1423f
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, -0x30(%rbp)
jne 0x142ba
movb $0x0, %al
callq 0x14d10
movq -0x10(%rbp), %rdi
callq 0x116e0
movq %rax, %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %edx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rcx
leaq 0x4748(%rip), %rdi      # 0x189bf
movb $0x0, %al
callq 0x1090
leaq 0x22d83(%rip), %rax     # 0x37008
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x142ae
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x14010
movl $0x0, -0x4(%rbp)
jmp 0x143f3
movq -0x30(%rbp), %rax
cmpl $0x3b, 0x8(%rax)
je 0x14358
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x14349
movb $0x0, %al
callq 0x14d10
movq -0x10(%rbp), %rdi
callq 0x116e0
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x12370
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %edx
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq (%rax), %r8
leaq 0x4f43(%rip), %rdi      # 0x19260
movb $0x0, %al
callq 0x1090
leaq 0x22cdd(%rip), %rax     # 0x37008
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x14010
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x143f3
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
je 0x143ec
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x14010
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x14397
leaq 0x22c80(%rip), %rax     # 0x37008
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
jmp 0x143dd
movq -0x30(%rbp), %rdi
callq 0x13fc0
cmpl $0x0, %eax
je 0x143db
cmpl $0x71, -0x24(%rbp)
jne 0x143db
movq -0x10(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdx
callq 0x14120
cmpl $0x0, %eax
jne 0x143d9
leaq 0x22c3c(%rip), %rax     # 0x37008
movq (%rax), %rax
movl $0x1, 0xd8(%rax)
jmp 0x143db
jmp 0x143dd
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x143f3
movl $0x3b, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)