// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
leaq 0x4879(%rip), %rdi      # 0x813c
leaq 0x5153(%rip), %rbx      # 0x8a1d
movq %rbx, %rsi
callq 0x21d0
movq %rax, (%rsp)
leaq 0x486d(%rip), %rdi      # 0x814a
movq %rbx, %rsi
callq 0x21d0
leaq 0x486d(%rip), %rdi      # 0x8159
movl $0x1a, %esi
movl $0x1, %edx
movq %rax, 0x8(%rsp)
movq %rax, %rcx
callq 0x2380
testq %r13, %r13
je 0x3c3c
movq %r14, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r12, %rax
movq $0x0, 0x10(%rsp)
xorl %r14d, %r14d
movq %r12, 0x28(%rsp)
movq %r13, 0x38(%rsp)
movq (%r13), %r15
testq %r15, %r15
movq %rax, 0x30(%rsp)
je 0x3bf3
xorl %r12d, %r12d
movq %rax, %r13
movq (%r15), %rax
movq 0x8(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3d51
leaq 0x46ee(%rip), %rdx      # 0x8050
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, (%r13)
movl %r14d, 0x4(%r13)
movq 0x8(%rax), %rdx
movq (%rsp), %rdi
leaq 0x4771(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x476d(%rip), %rsi      # 0x8106
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
movl $0x1, %ebp
jmp 0x3a82
movl $0x2, (%r13)
movl %r14d, 0x4(%r13)
movq 0x8(%rax), %rdx
movq (%rsp), %rdi
leaq 0x472c(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x4736(%rip), %rsi      # 0x8114
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
movl $0x2, %ebp
jmp 0x3a82
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x3d61
movl $0x0, (%r13)
movq (%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x42f0
movl %eax, 0x4(%r13)
movq (%r15), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x433e
movl %eax, %ebp
jmp 0x3a82
movl $0x2, (%r13)
movl %r14d, 0x4(%r13)
movq 0x8(%rax), %rdx
xorl %ebp, %ebp
movq (%rsp), %rdi
leaq 0x4695(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x4686(%rip), %rsi      # 0x80fb
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
movq (%r15), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x3d51
leaq 0x45c3(%rip), %rdx      # 0x8060
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl $0x2, 0x8(%r13)
movl %r14d, 0xc(%r13)
movq 0x8(%rax), %rdx
movq (%rsp), %rdi
leaq 0x4636(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x4632(%rip), %rsi      # 0x8106
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
movl $0x1, %ebx
jmp 0x3bbd
movl $0x2, 0x8(%r13)
movl %r14d, 0xc(%r13)
movq 0x8(%rax), %rdx
movq (%rsp), %rdi
leaq 0x45f1(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x45fb(%rip), %rsi      # 0x8114
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
movl $0x2, %ebx
jmp 0x3bbd
movq 0x8(%rax), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x42f0
cmpl $-0x1, %eax
je 0x3d7d
movl $0x0, 0x8(%r13)
movq (%r15), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x42f0
movl %eax, 0xc(%r13)
movq (%r15), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x433e
movl %eax, %ebx
jmp 0x3bbd
movl $0x2, 0x8(%r13)
movl %r14d, 0xc(%r13)
movq 0x8(%rax), %rdx
xorl %ebx, %ebx
movq (%rsp), %rdi
leaq 0x455a(%rip), %rsi      # 0x80f7
xorl %eax, %eax
callq 0x2240
movq 0x8(%rsp), %rdi
leaq 0x454b(%rip), %rsi      # 0x80fb
movl %r14d, %edx
xorl %eax, %eax
callq 0x2240
incl %r14d
cmpl %ebx, %ebp
jne 0x3d00
movl %ebp, 0x10(%r13)
movq (%r15), %rax
movl (%rax), %eax
decl %eax
cmpl $0x3, %eax
jae 0x3ced
movl %eax, 0x14(%r13)
movq 0x8(%r15), %r15
addq $0x18, %r13
incl %r12d
testq %r15, %r15
jne 0x3947
jmp 0x3bf6
xorl %r12d, %r12d
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movl %r12d, 0x2580(%rax,%rcx,4)
movq %rax, %r12
incq %rcx
movq %rcx, 0x10(%rsp)
movq 0x38(%rsp), %r13
movq 0x8(%r13), %r13
movq 0x30(%rsp), %rax
addq $0x1e0, %rax            # imm = 0x1E0
testq %r13, %r13
jne 0x392a
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rax
jmp 0x3c3e
xorl %eax, %eax
movl %eax, 0x25d0(%r12)
movq (%rsp), %rdi
callq 0x2180
leaq 0x45f6(%rip), %rdi      # 0x824c
movl $0x4, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2380
movq %rbx, %rdi
callq 0x2180
leaq 0x44c0(%rip), %rdi      # 0x813c
leaq 0x4e1f(%rip), %rsi      # 0x8aa2
callq 0x21d0
movq %rax, %rbx
leaq 0x44b8(%rip), %rsi      # 0x814a
leaq 0x45b8(%rip), %rdx      # 0x8251
leaq 0x40(%rsp), %r14
movq %r14, %rdi
callq 0x4394
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x261e
movq %rbx, %rdi
callq 0x2180
leaq 0x4479(%rip), %rdi      # 0x813c
callq 0x2320
leaq 0x447b(%rip), %rdi      # 0x814a
callq 0x2320
leaq 0x40(%rsp), %rdi
callq 0x46c8
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x82fc(%rip), %rdi      # 0xbff0
leaq 0x4522(%rip), %rsi      # 0x821d
jmp 0x3dad
movq 0x82e9(%rip), %rdi      # 0xbff0
leaq 0x44cf(%rip), %rsi      # 0x81dd
callq 0x2170
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x44cf(%rip), %rsi      # 0x81fc
movq %rax, %rdi
callq 0x2170
movq (%r15), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x44b3(%rip), %rsi      # 0x8202
jmp 0x3daa
movq 0x8298(%rip), %rdi      # 0xbff0
leaq 0x4436(%rip), %rsi      # 0x8195
jmp 0x3dad
movq 0x8258(%rip), %rdi      # 0xbfc0
leaq 0x4405(%rip), %rsi      # 0x8174
callq 0x2170
movq (%r15), %rcx
movq 0x8(%rcx), %rcx
jmp 0x3d97
movq 0x823c(%rip), %rdi      # 0xbfc0
leaq 0x43e9(%rip), %rsi      # 0x8174
callq 0x2170
movq (%r15), %rcx
movq 0x10(%rcx), %rcx
movq 0x8(%rcx), %rsi
movq %rax, %rdi
callq 0x2170
leaq 0x4dab(%rip), %rsi      # 0x8b55
movq %rax, %rdi
callq 0x2170
movl $0x1, %edi
callq 0x21f0
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x46c8
movq %rbx, %rdi
callq 0x2310
nopl (%rax)