// File: PrarabdhJoshi[P]DatabaseImplementation/Record.cc
// Function: Record::MergeRecords(Record*, Record*, int, int, int*, int, int)
void Record :: MergeRecords (Record *left, Record *right, int numAttsLeft, int numAttsRight, int *attsToKeep, int numAttsToKeep, int startOfRight) {
	delete [] bits;
	bits = NULL;

	// if one of the records is empty, new record is non-empty record
	if(numAttsLeft == 0 ) {
		Copy(right);
		return;

	} else if(numAttsRight == 0 ) {
		Copy(left);
		return;
	}

	// first, figure out the size of the new record
	int totSpace = sizeof (int) * (numAttsToKeep + 1);

	int numAttsNow = numAttsLeft;
	char *rec_bits = left->bits;

	for (int i = 0; i < numAttsToKeep; i++) {
		if (i == startOfRight) {
			numAttsNow = numAttsRight;
			rec_bits = right->bits;
		}
		// if we are keeping the last record, be careful!
		if (attsToKeep[i] == numAttsNow - 1) {
			// in this case, take the length of the record and subtract the start pos
			totSpace += ((int *) rec_bits)[0] - ((int *) rec_bits)[attsToKeep[i] + 1];
		} else {
			// in this case, subtract the start of the next field from the start of this field
			totSpace += ((int *) rec_bits)[attsToKeep[i] + 2] - ((int *) rec_bits)[attsToKeep[i] + 1]; 
		}
	}

	// now, allocate the new bits
	bits = new (std::nothrow) char[totSpace+1];
	if (bits == NULL)
	{
		cout << "ERROR : Not enough memory. EXIT !!!\n";
		exit(1);
	}

	// record the total length of the record
	*((int *) bits) = totSpace;

	numAttsNow = numAttsLeft;
	rec_bits = left->bits;

	// and copy all of the fields over
	int curPos = sizeof (int) * (numAttsToKeep + 1);
	for (int i = 0; i < numAttsToKeep; i++) {
		if (i == startOfRight) {
			numAttsNow = numAttsRight;
			rec_bits = right->bits;
		}
		
		// this is the length (in bytes) of the current attribute
		int attLen;

		// if we are keeping the last record, be careful!
		if (attsToKeep[i] == numAttsNow - 1) {
			// in this case, take the length of the record and subtract the start pos
			attLen = ((int *) rec_bits)[0] - ((int *) rec_bits)[attsToKeep[i] + 1];
		} else {
			// in this case, subtract the start of the next field from the start of this field
			attLen = ((int *) rec_bits)[attsToKeep[i] + 2] - ((int *) rec_bits)[attsToKeep[i] + 1]; 
		}

		// set the start position of this field
		((int *) bits)[i + 1] = curPos;	

		// and copy over the bits
		memmove (&(bits[curPos]), &(rec_bits[((int *) rec_bits)[attsToKeep[i] + 1]]), attLen);

		// note that we are moving along in the record
		curPos += attLen;

	}
}

// Assembly:
subq $0x58, %rsp
movl 0x68(%rsp), %eax
movl 0x60(%rsp), %eax
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movl %ecx, 0x3c(%rsp)
movl %r8d, 0x38(%rsp)
movq %r9, 0x30(%rsp)
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq (%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2f2a
movq 0x8(%rsp), %rdi
callq 0x21e0
movq (%rsp), %rax
movq $0x0, (%rax)
cmpl $0x0, 0x3c(%rsp)
jne 0x2f4f
movq (%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x2bd0
jmp 0x31ec
cmpl $0x0, 0x38(%rsp)
jne 0x2f69
movq (%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x2bd0
jmp 0x31ec
jmp 0x2f6b
movl 0x60(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, 0x2c(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0x305a
movl 0x1c(%rsp), %eax
cmpl 0x68(%rsp), %eax
jne 0x2fc6
movl 0x38(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x28(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x300a
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq 0x1c(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x3048
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x1c(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq 0x1c(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x304a
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x2f99
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rdi
movq 0xcf25(%rip), %rsi      # 0xff90
callq 0x2280
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x309d
movq 0xcf31(%rip), %rdi      # 0xffb8
leaq 0x8f76(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movq (%rsp), %rax
movl 0x2c(%rsp), %ecx
movq (%rax), %rax
movl %ecx, (%rax)
movl 0x3c(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x48(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movl 0x60(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x60(%rsp), %eax
jge 0x31ec
movl 0x14(%rsp), %eax
cmpl 0x68(%rsp), %eax
jne 0x3105
movl 0x38(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movslq 0x14(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x28(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3145
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq 0x14(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, 0x10(%rsp)
jmp 0x317f
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x14(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdx
movslq 0x14(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, 0x10(%rsp)
movq (%rsp), %rax
movl 0x18(%rsp), %esi
movq (%rax), %rcx
movl 0x14(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movl %esi, (%rcx,%rdx,4)
movq (%rax), %rdi
movslq 0x18(%rsp), %rax
addq %rax, %rdi
movq 0x20(%rsp), %rsi
movq 0x20(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x14(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movslq (%rax,%rcx,4), %rax
addq %rax, %rsi
movslq 0x10(%rsp), %rdx
callq 0x2260
movl 0x10(%rsp), %eax
addl 0x18(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x30d8
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)