// File: /workspace/github_cpp_makefile_star2_O1/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, %r13d
movq %rsi, %rbx
leal 0x4(,%r13,4), %ebp
movl %edx, %r12d
movl %ebp, %r14d
testl %edx, %edx
jle 0x29d1
leal -0x1(%rcx), %eax
movq (%rdi), %r9
xorl %edx, %edx
movl %ebp, %r14d
movslq (%rbx,%rdx,4), %rsi
cmpl %eax, %esi
leaq 0x8(%r9,%rsi,4), %rsi
movq %rsi, %r8
cmoveq %r9, %r8
movl (%r8), %r8d
subl -0x4(%rsi), %r8d
addl %r8d, %r14d
incq %rdx
cmpq %rdx, %r12
jne 0x29ad
movq %rcx, %r15
movq %rdi, (%rsp)
movslq %r14d, %rdi
movq 0x95be(%rip), %rsi      # 0xbfa0
callq 0x22d0
testq %rax, %rax
je 0x2a9d
movq %rax, %rdx
movl %r14d, (%rax)
testl %r13d, %r13d
jle 0x2a74
movq %r15, %r8
decl %r8d
movq (%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
movq %r12, 0x10(%rsp)
movslq (%rbx,%r15,4), %rax
cmpl %r8d, %eax
movq 0x8(%rsp), %r9
leaq 0x8(%r9,%rax,4), %rax
movq %rax, %rcx
cmoveq %r9, %rcx
movslq (%rcx), %r13
movslq -0x4(%rax), %rcx
subq %rcx, %r13
movl %ebp, 0x4(%rdx,%r15,4)
incq %r15
movslq %ebp, %rbp
leaq (%rdx,%rbp), %rdi
movslq -0x4(%rax), %rsi
addq %r9, %rsi
movq %rdx, %r14
movq %r13, %rdx
movq %rbx, %r12
movq %r8, %rbx
callq 0x2130
movq %r14, %rdx
movq %rbx, %r8
movq %r12, %rbx
movq 0x10(%rsp), %r12
addl %r13d, %ebp
cmpq %r15, %r12
jne 0x2a15
movq (%rsp), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x2a8b
movq %rdx, %r14
callq 0x2210
movq %r14, %rdx
movq %rdx, (%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x951c(%rip), %rdi      # 0xbfc0
leaq 0x5559(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0