// File: PrarabdhJoshi[P]DatabaseImplementation/Record.cc
// Function: Record::SuckNextRecord(Schema*, _IO_FILE*)
int Record :: SuckNextRecord (Schema *mySchema, FILE *textFile) {

	// this is temporary storage
	char *space = new (std::nothrow) char[PAGE_SIZE];
	if (space == NULL)
	{
		cout << "ERROR : Not enough memory. EXIT !!!\n";
		exit(1);
	}

	char *recSpace = new (std::nothrow) char[PAGE_SIZE];
	if (recSpace == NULL)
	{
		cout << "ERROR : Not enough memory. EXIT !!!\n";
		exit(1);
	}

	// clear out the present record
	if (bits != NULL) 
		delete [] bits;
	bits = NULL;

	int n = mySchema->GetNumAtts();
	Attribute *atts = mySchema->GetAtts();

	// this is the current position (int bytes) in the binary
	// representation of the record that we are dealing with
	int currentPosInRec = sizeof (int) * (n + 1);

	// loop through all of the attributes
	for (int i = 0; i < n; i++) {
		
		// first we suck in the next attribute value
		int len = 0;
		while (1) {
			int nextChar = getc (textFile);
			if (nextChar == '|')
				break;
			else if (nextChar == EOF) {
				delete [] space;
				delete [] recSpace;
				return 0;
			}

			space[len] = nextChar;
			len++;
		}

		// set up the pointer to the current attribute in the record
		((int *) recSpace)[i + 1] = currentPosInRec;

		// null terminate the string
		space[len] = 0;
		len++;

		// then we convert the data to the correct binary representation
		if (atts[i].myType == Int) {
			*((int *) &(recSpace[currentPosInRec])) = atoi (space);	
			currentPosInRec += sizeof (int);

		} else if (atts[i].myType == Double) {

			// make sure that we are starting at a double-aligned position;
			// if not, then we put some extra space in there
			while (currentPosInRec % sizeof(double) != 0) {
				currentPosInRec += sizeof (int);
				((int *) recSpace)[i + 1] = currentPosInRec;
			}

			*((double *) &(recSpace[currentPosInRec])) = atof (space);
			currentPosInRec += sizeof (double);

		} else if (atts[i].myType == String) {

			// align things to the size of an integer if needed
			if (len % sizeof (int) != 0) {
				len += sizeof (int) - (len % sizeof (int));
			}

			strcpy (&(recSpace[currentPosInRec]), space); 
			currentPosInRec += len;

		} 
		
	}

	// the last thing is to set up the pointer to just past the end of the reocrd
	((int *) recSpace)[0] = currentPosInRec;

	// and copy over the bits
	bits = new (std::nothrow) char[currentPosInRec];
	if (bits == NULL)
	{
		cout << "ERROR : Not enough memory. EXIT !!!\n";
		exit(1);
	}

	memcpy (bits, recSpace, currentPosInRec);	

	delete [] space;
	delete [] recSpace;

	return 1;
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movq %rdx, 0x68(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x20000, %edi          # imm = 0x20000
movq 0xd894(%rip), %rsi      # 0xff90
callq 0x2280
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x60(%rsp)
jne 0x272b
movq 0xd8a3(%rip), %rdi      # 0xffb8
leaq 0x98e8(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movl $0x20000, %edi          # imm = 0x20000
movq 0xd859(%rip), %rsi      # 0xff90
callq 0x2280
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x2766
movq 0xd868(%rip), %rdi      # 0xffb8
leaq 0x98ad(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax)
je 0x2790
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x278e
movq 0x28(%rsp), %rdi
callq 0x21e0
jmp 0x2790
movq 0x30(%rsp), %rax
movq $0x0, (%rax)
movq 0x70(%rsp), %rdi
callq 0x5d30
movl %eax, 0x54(%rsp)
movq 0x70(%rsp), %rdi
callq 0x5d40
movq %rax, 0x48(%rsp)
movl 0x54(%rsp), %eax
addl $0x1, %eax
cltq
shlq $0x2, %rax
movl %eax, 0x44(%rsp)
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0x29d1
movl $0x0, 0x3c(%rsp)
movq 0x68(%rsp), %rdi
callq 0x2090
movl %eax, 0x38(%rsp)
cmpl $0x7c, 0x38(%rsp)
jne 0x27ff
jmp 0x286f
cmpl $-0x1, 0x38(%rsp)
jne 0x284a
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x2820
movq 0x20(%rsp), %rdi
callq 0x21e0
movq 0x58(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x283a
movq 0x18(%rsp), %rdi
callq 0x21e0
movl $0x0, 0x84(%rsp)
jmp 0x2a71
jmp 0x284c
movl 0x38(%rsp), %eax
movb %al, %dl
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x27e8
movl 0x44(%rsp), %edx
movq 0x58(%rsp), %rax
movl 0x40(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movq 0x60(%rsp), %rax
movslq 0x3c(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
movq 0x48(%rsp), %rax
movslq 0x40(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jne 0x28e0
movq 0x60(%rsp), %rdi
callq 0x2250
movl %eax, %edx
movq 0x58(%rsp), %rax
movslq 0x44(%rsp), %rcx
movl %edx, (%rax,%rcx)
movslq 0x44(%rsp), %rax
addq $0x4, %rax
movl %eax, 0x44(%rsp)
jmp 0x29bf
movq 0x48(%rsp), %rax
movslq 0x40(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x1, 0x8(%rax)
jne 0x2955
jmp 0x28f9
movslq 0x44(%rsp), %rax
andq $0x7, %rax
cmpq $0x0, %rax
je 0x292d
movslq 0x44(%rsp), %rax
addq $0x4, %rax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %edx
movq 0x58(%rsp), %rax
movl 0x40(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x28f9
movq 0x60(%rsp), %rdi
callq 0x21d0
movq 0x58(%rsp), %rax
movslq 0x44(%rsp), %rcx
movsd %xmm0, (%rax,%rcx)
movslq 0x44(%rsp), %rax
addq $0x8, %rax
movl %eax, 0x44(%rsp)
jmp 0x29bd
movq 0x48(%rsp), %rax
movslq 0x40(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpl $0x2, 0x8(%rax)
jne 0x29bb
movslq 0x3c(%rsp), %rax
andq $0x3, %rax
cmpq $0x0, %rax
je 0x2998
movslq 0x3c(%rsp), %rax
andq $0x3, %rax
movl $0x4, %ecx
subq %rax, %rcx
movslq 0x3c(%rsp), %rax
addq %rcx, %rax
movl %eax, 0x3c(%rsp)
movq 0x58(%rsp), %rdi
movslq 0x44(%rsp), %rax
addq %rax, %rdi
movq 0x60(%rsp), %rsi
callq 0x2130
movl 0x3c(%rsp), %eax
addl 0x44(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x29bd
jmp 0x29bf
jmp 0x29c1
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x27d2
movl 0x44(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, (%rax)
movslq 0x44(%rsp), %rdi
movq 0xd5a8(%rip), %rsi      # 0xff90
callq 0x2280
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x2a1b
movq 0xd5b3(%rip), %rdi      # 0xffb8
leaq 0x95f8(%rip), %rsi      # 0xc004
callq 0x2140
movl $0x1, %edi
callq 0x21c0
movq 0x30(%rsp), %rax
movq (%rax), %rdi
movq 0x58(%rsp), %rsi
movslq 0x44(%rsp), %rdx
callq 0x2110
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x2a4c
movq 0x10(%rsp), %rdi
callq 0x21e0
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2a66
movq 0x8(%rsp), %rdi
callq 0x21e0
movl $0x1, 0x84(%rsp)
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq