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

	char *val1, *val2;

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

	for (int i = 0; i < orderUs->numAtts; i++) {
		val1 = left_bits + ((int *) left_bits)[orderUs->whichAtts[i] + 1];
		val2 = right_bits + ((int *) right_bits)[orderUs->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 (orderUs->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 0x60(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x38(%rsp)
movq 0x58(%rsp), %rdi
callq 0x2ad0
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x527d
movq 0x38(%rsp), %rax
movq 0x50(%rsp), %rcx
movslq 0x2c(%rsp), %rdx
movslq 0x4(%rcx,%rdx,4), %rcx
movslq 0x4(%rax,%rcx,4), %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x30(%rsp), %rax
movq 0x50(%rsp), %rcx
movslq 0x2c(%rsp), %rdx
movslq 0x4(%rcx,%rdx,4), %rcx
movslq 0x4(%rax,%rcx,4), %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x50(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movl 0x54(%rax,%rcx,4), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x51a7
jmp 0x5199
movl 0x8(%rsp), %eax
subl $0x1, %eax
je 0x51ef
jmp 0x5245
movq 0x48(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x24(%rsp), %eax
jge 0x51d4
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x5285
movl 0x28(%rsp), %eax
cmpl 0x24(%rsp), %eax
jle 0x51eb
movl $0x1, 0x74(%rsp)
jmp 0x5285
jmp 0x51ed
jmp 0x526b
movq 0x48(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x40(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x5229
movl $0xffffffff, 0x74(%rsp) # imm = 0xFFFFFFFF
jmp 0x5285
movsd 0x18(%rsp), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x5241
movl $0x1, 0x74(%rsp)
jmp 0x5285
jmp 0x5243
jmp 0x526b
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x2200
movl %eax, 0xc(%rsp)
cmpl $0x0, 0xc(%rsp)
je 0x5269
movl 0xc(%rsp), %eax
movl %eax, 0x74(%rsp)
jmp 0x5285
jmp 0x526b
jmp 0x526d
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x512e
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
addq $0x78, %rsp
retq
nop