// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x9964(%rip), %rsi      # 0xbfa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22d0
testq %rax, %rax
je 0x283d
movq %rax, %rbx
movq 0x9947(%rip), %rsi      # 0xbfa0
movl $0x20000, %edi          # imm = 0x20000
callq 0x22d0
testq %rax, %rax
je 0x283d
movq %rax, %r15
movq (%r14), %rdi
testq %rdi, %rdi
je 0x267c
callq 0x2210
movq %r14, 0x20(%rsp)
movq $0x0, (%r14)
movq %r13, %rdi
callq 0x438a
movl %eax, %ebp
movq %r13, %rdi
callq 0x438e
movq %rax, 0x28(%rsp)
leal 0x4(,%rbp,4), %edx
testl %ebp, %ebp
setg %cl
jle 0x27e2
movl %ebp, %eax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movb %cl, 0xf(%rsp)
movq %rdx, 0x10(%rsp)
xorl %r13d, %r13d
movq %r12, %rdi
callq 0x20a0
cmpl $0x7c, %eax
je 0x26ef
cmpl $-0x1, %eax
jne 0x26f9
movq %rbx, %rdi
callq 0x2210
movq %r15, %rdi
callq 0x2210
movl $0x1, %eax
jmp 0x26f4
movl $0x6, %eax
xorl %r14d, %r14d
jmp 0x2708
movslq %r13d, %r13
movb %al, (%rbx,%r13)
incl %r13d
xorl %eax, %eax
movb $0x1, %r14b
testl %eax, %eax
je 0x26c6
cmpl $0x6, %eax
jne 0x2766
movq 0x10(%rsp), %rax
movl %eax, 0x4(%r15,%rbp,4)
movslq %r13d, %rax
movb $0x0, (%rbx,%rax)
movq %rbp, %rax
shlq $0x4, %rax
movq 0x28(%rsp), %rcx
movl 0x8(%rcx,%rax), %eax
movb $0x1, %r14b
testl %eax, %eax
je 0x27a4
cmpl $0x2, %eax
je 0x2783
cmpl $0x1, %eax
jne 0x2766
movq 0x10(%rsp), %r13
movslq %r13d, %rax
testb $0x7, %al
je 0x27bd
orl $0x4, %r13d
addl $0x4, %r13d
movslq %r13d, %rcx
movq %rcx, 0x18(%rsp)
movl %ecx, 0x4(%r15,%rbp,4)
jmp 0x27c2
movq 0x10(%rsp), %rdx
testb %r14b, %r14b
je 0x27de
incq %rbp
cmpq 0x30(%rsp), %rbp
setb %cl
jne 0x26ba
jmp 0x27e2
andl $-0x4, %r13d
movslq 0x10(%rsp), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x2150
movq 0x10(%rsp), %rdx
addl %r13d, %edx
addl $0x4, %edx
jmp 0x276b
movq %rbx, %rdi
callq 0x2290
movq 0x10(%rsp), %rdx
movslq %edx, %rcx
movl %eax, (%r15,%rcx)
addl $0x4, %edx
jmp 0x276b
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
callq 0x2200
movq 0x18(%rsp), %rax
movsd %xmm0, (%r15,%rax)
addl $0x8, %r13d
movq %r13, %rdx
jmp 0x276b
movb 0xf(%rsp), %cl
xorl %eax, %eax
testb $0x1, %cl
jne 0x282e
movl %edx, (%r15)
movslq %edx, %r12
movq 0x97aa(%rip), %rsi      # 0xbfa0
movq %r12, %rdi
callq 0x22d0
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
testq %rax, %rax
je 0x283d
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x2130
movq %rbx, %rdi
callq 0x2210
movq %r15, %rdi
callq 0x2210
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x977c(%rip), %rdi      # 0xbfc0
leaq 0x57b9(%rip), %rsi      # 0x8004
callq 0x2170
movl $0x1, %edi
callq 0x21f0