// File: PrarabdhJoshi[P]DatabaseImplementation/ComparisonEngine.cc
// Function: ComparisonEngine::Run(Record*, Record*, Record*, Comparison*)
int ComparisonEngine :: Run (Record *left, Record *right, Record *literal, Comparison *c) {

	char *val1, *val2;

	char *left_bits = left->GetBits();
	char *right_bits = right->GetBits();
	char *lit_bits = literal->GetBits();

	// first get a pointer to the first value to compare
	switch (c->operand1) {

		case Left:
		val1 = left_bits + ((int *) left_bits)[c->whichAtt1 + 1];
		break;
	
		case Right:
		val1 = right_bits + ((int *) right_bits)[c->whichAtt1 + 1];
		break;

		default:
		val1 = lit_bits + ((int *) lit_bits)[c->whichAtt1 + 1];
		break;	
	}

	// next get a pointer to the second value to compare
	switch (c->operand2) {

		case Left:
		val2 = left_bits + ((int *) left_bits)[c->whichAtt2 + 1];
		break;

		case Right:
		val2 = right_bits + ((int *) right_bits)[c->whichAtt2 + 1];
		break;

		default:
		val2 = lit_bits + ((int *) lit_bits)[c->whichAtt2 + 1];
		break;	

	}
			
	int val1Int, val2Int, tempResult;
	double val1Double, val2Double;

	// now check the type and the comparison operation
	switch (c->attType) {

		// first case: we are dealing with integers
		case Int:

		val1Int = *((int *) val1);
		val2Int = *((int *) val2);

		// and check the operation type in order to actually do the comparison
		switch (c->op) {
				
			case LessThan:
			return (val1Int < val2Int);
			break;

			case GreaterThan:
			return (val1Int > val2Int);
			break;
					
			default:
			return (val1Int == val2Int);
			break;	
		}	
		break;

		// second case: dealing with doubles
		case Double:
		val1Double = *((double *) val1);
		val2Double = *((double *) val2);

		// and check the operation type in order to actually do the comparison
		switch (c->op) {
						
			case LessThan:
			return (val1Double < val2Double);
			break;

			case GreaterThan:
			return (val1Double > val2Double);
			break;
					
			default:
			return (val1Double == val2Double);
			break;	
		}	
		break;

		// final case: dealing with strings
		default:

		// so check the operation type in order to actually do the comparison
		tempResult = strcmp (val1, val2);
		switch (c->op) {
						
			case LessThan:
			return tempResult < 0;
			break;

			case GreaterThan:
			return tempResult > 0;
			break;
					
			default:
			return tempResult == 0;
			break;	
		}	
		break;
	}

}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x88(%rsp)
movq %rsi, 0x80(%rsp)
movq %rdx, 0x78(%rsp)
movq %rcx, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq 0x80(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x50(%rsp)
movq 0x78(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x48(%rsp)
movq 0x70(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x40(%rsp)
movq 0x68(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0x5952
jmp 0x5947
movl 0x1c(%rsp), %eax
subl $0x1, %eax
je 0x5978
jmp 0x599e
movq 0x50(%rsp), %rax
movq 0x50(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0x4(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
jmp 0x59c2
movq 0x48(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0x4(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
jmp 0x59c2
movq 0x40(%rsp), %rax
movq 0x40(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0x4(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x18(%rsp)
testl %eax, %eax
je 0x59df
jmp 0x59d4
movl 0x18(%rsp), %eax
subl $0x1, %eax
je 0x5a05
jmp 0x5a2b
movq 0x50(%rsp), %rax
movq 0x50(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0xc(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
jmp 0x5a4f
movq 0x48(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0xc(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
jmp 0x5a4f
movq 0x40(%rsp), %rax
movq 0x40(%rsp), %rcx
movq 0x68(%rsp), %rdx
movl 0xc(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
movq 0x68(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x5a73
jmp 0x5a61
movl 0x14(%rsp), %eax
subl $0x1, %eax
je 0x5afa
jmp 0x5b9b
movq 0x60(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0x58(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x68(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x10(%rsp)
testl %eax, %eax
je 0x5aa6
jmp 0x5a9b
movl 0x10(%rsp), %eax
subl $0x1, %eax
je 0x5ac2
jmp 0x5ade
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movl 0x3c(%rsp), %eax
cmpl 0x38(%rsp), %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movq 0x60(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x58(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x68(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x5b35
jmp 0x5b2a
movl 0xc(%rsp), %eax
subl $0x1, %eax
je 0x5b59
jmp 0x5b79
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movsd 0x28(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movsd 0x28(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
sete %al
setnp %cl
andb %cl, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x2200
movl %eax, 0x34(%rsp)
movq 0x68(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x5bcb
jmp 0x5bc0
movl 0x8(%rsp), %eax
subl $0x1, %eax
je 0x5be1
jmp 0x5bf7
cmpl $0x0, 0x34(%rsp)
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
cmpl $0x0, 0x34(%rsp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
jmp 0x5c0b
cmpl $0x0, 0x34(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopw (%rax,%rax)