// File: /workspace/github_cpp_makefile_star2_O3/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
movl %edx, %r15d
movq %rsi, %r13
movq %rdi, %r14
leal 0x4(,%r15,4), %ebx
movl %edx, %r8d
movl %ebx, %ebp
testl %edx, %edx
jle 0x298c
leal -0x1(%rcx), %edx
movq (%r14), %rax
movslq %edx, %r9
xorl %edx, %edx
movl %ebx, %ebp
movslq (%r13,%rdx,4), %rsi
cmpl %r9d, %esi
leaq 0x8(%rax,%rsi,4), %rdi
cmoveq %r9, %rsi
cmoveq %rax, %rdi
movl (%rdi), %edi
subl 0x4(%rax,%rsi,4), %edi
addl %edi, %ebp
incq %rdx
cmpq %rdx, %r8
jne 0x2967
movq %rcx, (%rsp)
movq %r8, 0x10(%rsp)
movslq %ebp, %rdi
movq 0x9601(%rip), %rsi      # 0xbfa0
callq 0x22d0
testq %rax, %rax
je 0x2a3c
movq %rax, %r12
movl %ebp, (%rax)
movq %r14, 0x8(%rsp)
movq (%r14), %rbp
testl %r15d, %r15d
jle 0x2a18
movq (%rsp), %rax
decl %eax
movslq %eax, %r14
movq %r13, (%rsp)
xorl %r13d, %r13d
movq (%rsp), %rax
movslq (%rax,%r13,4), %rax
cmpl %r14d, %eax
leaq 0x8(%rbp,%rax,4), %rcx
cmoveq %r14, %rax
cmoveq %rbp, %rcx
movslq (%rcx), %r15
movslq 0x4(%rbp,%rax,4), %rsi
subq %rsi, %r15
movl %ebx, 0x4(%r12,%r13,4)
incq %r13
movslq %ebx, %rbx
leaq (%r12,%rbx), %rdi
addq %rbp, %rsi
movq %r15, %rdx
callq 0x2130
addl %r15d, %ebx
cmpq %r13, 0x10(%rsp)
jne 0x29cf
jmp 0x2a1d
testq %rbp, %rbp
je 0x2a25
movq %rbp, %rdi
callq 0x2210
movq 0x8(%rsp), %rax
movq %r12, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x957d(%rip), %rdi      # 0xbfc0
leaq 0x55ba(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0
nop