// File: PrarabdhJoshi[P]DatabaseImplementation/File.cc
// Function: Page::FromBinary(char*)
void Page :: FromBinary (char *bits) {

	// first read the number of records on the page
	numRecs = ((int *) bits)[0];

	// sanity check
	if (numRecs > 1000000 || numRecs < 0) {
		cerr << "This is probably an error.  Found " << numRecs << " records on a page.\n";
		exit (1);
	}

	// and now get the binary representations of each
	char *curPos = bits + sizeof (int);

	// first, empty out the list of current records
	myRecs->MoveToStart ();
	while (myRecs->RightLength ()) {
		Record temp;
		myRecs->Remove(&temp);
	}

	// now loop through and re-populate it
	Record *temp = new (std::nothrow) Record();
	if (temp == NULL)
	{
		cout << "ERROR : Not enough memory. EXIT !!!\n";
		exit(1);
	}

	curSizeInBytes = sizeof (int);
	for (int i = 0; i < numRecs; i++) {

		// get the length of the current record
		int len = ((int *) curPos)[0];
		curSizeInBytes += len;

		// create the record
		temp->CopyBits(curPos, len);

		// add it
		myRecs->Insert(temp);

		// and move along
		myRecs->Advance ();
		curPos += len;
	}

	delete temp;
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x68(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x10(%rax)
cmpl $0xf4240, 0x10(%rax)    # imm = 0xF4240
jg 0x65f6
movq 0x20(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jge 0x6632
movq 0x99eb(%rip), %rdi      # 0xffe8
leaq 0x5cb2(%rip), %rsi      # 0xc2b6
callq 0x2140
movq %rax, %rdi
movq 0x20(%rsp), %rax
movl 0x10(%rax), %esi
callq 0x2290
movq %rax, %rdi
leaq 0x5cb6(%rip), %rsi      # 0xc2d9
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movq 0x20(%rsp), %rax
movq 0x68(%rsp), %rcx
addq $0x4, %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%rax), %rdi
callq 0x6f90
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x6fd0
cmpl $0x0, %eax
je 0x66ae
leaq 0x58(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x2660
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rsi
movq 0x8(%rax), %rdi
callq 0x6ff0
jmp 0x6685
leaq 0x58(%rsp), %rdi
callq 0x2680
jmp 0x664e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x2680
jmp 0x6813
movl $0x8, %edi
movq 0x98d6(%rip), %rsi      # 0xff90
callq 0x2350
movq %rax, %rcx
movq %rcx, 0x8(%rsp)
movb $0x0, 0x37(%rsp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, 0x10(%rsp)
je 0x66fb
movq 0x8(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movb $0x1, 0x37(%rsp)
callq 0x2660
jmp 0x66ef
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x66fb
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
jne 0x6757
movq 0x98a4(%rip), %rdi      # 0xffb8
leaq 0x58e9(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
testb $0x1, 0x37(%rsp)
jne 0x6741
jmp 0x6752
movq 0x38(%rsp), %rdi
movq 0x9843(%rip), %rsi      # 0xff90
callq 0x20e0
jmp 0x6813
movq 0x20(%rsp), %rax
movl $0x4, 0x14(%rax)
movl $0x0, 0x30(%rsp)
movq 0x20(%rsp), %rcx
movl 0x30(%rsp), %eax
cmpl 0x10(%rcx), %eax
jge 0x67e8
movq 0x20(%rsp), %rax
movq 0x60(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x2c(%rsp)
movl 0x2c(%rsp), %ecx
addl 0x14(%rax), %ecx
movl %ecx, 0x14(%rax)
movq 0x40(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x2c(%rsp), %edx
callq 0x2ae0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x40(%rsp), %rsi
callq 0x70d0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x7320
movl 0x2c(%rsp), %ecx
movq 0x60(%rsp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x676b
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x680e
movq (%rsp), %rdi
callq 0x2680
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x2170
addq $0x78, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x22b0
nopl (%rax)