// File: /workspace/github_cpp_makefile_star2_O1/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 %rsi, %r14
movl 0x68(%rsp), %ebp
movl 0x60(%rsp), %r13d
movq %rdi, 0x8(%rsp)
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x2af9
movq %rdx, %rbx
callq 0x2210
movq %rbx, %rdx
movq 0x8(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x0, 0x10(%rsp)
je 0x2c3e
cmpl $0x0, 0x4(%rsp)
je 0x2c3b
leal 0x4(,%r13,4), %ebx
movl %ebp, %ebp
movl %r13d, %r11d
movl %ebx, %r12d
testl %r13d, %r13d
jle 0x2b76
movq (%r14), %rax
xorl %ecx, %ecx
xorl %r10d, %r10d
movq 0x10(%rsp), %rsi
movl %ebx, %r12d
cmpq %r10, %rbp
jne 0x2b4c
movq (%rdx), %rax
movl 0x4(%rsp), %esi
movslq (%r15,%r10,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 %r10
cmpq %r10, %r11
jne 0x2b40
movq %r11, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
leal 0x1(%r12), %eax
movslq %eax, %rdi
movq 0x9411(%rip), %rsi      # 0xbfa0
callq 0x22d0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x2c59
movl %r12d, (%rax)
testl %r13d, %r13d
jle 0x2c2c
movq (%r14), %r12
xorl %eax, %eax
movq %rbp, %r14
cmpq %rax, %rbp
jne 0x2bcb
movq 0x18(%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), %rbp
movl %ebx, 0x4(%rdi,%rax,4)
movslq %ebx, %rbx
addq %rbx, %rdi
movslq (%r15,%rax,4), %rax
movslq 0x4(%r12,%rax,4), %rsi
addq %r12, %rsi
movq %r13, %rdx
callq 0x22b0
addl %r13d, %ebx
movq %rbp, %rax
cmpq %rbp, 0x20(%rsp)
movq %r14, %rbp
jne 0x2bb2
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdx
movq 0x8(%rsp), %rdi
movq %rdx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2918
movq 0x9360(%rip), %rdi      # 0xbfc0
leaq 0x539d(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0