// File: /workspace/github_cpp_makefile_star2_O2/akgupta007[P]DBMS-query-processing-Algotihms/check.cpp
// Function: binary_search(std::__cxx11::list<int, std::allocator<int>>, FileHandler&, int, int)
list<pair<int, int>> binary_search(list<int> num, FileHandler &fh, int first, int last){
	list<pair<int, int>> location;
	PageHandler ph;
	int mid = (first + last)/2;
	list<int> pr;
	ph = fh.PageAt(mid);
	fh.UnpinPage(mid);
	char *pageData = ph.GetData();
	pr = checkPage (num, pageData); // returns position which is in the page

// for (auto v : pr)
 //        std::cout << v << "  ";
    list<int> first_half;
	list<int> second_half;
	int n;
	list<pair<int, int>>::iterator end_location = location.begin();
	int flag = 0;
	for(list<int>::iterator it=pr.begin(), it_num = num.begin(); it_num != num.end(); ++it, ++it_num)
	{
		if(*it == 0){
			first_half.push_back(*it_num);
			location.push_back(make_pair(mid, *it));
		}
		else{
			memcpy (&n, &pageData[(*it)*sizeof(int)], sizeof(int));
			location.push_back(make_pair(mid, *it));
			if(n == INT_MIN){
				second_half.push_back(*it_num);
				if(flag == 0){
					flag = 1;
					end_location = location.end();
					--end_location;
				}
			}
		}
	}
	
	if (first == last){
		return location;
	}

	if( mid > first)
	{
		if(!first_half.empty())
		{
			list<pair<int, int>> first_location = binary_search(first_half, fh, first, mid - 1);
			//location.insert(location.begin(), first_location.begin(), first_location.end());
			list<pair<int, int>>::iterator start_it = location.begin();
			while( !first_location.empty() ){
				start_it = location.erase(start_it);
				location.insert(start_it, first_location.front());
				first_location.pop_front();
			}
		}
	}
	if(!second_half.empty()){
		list<pair<int, int>> last_location = binary_search(second_half, fh, mid + 1, last);
		while( !last_location.empty() ){
			if ( last_location.front().second != 0 ){
				end_location = location.erase(end_location);
				location.insert(end_location, last_location.front());
			}
			else{
				++end_location;
			}
			last_location.pop_front();
		}
	}
	return location;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %r8d, %r15d
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq %rdi, -0x30(%rbp)
andq $0x0, 0x10(%rdi)
leaq -0xc0(%rbp), %rdi
callq 0x3610
movq %r14, -0xb0(%rbp)
leal (%r15,%r14), %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
movl %eax, %r13d
leaq -0x120(%rbp), %r14
movq %r14, 0x8(%r14)
movq %r14, (%r14)
andq $0x0, 0x10(%r14)
leaq -0x70(%rbp), %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x37f8
movups -0x70(%rbp), %xmm0
movaps %xmm0, -0xc0(%rbp)
movq %r12, %rdi
movq %r13, -0x58(%rbp)
movl %r13d, %esi
callq 0x38a2
leaq -0xc0(%rbp), %rdi
callq 0x3656
movq %rax, -0x88(%rbp)
leaq -0x108(%rbp), %rdi
movq %rbx, %rsi
callq 0x4612
movq %r12, -0x78(%rbp)
movq %r15, -0xa8(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x108(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x3e71
leaq -0x70(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4722
movq %r15, %rdi
callq 0x2f42
leaq -0x108(%rbp), %rdi
callq 0x2f42
movq %r15, 0x8(%r15)
movq %r15, (%r15)
andq $0x0, 0x10(%r15)
leaq -0xa0(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
andq $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movl -0x58(%rbp), %r12d
movb $0x1, %al
movl %eax, -0x34(%rbp)
movq %rbx, %r15
movq (%r15), %r15
cmpq %rbx, %r15
je 0x42af
movq (%r14), %r14
movslq 0x10(%r14), %rax
testq %rax, %rax
je 0x4281
movq -0x88(%rbp), %rcx
movl (%rcx,%rax,4), %r13d
shlq $0x20, %rax
orq %r12, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x464c
negl %r13d
jno 0x4216
leaq 0x10(%r15), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x2de0
testb $0x1, -0x34(%rbp)
je 0x4278
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x34(%rbp)
jmp 0x4216
leaq 0x10(%r15), %rsi
leaq -0x70(%rbp), %rdi
callq 0x2de0
movl 0x10(%r14), %eax
shlq $0x20, %rax
orq %r12, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x464c
jmp 0x4216
movq -0xa8(%rbp), %r12
movq -0xb0(%rbp), %r15
cmpl %r12d, %r15d
movq -0x58(%rbp), %r14
je 0x440c
leaq -0x70(%rbp), %rax
cmpq %rax, -0x70(%rbp)
sete %al
cmpl %r15d, %r14d
jle 0x4369
testb %al, %al
jne 0x4369
leaq -0xf0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x4612
leal -0x1(%r14), %r8d
leaq -0x50(%rbp), %rbx
leaq -0xf0(%rbp), %rsi
movq %rbx, %rdi
movq -0x78(%rbp), %rdx
movl %r15d, %ecx
callq 0x40ee
leaq -0xf0(%rbp), %rdi
callq 0x2f42
movq -0x30(%rbp), %rax
movq (%rax), %r14
movq -0x30(%rbp), %r15
cmpq %rbx, -0x50(%rbp)
je 0x435c
movq %r15, %rdi
movq %r14, %rsi
callq 0x465c
movq %rax, %r14
movq -0x50(%rbp), %rdx
addq $0x10, %rdx
movq %r15, %rdi
movq %rax, %rsi
callq 0x4674
movq %rbx, %rdi
callq 0x46ac
jmp 0x432b
leaq -0x50(%rbp), %rdi
callq 0x46ba
movq -0x58(%rbp), %r14
leaq -0xa0(%rbp), %rax
cmpq %rax, -0xa0(%rbp)
je 0x440c
leaq -0xd8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4612
incl %r14d
leaq -0x50(%rbp), %rbx
leaq -0xd8(%rbp), %rsi
movq %rbx, %rdi
movq -0x78(%rbp), %rdx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x40ee
leaq -0xd8(%rbp), %rdi
callq 0x2f42
movq -0x80(%rbp), %r15
movq -0x30(%rbp), %r14
movq -0x50(%rbp), %rax
cmpq %rbx, %rax
je 0x4403
cmpl $0x0, 0x14(%rax)
je 0x43f6
movq %r14, %rdi
movq %r15, %rsi
callq 0x465c
movq %rax, %r15
movq -0x50(%rbp), %rdx
addq $0x10, %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x4674
jmp 0x43f9
movq (%r15), %r15
movq %rbx, %rdi
callq 0x46ac
jmp 0x43c4
leaq -0x50(%rbp), %rdi
callq 0x46ba
leaq -0xa0(%rbp), %rdi
callq 0x2f42
leaq -0x70(%rbp), %rdi
callq 0x2f42
leaq -0x120(%rbp), %rdi
callq 0x2f42
movq -0x30(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq -0xf0(%rbp), %rdi
jmp 0x445b
jmp 0x4496
movq %rax, %r14
leaq -0xd8(%rbp), %rdi
callq 0x2f42
jmp 0x4499
jmp 0x4496
jmp 0x4496
movq %rax, %r14
leaq -0x108(%rbp), %rdi
jmp 0x44a9
jmp 0x4478
jmp 0x4478
jmp 0x4478
movq %rax, %r14
jmp 0x44ae
movq %rax, %r14
jmp 0x44ba
jmp 0x4484
movq %rax, %r14
leaq -0x50(%rbp), %rdi
callq 0x46ba
jmp 0x4499
jmp 0x4496
jmp 0x4496
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x2f42
leaq -0x70(%rbp), %rdi
callq 0x2f42
leaq -0x120(%rbp), %rdi
callq 0x2f42
movq -0x30(%rbp), %rdi
callq 0x46ba
movq %r14, %rdi
callq 0x2240