// File: akgupta007[P]DBMS-query-processing-Algotihms/check.cpp
// Function: InsertValue(int, int, int, FileHandler&)
void InsertValue(int num, int position, int pageNo, FileHandler &fh){
	try{
		PageHandler ph = fh.PageAt(pageNo);
		bool pin = fh.MarkDirty(pageNo);
		char *pageData = ph.GetData();
		int n;
		int max_data = PAGE_CONTENT_SIZE/sizeof(int);
		for(int i = position; i< max_data; i++){
			memcpy (&n, &pageData[i*sizeof(int)], sizeof(int));
			if( n == INT_MIN ){
				if (i == max_data -1){
					fh.UnpinPage(pageNo);
					InsertValue(num, 0, pageNo + 1, fh);
					return;
				}
				else {
					memcpy (&pageData[i*sizeof(int)], &num, sizeof(int));
					memcpy (&pageData[(i+1)*sizeof(int)], &n, sizeof(int));
					break;
				}
			}
			memcpy (&pageData[i*sizeof(int)], &num, sizeof(int));
			num = n;
		}
		return;
	}
	catch(InvalidPageException args){
		//create New Page
		PageHandler ph = fh.NewPage();
		char *pageData = ph.GetData();
		int m = INT_MIN;
		memcpy (&pageData[0], &m, sizeof(int));
		InsertValue(num, 0, ph.GetPageNum(), fh);
		return;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movq %rcx, -0x18(%rbp)
movq -0x18(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq -0x28(%rbp), %rdi
callq 0x6b20
jmp 0x78fa
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x6da0
movb %al, -0x75(%rbp)
jmp 0x790b
movb -0x75(%rbp), %al
andb $0x1, %al
movb %al, -0x35(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x6800
movq %rax, -0x80(%rbp)
jmp 0x7922
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movl $0x3ff, -0x48(%rbp)     # imm = 0x3FF
movl -0x8(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x7aaf
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x2, %rcx
movl (%rax,%rcx), %eax
movl %eax, -0x44(%rbp)
cmpl $0x80000000, -0x44(%rbp) # imm = 0x80000000
jne 0x7a89
movl -0x4c(%rbp), %eax
movl -0x48(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x7a5e
movq -0x18(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x6c50
jmp 0x7981
movl -0x4(%rbp), %edi
movl -0xc(%rbp), %edx
incl %edx
movq -0x18(%rbp), %rcx
xorl %esi, %esi
callq 0x78d0
jmp 0x7996
jmp 0x7acf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x7ad8
movq -0x30(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0x21d0
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
callq 0x8450
movq -0x88(%rbp), %rdi
callq 0x20a0
movq -0x18(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x6c90
jmp 0x79ee
leaq -0x68(%rbp), %rdi
callq 0x6800
movq %rax, -0x90(%rbp)
jmp 0x7a00
movq -0x90(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x80000000, -0x74(%rbp) # imm = 0x80000000
movq -0x70(%rbp), %rax
movl -0x74(%rbp), %ecx
movl %ecx, (%rax)
movl -0x4(%rbp), %eax
movl %eax, -0x98(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x6820
movl %eax, -0x94(%rbp)
jmp 0x7a35
movl -0x94(%rbp), %edx
movl -0x98(%rbp), %edi
movq -0x18(%rbp), %rcx
xorl %esi, %esi
callq 0x78d0
jmp 0x7a4e
leaq -0x58(%rbp), %rdi
callq 0x7400
callq 0x2240
jmp 0x7acf
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x2, %rcx
movl -0x4(%rbp), %edx
movl %edx, (%rax,%rcx)
movq -0x40(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
movl -0x44(%rbp), %edx
movl %edx, (%rax,%rcx)
jmp 0x7aaf
movq -0x40(%rbp), %rax
movslq -0x4c(%rbp), %rcx
shlq $0x2, %rcx
movl -0x4(%rbp), %edx
movl %edx, (%rax,%rcx)
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x7937
jmp 0x7acf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x7400
callq 0x2240
jmp 0x7acd
jmp 0x7ad8
addq $0xa0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x2270
movq %rax, %rdi
callq 0x4170
nopl (%rax)