// File: 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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edx, 0x34(%rsp)
movl %ecx, 0x30(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x2d4d
movq 0x38(%rsp), %rax
movslq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x30(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2cfc
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movl (%rax), %eax
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movslq 0x28(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x2d3b
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x38(%rsp), %rdx
movslq 0x28(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x2, %edx
movslq %edx, %rdx
movl (%rax,%rdx,4), %eax
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movslq 0x28(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
addl 0x2c(%rsp), %eax
movl %eax, 0x2c(%rsp)
jmp 0x2d3d
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x2ca9
movslq 0x2c(%rsp), %rdi
movq 0xd237(%rip), %rsi      # 0xff90
callq 0x2280
movq %rax, 0x20(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x2d88
movq 0xd246(%rip), %rdi      # 0xffb8
leaq 0x928b(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movl 0x2c(%rsp), %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
movl 0x34(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, 0x1c(%rsp)
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x34(%rsp), %eax
jge 0x2ea4
movq 0x38(%rsp), %rax
movslq 0x18(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0x30(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x2dfb
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movl (%rax), %eax
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movslq 0x18(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, 0x14(%rsp)
jmp 0x2e36
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x38(%rsp), %rdx
movslq 0x18(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x2, %edx
movslq %edx, %rdx
movl (%rax,%rdx,4), %eax
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movslq 0x18(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
addl $0x1, %edx
movslq %edx, %rdx
subl (%rcx,%rdx,4), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rax
movl 0x1c(%rsp), %esi
movq 0x20(%rsp), %rcx
movl 0x18(%rsp), %edx
addl $0x1, %edx
movslq %edx, %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x20(%rsp), %rdi
movslq 0x1c(%rsp), %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movq (%rax), %rax
movq 0x38(%rsp), %rcx
movslq 0x18(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movslq (%rax,%rcx,4), %rax
addq %rax, %rsi
movslq 0x14(%rsp), %rdx
callq 0x2110
movl 0x14(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x2dac
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x2ebf
movq (%rsp), %rdi
callq 0x21e0
movq 0x8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)