// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, %r15
movl %r8d, 0x4(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movl 0x68(%rsp), %r14d
movl 0x60(%rsp), %r10d
movq %rdi, 0x8(%rsp)
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x2a37
movq %r10, %r12
callq 0x21e0
movq %r12, %r10
movq 0x8(%rsp), %rax
andq $0x0, (%rax)
cmpl $0x0, 0x10(%rsp)
je 0x2b73
cmpl $0x0, 0x4(%rsp)
je 0x2b70
leal 0x4(,%r10,4), %ebp
movq (%r13), %rax
xorl %ecx, %ecx
testl %r10d, %r10d
cmovlel %ecx, %r10d
movl %r14d, %r14d
xorl %edx, %edx
movl %ebp, %r12d
movq 0x10(%rsp), %rsi
cmpq %rdx, %r10
je 0x2ab0
cmpq %rdx, %r14
jne 0x2a89
movq (%rbx), %rax
movl 0x4(%rsp), %esi
movslq (%r15,%rdx,4), %rdi
leal -0x1(%rsi), %r8d
leal 0x2(%rdi), %r9d
cmpl %r8d, %edi
cmovel %ecx, %r9d
movslq %r9d, %r8
movl (%rax,%r8,4), %r8d
subl 0x4(%rax,%rdi,4), %r8d
addl %r8d, %r12d
incq %rdx
jmp 0x2a78
movq %r10, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
leal 0x1(%r12), %eax
movslq %eax, %rdi
movq 0x84d7(%rip), %rsi      # 0xafa0
callq 0x22a0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x2b8e
movl %r12d, (%rax)
movq (%r13), %r12
xorl %eax, %eax
movq 0x18(%rsp), %rcx
cmpq %rax, %rcx
je 0x2b61
cmpq %rax, %r14
jne 0x2b08
movq 0x20(%rsp), %rcx
movq (%rcx), %r12
movl 0x4(%rsp), %ecx
movq %rcx, 0x10(%rsp)
movslq (%r15,%rax,4), %rcx
movq 0x10(%rsp), %rdx
decl %edx
leal 0x2(%rcx), %esi
cmpl %edx, %ecx
movl $0x0, %edx
cmovel %edx, %esi
movslq %esi, %rdx
movslq (%r12,%rdx,4), %r13
movslq 0x4(%r12,%rcx,4), %rcx
subq %rcx, %r13
movq 0x8(%rsp), %rcx
movq (%rcx), %rdi
leaq 0x1(%rax), %rbx
movl %ebp, 0x4(%rdi,%rax,4)
movslq %ebp, %rbp
addq %rbp, %rdi
movslq (%r15,%rax,4), %rax
movslq 0x4(%r12,%rax,4), %rsi
addq %r12, %rsi
movq %r13, %rdx
callq 0x2280
addl %r13d, %ebp
movq %rbx, %rax
jmp 0x2ae8
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x288c
movq 0x842b(%rip), %rdi      # 0xafc0
leaq 0x4468(%rip), %rsi      # 0x7004
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
nop