// File: /workspace/github_cpp_makefile_star2_O2/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, 0x38(%rsp)
movq %rcx, %r15
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r12
leaq 0x4000(%rip), %rdi      # 0x713c
leaq 0x48ea(%rip), %r14      # 0x7a2d
movq %r14, %rsi
callq 0x21a0
movq %rax, 0x10(%rsp)
leaq 0x3ff3(%rip), %rdi      # 0x714a
movq %r14, %rsi
callq 0x21a0
leaq 0x3ff3(%rip), %rdi      # 0x7159
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rbx
movq %rax, %rcx
callq 0x2340
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
movq $0x0, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r12, %rax
movq %rbp, %r12
movq %r15, 0x28(%rsp)
movq %r13, 0x30(%rsp)
testq %r12, %r12
je 0x3451
xorl %r13d, %r13d
movq %rax, 0x40(%rsp)
movq %rax, %r15
movq %r12, 0x48(%rsp)
movq (%r12), %r12
testq %r12, %r12
je 0x341d
movq (%r12), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3568
leaq 0x3e51(%rip), %rdx      # 0x7030
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, (%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%r15)
movq 0x8(%rax), %rcx
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
pushq $0x1
jmp 0x322e
movl $0x2, (%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%r15)
movq 0x8(%rax), %rcx
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
pushq $0x2
popq %rbp
movl %ebp, %r8d
jmp 0x3275
movq 0x8(%rax), %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x327c
andl $0x0, (%r15)
jmp 0x32a5
movl $0x2, (%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4(%r15)
movq 0x8(%rax), %rcx
xorl %ebp, %ebp
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x30af
jmp 0x32d3
movq (%r12), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x3583
movl $0x1, (%r15)
movq (%r12), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x3eb4
movl %eax, 0x4(%r15)
movq (%r12), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x3f00
movl %eax, %ebp
movq (%r12), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3568
leaq 0x3d51(%rip), %rdx      # 0x7040
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, 0x8(%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0xc(%r15)
movq 0x8(%rax), %rcx
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
pushq $0x1
jmp 0x3340
movl $0x2, 0x8(%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0xc(%r15)
movq 0x8(%rax), %rcx
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
pushq $0x2
popq %r14
movl %r14d, %r8d
jmp 0x338b
movq 0x8(%rax), %rsi
movq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x3392
andl $0x0, 0x8(%r15)
jmp 0x33bc
movl $0x2, 0x8(%r15)
movl 0xc(%rsp), %ecx
movl %ecx, 0xc(%r15)
movq 0x8(%rax), %rcx
xorl %r14d, %r14d
leaq 0xc(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x30af
jmp 0x33eb
movq (%r12), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x35a0
movl $0x1, 0x8(%r15)
movq (%r12), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x3eb4
movl %eax, 0xc(%r15)
movq (%r12), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r14, %rdi
callq 0x3f00
movl %eax, %r14d
cmpl %r14d, %ebp
jne 0x3512
movl %ebp, 0x10(%r15)
movq (%r12), %rax
movl (%rax), %eax
decl %eax
cmpl $0x3, %eax
jae 0x3502
movl %eax, 0x14(%r15)
addq $0x8, %r12
incl %r13d
addq $0x18, %r15
jmp 0x31b6
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %r13d, 0x2580(%rax,%rcx,4)
incq %rcx
movq %rcx, 0x18(%rsp)
movq 0x48(%rsp), %r12
movq 0x8(%r12), %r12
movq 0x40(%rsp), %rax
addq $0x1e0, %rax            # imm = 0x1E0
jmp 0x319d
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movl %ecx, 0x25d0(%rax)
movq 0x10(%rsp), %rdi
callq 0x2150
leaq 0x3dda(%rip), %rdi      # 0x724c
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2340
movq %rbx, %rdi
callq 0x2150
leaq 0x3cad(%rip), %rdi      # 0x713c
leaq 0x461c(%rip), %rsi      # 0x7ab2
callq 0x21a0
movq %rax, %rbx
leaq 0x3ca5(%rip), %rsi      # 0x714a
leaq 0x3da5(%rip), %rdx      # 0x7251
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x3f5e
movq 0x38(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x25da
movq %rbx, %rdi
callq 0x2150
leaq 0x3c64(%rip), %rdi      # 0x713c
callq 0x22e0
leaq 0x3c66(%rip), %rdi      # 0x714a
callq 0x22e0
leaq 0x50(%rsp), %rdi
callq 0x4222
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x7ae7(%rip), %rdi      # 0xaff0
leaq 0x3d0d(%rip), %rsi      # 0x721d
jmp 0x3576
movq 0x7ad7(%rip), %rdi      # 0xaff0
leaq 0x3cbd(%rip), %rsi      # 0x71dd
callq 0x2140
movq (%r12), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x3cbc(%rip), %rsi      # 0x71fc
movq %rax, %rdi
callq 0x2140
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x3c9f(%rip), %rsi      # 0x7202
movq %rax, %rdi
jmp 0x3576
movq 0x7a81(%rip), %rdi      # 0xaff0
leaq 0x3c1f(%rip), %rsi      # 0x7195
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
movq 0x7a36(%rip), %rdi      # 0xafc0
leaq 0x3be3(%rip), %rsi      # 0x7174
callq 0x2140
movq (%r12), %rcx
movq 0x8(%rcx), %rcx
jmp 0x35bb
movq 0x7a19(%rip), %rdi      # 0xafc0
leaq 0x3bc6(%rip), %rsi      # 0x7174
callq 0x2140
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x4597(%rip), %rsi      # 0x7b65
jmp 0x3563
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4222
movq %rbx, %rdi
callq 0x22d0
nop