// File: PrarabdhJoshi[P]DatabaseImplementation/ComparisonEngine.cc
// Function: ComparisonEngine::Compare(Record*, OrderMaker*, Record*, OrderMaker*)
int ComparisonEngine :: Compare (Record *left, OrderMaker *order_left, Record *right, OrderMaker *order_right) {

	char *val1, *val2;

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

	for (int i = 0; i < order_left->numAtts; i++) {
		val1 = left_bits + ((int *) left_bits)[order_left->whichAtts[i] + 1];
		val2 = right_bits + ((int *) right_bits)[order_right->whichAtts[i] + 1];
	
		// these are used to store the two operands, depending on their type
		int val1Int, val2Int;
		double val1Double, val2Double;
		
		// now check the type and do the comparison
		switch (order_left->whichTypes[i]) {
	
			// first case: we are dealing with integers
			case Int:
	
			// cast the two bit strings to ints
			val1Int = *((int *) val1);
			val2Int = *((int *) val2);
	
			// and do the comparison
			if (val1Int < val2Int) 
				return -1;
			else if (val1Int > val2Int)
				return 1;
	
			break;
	
	
			// second case: dealing with doubles
			case Double:
	
			// cast the two bit strings to doubles
			val1Double = *((double *) val1);
			val2Double = *((double *) val2);
	
			// and do the comparison
			if (val1Double < val2Double)
				return -1;
			else if (val1Double > val2Double)
				return 1;
	
			break;
	
	
			// last case: dealing with strings
			default:
			int sc = strcmp (val1, val2);
			if (sc != 0)
				return sc;

			break;
	
		}
	}

	return 0;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdx, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq 0x60(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x30(%rsp)
movq 0x50(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x5420
movq 0x30(%rsp), %rax
movq 0x58(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movslq 0x4(%rcx,%rdx,4), %rcx
movslq 0x4(%rax,%rcx,4), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq 0x48(%rsp), %rcx
movslq 0x24(%rsp), %rdx
movslq 0x4(%rcx,%rdx,4), %rcx
movslq 0x4(%rax,%rcx,4), %rcx
addq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0x58(%rsp), %rax
movslq 0x24(%rsp), %rcx
movl 0x54(%rax,%rcx,4), %eax
movl %eax, (%rsp)
testl %eax, %eax
je 0x534a
jmp 0x533d
movl (%rsp), %eax
subl $0x1, %eax
je 0x5392
jmp 0x53e8
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x20(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x20(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0x5377
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x5428
movl 0x20(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jle 0x538e
movl $0x1, 0x74(%rsp)
jmp 0x5428
jmp 0x5390
jmp 0x540e
movq 0x40(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x38(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x53cc
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x5428
movsd 0x10(%rsp), %xmm0
ucomisd 0x8(%rsp), %xmm0
jbe 0x53e4
movl $0x1, 0x74(%rsp)
jmp 0x5428
jmp 0x53e6
jmp 0x540e
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x2200
movl %eax, 0x4(%rsp)
cmpl $0x0, 0x4(%rsp)
je 0x540c
movl 0x4(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x5428
jmp 0x540e
jmp 0x5410
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x52d3
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)