// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %r9, %r15
movl %r8d, 0xc(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movl 0x78(%rsp), %ebp
movl 0x70(%rsp), %ebx
movq %rdi, 0x10(%rsp)
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x2a95
callq 0x2210
movq 0x10(%rsp), %rax
movq $0x0, (%rax)
cmpl $0x0, 0x18(%rsp)
je 0x2be1
cmpl $0x0, 0xc(%rsp)
je 0x2bde
movq %rbx, %rax
leal 0x4(,%rbx,4), %ebx
movl %ebp, %ebp
movl %eax, %r10d
movl %ebx, %r13d
movq %rax, 0x20(%rsp)
testl %eax, %eax
jle 0x2b18
movq (%r14), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movq 0x18(%rsp), %rsi
movl %ebx, %r13d
cmpq %rdx, %rbp
jne 0x2aee
movq (%r12), %rax
movl 0xc(%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, %r13d
incq %rdx
cmpq %rdx, %r10
jne 0x2ae1
movq %r10, 0x30(%rsp)
movq %r12, 0x28(%rsp)
leal 0x1(%r13), %eax
movslq %eax, %rdi
movq 0x9470(%rip), %rsi      # 0xbfa0
callq 0x22d0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x2bfc
movl %r13d, (%rax)
cmpl $0x0, 0x20(%rsp)
jle 0x2bcf
movq (%r14), %r12
xorl %eax, %eax
movq %rbp, %r14
cmpq %rax, %rbp
jne 0x2b6e
movq 0x28(%rsp), %rcx
movq (%rcx), %r12
movl 0xc(%rsp), %ecx
movq %rcx, 0x18(%rsp)
movslq (%r15,%rax,4), %rcx
movq 0x18(%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 0x10(%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, 0x30(%rsp)
movq %r14, %rbp
jne 0x2b55
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r12
movq 0x10(%rsp), %rdi
movq %r12, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28ce
movq 0x93bd(%rip), %rdi      # 0xbfc0
leaq 0x53fa(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0
nop