// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r15
movq 0x9964(%rip), %rsi      # 0xbfa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22d0
testq %rax, %rax
je 0x27f2
movq %rax, %rbx
movq 0x9947(%rip), %rsi      # 0xbfa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22d0
testq %rax, %rax
je 0x27f2
movq %rax, %r14
movq (%r15), %rdi
testq %rdi, %rdi
je 0x267c
callq 0x2210
movq %r15, 0x18(%rsp)
movq $0x0, (%r15)
movq %r13, %rdi
callq 0x427a
movl %eax, %ebp
movq %r13, %rdi
callq 0x427e
movq %rax, 0x20(%rsp)
leal 0x4(,%rbp,4), %r13d
testl %ebp, %ebp
jle 0x278a
movl %ebp, %eax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r12, %rdi
callq 0x20a0
cmpl $-0x1, %eax
je 0x27d1
cmpl $0x7c, %eax
je 0x26da
movb %al, (%rbx,%r15)
incq %r15
jmp 0x26bb
movl %r13d, 0x4(%r14,%rbp,4)
movl %r15d, %eax
movb $0x0, (%rbx,%rax)
movq %rbp, %rax
shlq $0x4, %rax
movq 0x20(%rsp), %rcx
movl 0x8(%rcx,%rax), %eax
testl %eax, %eax
je 0x2769
cmpl $0x2, %eax
je 0x2731
cmpl $0x1, %eax
jne 0x277c
movslq %r13d, %r15
testb $0x7, %r15b
je 0x271d
orl $0x4, %r13d
addl $0x4, %r13d
movslq %r13d, %r15
movl %r15d, 0x4(%r14,%rbp,4)
movq %rbx, %rdi
callq 0x2200
movsd %xmm0, (%r14,%r15)
addl $0x8, %r13d
jmp 0x277c
incl %r15d
movq %r14, 0x8(%rsp)
movl %r15d, %r14d
andl $0x7ffffffc, %r14d      # imm = 0x7FFFFFFC
addl $0x4, %r14d
testb $0x3, %r15b
cmovel %r15d, %r14d
movslq %r13d, %rdi
addq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x2150
addl %r14d, %r13d
movq 0x8(%rsp), %r14
jmp 0x277c
movq %rbx, %rdi
callq 0x2290
movslq %r13d, %rcx
movl %eax, (%r14,%rcx)
addl $0x4, %r13d
incq %rbp
cmpq 0x10(%rsp), %rbp
jne 0x26b8
movl %r13d, (%r14)
movslq %r13d, %r12
movq 0x9809(%rip), %rsi      # 0xbfa0
movq %r12, %rdi
callq 0x22d0
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x27f2
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x2130
movq %rbx, %rdi
callq 0x2210
movq %r14, %rdi
callq 0x2210
movl $0x1, %eax
jmp 0x27e3
movq %rbx, %rdi
callq 0x2210
movq %r14, %rdi
callq 0x2210
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x97c7(%rip), %rdi      # 0xbfc0
leaq 0x5804(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0
nop