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

	char *val1, *val2;

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

	// first get a pointer to the first value to compare
	if (c->operand1 == Left) {
		val1 = left_bits + ((int *) left_bits)[c->whichAtt1 + 1];
	} else {
		val1 = lit_bits + ((int *) lit_bits)[c->whichAtt1 + 1];
	}

	// next get a pointer to the second value to compare
	if (c->operand2 == Left) {
		val2 = left_bits + ((int *) left_bits)[c->whichAtt2 + 1];
	} else {
		val2 = lit_bits + ((int *) lit_bits)[c->whichAtt2 + 1];
	}


	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 $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq 0x60(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x38(%rsp)
movq 0x58(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x30(%rsp)
movq 0x50(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x55a6
movq 0x38(%rsp), %rax
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl 0x4(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
jmp 0x55ca
movq 0x30(%rsp), %rax
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl 0x4(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x55fb
movq 0x38(%rsp), %rax
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl 0xc(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
jmp 0x561f
movq 0x30(%rsp), %rax
movq 0x30(%rsp), %rcx
movq 0x50(%rsp), %rdx
movl 0xc(%rdx), %edx
addl $0x1, %edx
movslq %edx, %rdx
movslq (%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x50(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x5643
jmp 0x5631
movl 0xc(%rsp), %eax
subl $0x1, %eax
je 0x56c1
jmp 0x5759
movq 0x48(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x2c(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x50(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x5676
jmp 0x566b
movl 0x8(%rsp), %eax
subl $0x1, %eax
je 0x568f
jmp 0x56a8
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movl 0x2c(%rsp), %eax
cmpl 0x28(%rsp), %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x40(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x50(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x56fc
jmp 0x56f1
movl 0x4(%rsp), %eax
subl $0x1, %eax
je 0x571d
jmp 0x573a
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
sete %al
setnp %cl
andb %cl, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x2200
movl %eax, 0x24(%rsp)
movq 0x50(%rsp), %rax
movl 0x14(%rax), %eax
movl %eax, (%rsp)
testl %eax, %eax
je 0x5787
jmp 0x577d
movl (%rsp), %eax
subl $0x1, %eax
je 0x579a
jmp 0x57ad
cmpl $0x0, 0x24(%rsp)
setl %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
cmpl $0x0, 0x24(%rsp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
jmp 0x57be
cmpl $0x0, 0x24(%rsp)
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw (%rax,%rax)