// File: /workspace/github_cpp_makefile_star2_O2/PrarabdhJoshi[P]DatabaseImplementation/Record.cc
// Function: Record::Project(int*, int, int)
void Record :: Project (int *attsToKeep, int numAttsToKeep, int numAttsNow) {
	// first, figure out the size of the new record
	int totSpace = sizeof (int) * (numAttsToKeep + 1);

	for (int i = 0; i < numAttsToKeep; i++) {
		// 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 *) bits)[0] - ((int *) bits)[attsToKeep[i] + 1];
		} else {
			// in this case, subtract the start of the next field from the start of this field
			totSpace += ((int *) bits)[attsToKeep[i] + 2] - ((int *) bits)[attsToKeep[i] + 1]; 
		}
	}

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

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

	// and copy all of the fields over
	int curPos = sizeof (int) * (numAttsToKeep + 1);
	for (int i = 0; i < numAttsToKeep; i++) {
		// 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 *) bits)[0] - ((int *) bits)[attsToKeep[i] + 1];

		} else {
			// in this case, subtract the start of the next field from the start of this field
			attLen = ((int *) bits)[attsToKeep[i] + 2] - ((int *) bits)[attsToKeep[i] + 1]; 
		}

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

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

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

	// kill the old bits
	delete [] bits;

	// and attach the new ones
	bits = newBits;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
leal 0x4(,%rdx,4), %ebx
decl %ecx
movq %rdi, 0x8(%rsp)
movq (%rdi), %r15
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
movslq %ecx, %r12
movl %ebx, %ebp
cmpq %rax, %rdx
je 0x2940
movslq (%rsi,%rax,4), %rcx
cmpl %r12d, %ecx
leaq 0x8(%r15,%rcx,4), %rdi
cmoveq %r12, %rcx
cmoveq %r15, %rdi
movl (%rdi), %edi
subl 0x4(%r15,%rcx,4), %edi
addl %edi, %ebp
incq %rax
jmp 0x2919
movq %rsi, 0x10(%rsp)
movq %rdx, (%rsp)
movslq %ebp, %rdi
movq 0x864d(%rip), %rsi      # 0xafa0
callq 0x22a0
testq %rax, %rax
je 0x29d8
movq %rax, %r13
movl %ebp, (%rax)
xorl %r14d, %r14d
movq (%rsp), %rax
cmpq %r14, %rax
je 0x29b4
movq 0x10(%rsp), %rax
movslq (%rax,%r14,4), %rax
cmpl %r12d, %eax
leaq 0x8(%r15,%rax,4), %rcx
cmoveq %r12, %rax
cmoveq %r15, %rcx
movslq (%rcx), %rbp
movslq 0x4(%r15,%rax,4), %rsi
subq %rsi, %rbp
movl %ebx, 0x4(%r13,%r14,4)
incq %r14
movslq %ebx, %rbx
leaq (%rbx,%r13), %rdi
addq %r15, %rsi
movq %rbp, %rdx
callq 0x2110
movq (%rsp), %rax
addl %ebp, %ebx
jmp 0x2969
testq %r15, %r15
je 0x29c1
movq %r15, %rdi
callq 0x21e0
movq 0x8(%rsp), %rax
movq %r13, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x85e1(%rip), %rdi      # 0xafc0
leaq 0x461e(%rip), %rsi      # 0x7004
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
nop