// File: /workspace/github_cpp_makefile_star2_O2/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, %rbp
movq %rdi, %r14
movq 0x89a8(%rip), %rsi      # 0xafa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22a0
testq %rax, %rax
je 0x27b6
movq %rax, %rbx
movq 0x898b(%rip), %rsi      # 0xafa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22a0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x27b6
movq (%r14), %rdi
testq %rdi, %rdi
je 0x263a
callq 0x21e0
movq %r14, 0x10(%rsp)
andq $0x0, (%r14)
movq %rbp, %rdi
callq 0x3f54
movl %eax, %r14d
movq %rbp, %rdi
callq 0x3f58
movq %rax, 0x18(%rsp)
leal 0x4(,%r14,4), %r15d
xorl %r13d, %r13d
testl %r14d, %r14d
cmovlel %r13d, %r14d
movq %r14, 0x20(%rsp)
movslq %r15d, %rbp
cmpq %r14, %r13
je 0x276f
xorl %r14d, %r14d
movq %r12, %rdi
callq 0x2090
cmpl $-0x1, %eax
je 0x274c
cmpl $0x7c, %eax
je 0x26a0
movb %al, (%rbx,%r14)
incq %r14
jmp 0x2681
movq 0x8(%rsp), %rax
movl %r15d, 0x4(%rax,%r13,4)
movl %r14d, %eax
movb $0x0, (%rbx,%rax)
movq %r13, %rax
shlq $0x4, %rax
movq 0x18(%rsp), %rcx
movl 0x8(%rcx,%rax), %eax
testl %eax, %eax
je 0x2716
cmpl $0x2, %eax
je 0x26e8
cmpl $0x1, %eax
movq 0x8(%rsp), %rbp
jne 0x273f
movslq %r15d, %r14
testb $0x7, %r14b
je 0x272c
addl $0x4, %r15d
movl %r15d, 0x4(%rbp,%r13,4)
jmp 0x26d4
incl %r14d
movl %r14d, %eax
andl $0x7ffffffc, %eax       # imm = 0x7FFFFFFC
addl $0x4, %eax
testb $0x3, %r14b
cmovel %r14d, %eax
movl %eax, %r14d
addq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x2130
addl %r14d, %r15d
jmp 0x273f
movq %rbx, %rdi
callq 0x2260
movq 0x8(%rsp), %rcx
movl %eax, (%rcx,%rbp)
addl $0x4, %r15d
jmp 0x273f
movq %rbx, %rdi
callq 0x21d0
movsd %xmm0, (%rbp,%r14)
addl $0x8, %r15d
incq %r13
movq 0x20(%rsp), %r14
jmp 0x2672
movq %rbx, %rdi
callq 0x21e0
movq 0x8(%rsp), %rdi
callq 0x21e0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r14
movl %r15d, (%r14)
movq 0x8822(%rip), %rsi      # 0xafa0
movq %rbp, %rdi
callq 0x22a0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x27b6
movq %rax, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2110
movq %rbx, %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x21e0
pushq $0x1
popq %rax
jmp 0x2760
movq 0x8803(%rip), %rdi      # 0xafc0
leaq 0x4840(%rip), %rsi      # 0x7004
callq 0x2140
pushq $0x1
popq %rdi
callq 0x21c0
nop