// File: /workspace/github_cpp_makefile_star2_O1/PrarabdhJoshi[P]DatabaseImplementation/Comparison.cc
// Function: CNF::GrowFromParseTree(AndList*, Schema*, Schema*, Record&)
void CNF :: GrowFromParseTree (struct AndList *parseTree, Schema *leftSchema, 
	Schema *rightSchema, Record &literal) {

	CNF &cnf = *this;

	// as kind of a hack, the literal record is built up insiide of a text file,
	// where it will be read in from subsequently
	FILE *outRecFile = fopen ("sdafdsfFFDSDA", "w");

	// also as kind of a hack, the schema for the literal record is built up
	// inside of a text file, where it will also be read from subsequently
	FILE *outSchemaFile = fopen ("hkljdfgkSDFSDF", "w");
	fprintf (outSchemaFile, "BEGIN\ntempSchema\nwherever\n");

	// this tells us the size of the literal record
	int numFieldsInLiteral = 0;

	// now we go through and build the comparison structure
	for (int whichAnd = 0; 1; whichAnd++, parseTree = parseTree->rightAnd) {
		
		// see if we have run off of the end of all of the ANDs
		if (parseTree == NULL) {
			cnf.numAnds = whichAnd;
			break;
		}	

		// we have not, so copy over all of the ORs hanging off of this AND
		struct OrList *myOr = parseTree->left;
		for (int whichOr = 0; 1; whichOr++, myOr = myOr->rightOr) {

			// see if we have run off of the end of the ORs
			if (myOr == NULL) {
				cnf.orLens[whichAnd] = whichOr;
				break;
			}

			// we have not run off the list, so add the current OR in!
			
			// these store the types of the two values that are found
			Type typeLeft;
			Type typeRight;


			// first thing is to deal with the left operand
			// so we check to see if it is an attribute name, and if so,
			// we look it up in the schema
			if (myOr->left->left->code == NAME) {
				
				// see if we can find this attribute in the left schema
				if (leftSchema->Find (myOr->left->left->value) != -1) {
					cnf.orList[whichAnd][whichOr].operand1 = Left;
					cnf.orList[whichAnd][whichOr].whichAtt1 =
						leftSchema->Find (myOr->left->left->value);	
					typeLeft = leftSchema->FindType (myOr->left->left->value);

				// see if we can find it in the right schema
				} else if (rightSchema->Find (myOr->left->left->value) != -1) {
                                        cnf.orList[whichAnd][whichOr].operand1 = Right;
                                        cnf.orList[whichAnd][whichOr].whichAtt1 =
                                                rightSchema->Find (myOr->left->left->value);
                                        typeLeft = rightSchema->FindType (myOr->left->left->value);

				// it is not there!  So there is an error in the query
                                } else {
					cout << "ERROR: Could not find attribute " <<
						myOr->left->left->value << "\n";
					exit (1);	
				}

			// the next thing is to see if we have a string; if so, add it to the 
			// literal record that stores all of the comparison values
			} else if (myOr->left->left->code == STRING) {

				cnf.orList[whichAnd][whichOr].operand1 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt1 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->left->value, String);
				typeLeft = String;

			// see if it is an integer
			} else if (myOr->left->left->code == INT) {

				cnf.orList[whichAnd][whichOr].operand1 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt1 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->left->value, Int);
				typeLeft = Int;

			// see if it is a double
			} else if (myOr->left->left->code == DOUBLE) {

				cnf.orList[whichAnd][whichOr].operand1 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt1 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->left->value, Double);
				typeLeft = Double;
	
			// catch-all case
			} else {
				cerr << "You gave me some strange type for an operand that I do not recognize!!\n";
				exit (1);
			}

			// now that we have dealt with the left operand, we need to deal with the
			// right operand
			if (myOr->left->right->code == NAME) {
				
				// see if we can find this attribute in the left schema
				if (leftSchema->Find (myOr->left->right->value) != -1) {
					cnf.orList[whichAnd][whichOr].operand2 = Left;
					cnf.orList[whichAnd][whichOr].whichAtt2 =
						leftSchema->Find (myOr->left->right->value);	
					typeRight = leftSchema->FindType (myOr->left->right->value);

				// see if we can find it in the right schema
				} else if (rightSchema->Find (myOr->left->right->value) != -1) {
                                        cnf.orList[whichAnd][whichOr].operand2 = Right;
                                        cnf.orList[whichAnd][whichOr].whichAtt2 =
                                                rightSchema->Find (myOr->left->right->value);
					typeRight = rightSchema->FindType (myOr->left->right->value);

				// it is not there!  So there is an error in the query
                                } else {
					cout << "ERROR: Could not find attribute " << myOr->left->right->value << "\n";
					exit (1);	
				}

			// the next thing is to see if we have a string; if so, add it to the 
			// literal record that stores all of the comparison values
			} else if (myOr->left->right->code == STRING) {

				cnf.orList[whichAnd][whichOr].operand2 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt2 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->right->value, String);
				typeRight = String;

			// see if it is an integer
			} else if (myOr->left->right->code == INT) {

				cnf.orList[whichAnd][whichOr].operand2 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt2 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->right->value, Int);
				typeRight = Int;

			// see if it is a double
			} else if (myOr->left->right->code == DOUBLE) {

				cnf.orList[whichAnd][whichOr].operand2 = Literal;
				cnf.orList[whichAnd][whichOr].whichAtt2 = numFieldsInLiteral;
				AddLitToFile (numFieldsInLiteral, outRecFile, outSchemaFile, myOr->left->right->value, Double);
				typeRight = Double;
	
			// catch-all case
			} else {
				cerr << "You gave me some strange type for an operand that I do not recognize!!\n";
				exit (1);
			}

			

			// now we check to make sure that there was not a type mismatch
			if (typeLeft != typeRight) {
				cerr << "ERROR! Type mismatch in CNF.  " << myOr->left->left->value << " and "
					<< myOr->left->right->value << " were found to not match.\n";
				exit (1);
			}

			// set up the type info for this comparison
			cnf.orList[whichAnd][whichOr].attType = typeLeft;

			// and finally set up the comparison operator for this comparison
			if (myOr->left->code == LESS_THAN) {
				cnf.orList[whichAnd][whichOr].op = LessThan;	
			} else if (myOr->left->code == GREATER_THAN) {
				cnf.orList[whichAnd][whichOr].op = GreaterThan;	
			} else if (myOr->left->code == EQUALS) {
				cnf.orList[whichAnd][whichOr].op = Equals;
			} else {
				cerr << "BAD: found a comparison op I don't recognize.\n";
				exit (1);
			}
		
		}
	}

	// the very last thing is to set up the literal record; first close the
	// file where its information has been stored
	fclose (outRecFile);
	fprintf (outSchemaFile, "END\n");
	fclose (outSchemaFile);

	// and open up the record file
	outRecFile = fopen ("sdafdsfFFDSDA", "r");

	// read in the record's schema
	Schema mySchema("hkljdfgkSDFSDF", "tempSchema");

	// and get the record
	literal.SuckNextRecord (&mySchema, outRecFile);

	// close the record file
	fclose (outRecFile);

	remove("sdafdsfFFDSDA");
	remove("hkljdfgkSDFSDF");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x4e06(%rip), %rdi      # 0x813c
leaq 0x56e0(%rip), %rbx      # 0x8a1d
movq %rbx, %rsi
callq 0x21d0
movq %rax, 0x8(%rsp)
leaq 0x4df9(%rip), %rdi      # 0x814a
movq %rbx, %rsi
callq 0x21d0
leaq 0x4df9(%rip), %rdi      # 0x8159
movl $0x1a, %esi
movl $0x1, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rcx
callq 0x2380
testq %rbp, %rbp
je 0x370c
movq %r13, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movq %r14, %r12
xorl %eax, %eax
xorl %ebx, %ebx
movq %r14, 0x30(%rsp)
movq %rax, 0x40(%rsp)
movq %rbp, 0x48(%rsp)
movq (%rbp), %rbp
testq %rbp, %rbp
movq %r12, 0x38(%rsp)
je 0x36cf
xorl %r15d, %r15d
movq (%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3823
leaq 0x4c5a(%rip), %rdx      # 0x8030
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, (%r12)
movl %ebx, 0x4(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4cfb(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4cf7(%rip), %rsi      # 0x8106
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x1, %r13d
jmp 0x3525
movl $0x2, (%r12)
movl %ebx, 0x4(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4cb5(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4cbf(%rip), %rsi      # 0x8114
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x2, %r13d
jmp 0x3525
movq 0x8(%rax), %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x34cb
movl $0x0, (%r12)
jmp 0x34f5
movl $0x2, (%r12)
movl %ebx, 0x4(%r12)
movq 0x8(%rax), %rdx
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
leaq 0x4c4c(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4c3d(%rip), %rsi      # 0x80fb
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
jmp 0x3525
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x3840
movl $0x1, (%r12)
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x42f0
movl %eax, 0x4(%r12)
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x433e
movl %eax, %r13d
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3823
leaq 0x4aff(%rip), %rdx      # 0x8040
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4b8f(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4b8b(%rip), %rsi      # 0x8106
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x1, %r14d
jmp 0x3695
movl $0x2, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4b48(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4b52(%rip), %rsi      # 0x8114
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x2, %r14d
jmp 0x3695
movq 0x8(%rax), %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x363a
movl $0x0, 0x8(%r12)
jmp 0x3665
movl $0x2, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq 0x8(%rax), %rdx
xorl %r14d, %r14d
movq 0x8(%rsp), %rdi
leaq 0x4add(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4ace(%rip), %rsi      # 0x80fb
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
jmp 0x3695
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x385d
movl $0x1, 0x8(%r12)
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x42f0
movl %eax, 0xc(%r12)
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x433e
movl %eax, %r14d
cmpl %r14d, %r13d
jne 0x37bd
movl %r13d, 0x10(%r12)
movq (%rbp), %rax
movl (%rax), %eax
decl %eax
cmpl $0x3, %eax
jae 0x3813
movl %eax, 0x14(%r12)
movq 0x8(%rbp), %rbp
addq $0x18, %r12
incl %r15d
testq %rbp, %rbp
jne 0x33ba
jmp 0x36d2
xorl %r15d, %r15d
movq 0x30(%rsp), %r14
movq 0x40(%rsp), %rax
movl %r15d, 0x2580(%r14,%rax,4)
incq %rax
movq 0x48(%rsp), %rbp
movq 0x8(%rbp), %rbp
movq 0x38(%rsp), %r12
addq $0x1e0, %r12            # imm = 0x1E0
testq %rbp, %rbp
jne 0x339b
movq 0x28(%rsp), %r15
jmp 0x370e
xorl %eax, %eax
movl %eax, 0x25d0(%r14)
movq 0x8(%rsp), %rdi
callq 0x2180
leaq 0x4b26(%rip), %rdi      # 0x824c
movl $0x4, %esi
movl $0x1, %edx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
callq 0x2380
movq %rbx, %rdi
callq 0x2180
leaq 0x49f0(%rip), %rdi      # 0x813c
leaq 0x534f(%rip), %rsi      # 0x8aa2
callq 0x21d0
movq %rax, %rbx
leaq 0x49e8(%rip), %rsi      # 0x814a
leaq 0x4ae8(%rip), %rdx      # 0x8251
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x4394
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x261e
movq %rbx, %rdi
callq 0x2180
leaq 0x49a9(%rip), %rdi      # 0x813c
callq 0x2320
leaq 0x49ab(%rip), %rdi      # 0x814a
callq 0x2320
leaq 0x50(%rsp), %rdi
callq 0x46c8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x882c(%rip), %rdi      # 0xbff0
leaq 0x4a12(%rip), %rsi      # 0x81dd
callq 0x2170
movq (%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x4a11(%rip), %rsi      # 0x81fc
movq %rax, %rdi
callq 0x2170
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x49f4(%rip), %rsi      # 0x8202
movq %rax, %rdi
jmp 0x3831
movq 0x87d6(%rip), %rdi      # 0xbff0
leaq 0x49fc(%rip), %rsi      # 0x821d
jmp 0x3831
movq 0x87c6(%rip), %rdi      # 0xbff0
leaq 0x4964(%rip), %rsi      # 0x8195
callq 0x2170
movl $0x1, %edi
callq 0x21f0
movq 0x8779(%rip), %rdi      # 0xbfc0
leaq 0x4926(%rip), %rsi      # 0x8174
callq 0x2170
movq (%rbp), %rcx
movq 0x8(%rcx), %rcx
jmp 0x3878
movq 0x875c(%rip), %rdi      # 0xbfc0
leaq 0x4909(%rip), %rsi      # 0x8174
callq 0x2170
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x52ca(%rip), %rsi      # 0x8b55
jmp 0x380e
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x46c8
movq %rbx, %rdi
callq 0x2310