// File: /workspace/github_cpp_makefile_star2_O3/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 0x4e80(%rip), %rdi      # 0x813c
leaq 0x576a(%rip), %rbx      # 0x8a2d
movq %rbx, %rsi
callq 0x21d0
movq %rax, 0x8(%rsp)
leaq 0x4e73(%rip), %rdi      # 0x814a
movq %rbx, %rsi
callq 0x21d0
leaq 0x4e73(%rip), %rdi      # 0x8159
movl $0x1a, %esi
movl $0x1, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rcx
callq 0x2380
testq %rbp, %rbp
je 0x3692
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 0x3655
xorl %r15d, %r15d
movq (%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x37a9
leaq 0x4cd4(%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 0x4d75(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4d71(%rip), %rsi      # 0x8106
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x1, %r13d
jmp 0x34ab
movl $0x2, (%r12)
movl %ebx, 0x4(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4d2f(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4d39(%rip), %rsi      # 0x8114
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x2, %r13d
jmp 0x34ab
movq 0x8(%rax), %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x41e0
cmpl $-0x1, %eax
je 0x3451
movl $0x0, (%r12)
jmp 0x347b
movl $0x2, (%r12)
movl %ebx, 0x4(%r12)
movq 0x8(%rax), %rdx
xorl %r13d, %r13d
movq 0x8(%rsp), %rdi
leaq 0x4cc6(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4cb7(%rip), %rsi      # 0x80fb
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
jmp 0x34ab
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x41e0
cmpl $-0x1, %eax
je 0x37c6
movl $0x1, (%r12)
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x41e0
movl %eax, 0x4(%r12)
movq (%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x422e
movl %eax, %r13d
movq (%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x37a9
leaq 0x4b79(%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 0x4c09(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4c05(%rip), %rsi      # 0x8106
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x1, %r14d
jmp 0x361b
movl $0x2, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq 0x8(%rax), %rdx
movq 0x8(%rsp), %rdi
leaq 0x4bc2(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4bcc(%rip), %rsi      # 0x8114
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
movl $0x2, %r14d
jmp 0x361b
movq 0x8(%rax), %rsi
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x41e0
cmpl $-0x1, %eax
je 0x35c0
movl $0x0, 0x8(%r12)
jmp 0x35eb
movl $0x2, 0x8(%r12)
movl %ebx, 0xc(%r12)
movq 0x8(%rax), %rdx
xorl %r14d, %r14d
movq 0x8(%rsp), %rdi
leaq 0x4b57(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x10(%rsp), %rdi
leaq 0x4b48(%rip), %rsi      # 0x80fb
movl %ebx, %edx
xorl %eax, %eax
callq 0x2240
incl %ebx
jmp 0x361b
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x41e0
cmpl $-0x1, %eax
je 0x37e3
movl $0x1, 0x8(%r12)
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x41e0
movl %eax, 0xc(%r12)
movq (%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x422e
movl %eax, %r14d
cmpl %r14d, %r13d
jne 0x3743
movl %r13d, 0x10(%r12)
movq (%rbp), %rax
movl (%rax), %eax
decl %eax
cmpl $0x3, %eax
jae 0x3799
movl %eax, 0x14(%r12)
movq 0x8(%rbp), %rbp
addq $0x18, %r12
incl %r15d
testq %rbp, %rbp
jne 0x3340
jmp 0x3658
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 0x3321
movq 0x28(%rsp), %r15
jmp 0x3694
xorl %eax, %eax
movl %eax, 0x25d0(%r14)
movq 0x8(%rsp), %rdi
callq 0x2180
leaq 0x4ba0(%rip), %rdi      # 0x824c
movl $0x4, %esi
movl $0x1, %edx
movq 0x10(%rsp), %rbx
movq %rbx, %rcx
callq 0x2380
movq %rbx, %rdi
callq 0x2180
leaq 0x4a6a(%rip), %rdi      # 0x813c
leaq 0x53d9(%rip), %rsi      # 0x8ab2
callq 0x21d0
movq %rax, %rbx
leaq 0x4a62(%rip), %rsi      # 0x814a
leaq 0x4b62(%rip), %rdx      # 0x8251
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x4284
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x261e
movq %rbx, %rdi
callq 0x2180
leaq 0x4a23(%rip), %rdi      # 0x813c
callq 0x2320
leaq 0x4a25(%rip), %rdi      # 0x814a
callq 0x2320
leaq 0x50(%rsp), %rdi
callq 0x45b8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x88a6(%rip), %rdi      # 0xbff0
leaq 0x4a8c(%rip), %rsi      # 0x81dd
callq 0x2170
movq (%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x4a8b(%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 0x4a6e(%rip), %rsi      # 0x8202
movq %rax, %rdi
jmp 0x37b7
movq 0x8850(%rip), %rdi      # 0xbff0
leaq 0x4a76(%rip), %rsi      # 0x821d
jmp 0x37b7
movq 0x8840(%rip), %rdi      # 0xbff0
leaq 0x49de(%rip), %rsi      # 0x8195
callq 0x2170
movl $0x1, %edi
callq 0x21f0
movq 0x87f3(%rip), %rdi      # 0xbfc0
leaq 0x49a0(%rip), %rsi      # 0x8174
callq 0x2170
movq (%rbp), %rcx
movq 0x8(%rcx), %rcx
jmp 0x37fe
movq 0x87d6(%rip), %rdi      # 0xbfc0
leaq 0x4983(%rip), %rsi      # 0x8174
callq 0x2170
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x5354(%rip), %rsi      # 0x8b65
jmp 0x3794
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x45b8
movq %rbx, %rdi
callq 0x2310