// File: /workspace/github_cpp_makefile_star2_O2/PrarabdhJoshi[P]DatabaseImplementation/Comparison.cc
// Function: CNF::GrowFromParseTree(AndList*, Schema*, Record&)
void CNF :: GrowFromParseTree (struct AndList *parseTree, Schema *mySchema, 
	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 schema
				if (mySchema->Find (myOr->left->left->value) != -1) {
					cnf.orList[whichAnd][whichOr].operand1 = Left;
					cnf.orList[whichAnd][whichOr].whichAtt1 =
						mySchema->Find (myOr->left->left->value);	
					typeLeft = mySchema->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 (mySchema->Find (myOr->left->right->value) != -1) {
					cnf.orList[whichAnd][whichOr].operand2 = Left;
					cnf.orList[whichAnd][whichOr].whichAtt2 =
						mySchema->Find (myOr->left->right->value);	
					typeRight = mySchema->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 outSchema("hkljdfgkSDFSDF", "tempSchema");

	// and get the record
	literal.SuckNextRecord (&outSchema, 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 $0x58, %rsp
movq %rcx, 0x18(%rsp)
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
leaq 0x3b33(%rip), %rdi      # 0x713c
leaq 0x441d(%rip), %rbx      # 0x7a2d
movq %rbx, %rsi
callq 0x21a0
movq %rax, 0x8(%rsp)
leaq 0x3b26(%rip), %rdi      # 0x714a
movq %rbx, %rsi
movq %r15, %rbx
callq 0x21a0
leaq 0x3b23(%rip), %rdi      # 0x7159
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rbp
movq %rax, %rcx
callq 0x2340
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
xorl %ecx, %ecx
movq %r14, 0x10(%rsp)
movq %r13, 0x38(%rsp)
testq %rbx, %rbx
je 0x38b3
movq %rcx, 0x28(%rsp)
xorl %r12d, %r12d
movq %r14, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3884
movq (%rbx), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x39c3
leaq 0x39b3(%rip), %rdx      # 0x7050
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, (%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%rax), %rcx
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
pushq $0x1
jmp 0x36ec
movl $0x2, (%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%rax), %rcx
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
pushq $0x2
popq %r15
movl %r15d, %r8d
jmp 0x3761
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x39d3
andl $0x0, (%r14)
movq (%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3eb4
movl %eax, 0x4(%r14)
movq (%rbx), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3f00
movl %eax, %r15d
jmp 0x3766
movl $0x2, (%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0x4(%r14)
movq 0x8(%rax), %rcx
xorl %r15d, %r15d
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x30af
movq (%rbx), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x39c3
leaq 0x38df(%rip), %rdx      # 0x7060
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, 0x8(%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0xc(%r14)
movq 0x8(%rax), %rcx
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
pushq $0x1
jmp 0x37d2
movl $0x2, 0x8(%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0xc(%r14)
movq 0x8(%rax), %rcx
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
pushq $0x2
popq %r13
movl %r13d, %r8d
jmp 0x3849
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3eb4
cmpl $-0x1, %eax
je 0x39ef
andl $0x0, 0x8(%r14)
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3eb4
movl %eax, 0xc(%r14)
movq (%rbx), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %r13, %rdi
callq 0x3f00
movl %eax, %r13d
jmp 0x384e
movl $0x2, 0x8(%r14)
movl 0x4(%rsp), %ecx
movl %ecx, 0xc(%r14)
movq 0x8(%rax), %rcx
xorl %r13d, %r13d
leaq 0x4(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
xorl %r8d, %r8d
callq 0x30af
cmpl %r13d, %r15d
jne 0x3972
movl %r15d, 0x10(%r14)
movq (%rbx), %rax
movl (%rax), %eax
decl %eax
cmpl $0x3, %eax
jae 0x395f
movl %eax, 0x14(%r14)
addq $0x8, %rbx
incl %r12d
addq $0x18, %r14
movq 0x38(%rsp), %r13
jmp 0x3676
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
movl %r12d, 0x2580(%rax,%rcx,4)
incq %rcx
movq 0x30(%rsp), %rbx
movq 0x8(%rbx), %rbx
movq 0x20(%rsp), %r14
addq $0x1e0, %r14            # imm = 0x1E0
jmp 0x365b
movq 0x10(%rsp), %rax
movl %ecx, 0x25d0(%rax)
movq 0x8(%rsp), %rdi
callq 0x2150
leaq 0x397d(%rip), %rdi      # 0x724c
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbp, %rcx
callq 0x2340
movq %rbp, %rdi
callq 0x2150
leaq 0x3850(%rip), %rdi      # 0x713c
leaq 0x41bf(%rip), %rsi      # 0x7ab2
callq 0x21a0
movq %rax, %rbx
leaq 0x3848(%rip), %rsi      # 0x714a
leaq 0x3948(%rip), %rdx      # 0x7251
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x3f5e
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x25da
movq %rbx, %rdi
callq 0x2150
leaq 0x3807(%rip), %rdi      # 0x713c
callq 0x22e0
leaq 0x3809(%rip), %rdi      # 0x714a
callq 0x22e0
leaq 0x40(%rsp), %rdi
callq 0x4222
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x768a(%rip), %rdi      # 0xaff0
leaq 0x38b0(%rip), %rsi      # 0x721d
jmp 0x3a1f
movq 0x7677(%rip), %rdi      # 0xaff0
leaq 0x385d(%rip), %rsi      # 0x71dd
callq 0x2140
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x385d(%rip), %rsi      # 0x71fc
movq %rax, %rdi
callq 0x2140
movq (%rbx), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x3841(%rip), %rsi      # 0x7202
jmp 0x3a1c
movq 0x7626(%rip), %rdi      # 0xaff0
leaq 0x37c4(%rip), %rsi      # 0x7195
jmp 0x3a1f
movq 0x75e6(%rip), %rdi      # 0xafc0
leaq 0x3793(%rip), %rsi      # 0x7174
callq 0x2140
movq (%rbx), %rcx
movq 0x8(%rcx), %rcx
jmp 0x3a09
movq 0x75ca(%rip), %rdi      # 0xafc0
leaq 0x3777(%rip), %rsi      # 0x7174
callq 0x2140
movq (%rbx), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0x4149(%rip), %rsi      # 0x7b65
movq %rax, %rdi
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4222
movq %rbx, %rdi
callq 0x22d0
nopl (%rax)