// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movl %r8d, %ebx
movl %ecx, %r12d
movq %rdx, %r15
movq %rsi, -0x30(%rbp)
movq %rdi, %r14
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
leaq -0xe0(%rbp), %rdi
movq %r14, -0x38(%rbp)
callq 0x3620
movq %rbx, -0xb0(%rbp)
movq %r12, -0xd0(%rbp)
leal (%rbx,%r12), %eax
movl %eax, %ebx
shrl $0x1f, %ebx
addl %eax, %ebx
sarl %ebx
leaq -0x80(%rbp), %r13
movq %r13, 0x8(%r13)
movq %r13, (%r13)
movq $0x0, 0x10(%r13)
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x3824
movups -0x50(%rbp), %xmm0
movaps %xmm0, -0xe0(%rbp)
movq %r15, -0xa8(%rbp)
movq %r15, %rdi
movq %rbx, -0x88(%rbp)
movl %ebx, %esi
callq 0x38dc
leaq -0xe0(%rbp), %rdi
callq 0x366c
movq %rax, -0xc8(%rbp)
leaq -0x128(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x4a5c
leaq -0x50(%rbp), %r15
leaq -0x128(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq -0xc8(%rbp), %rdx
callq 0x3f46
movq -0x80(%rbp), %rdi
cmpq %r13, %rdi
je 0x4344
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x4334
movq %r13, -0x78(%rbp)
movq %r13, -0x80(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x50(%rbp), %rax
cmpq %r15, %rax
je 0x4389
movq %rax, -0x80(%rbp)
leaq -0x40(%rbp), %rax
movq -0x8(%rax), %rcx
movq %rcx, -0x78(%rbp)
movq %r13, (%rcx)
movq -0x80(%rbp), %rcx
movq %r13, 0x8(%rcx)
movq (%rax), %rcx
movq %rcx, -0x70(%rbp)
movq %r15, -0x8(%rax)
movq %r15, -0x10(%rax)
jmp 0x4398
leaq -0x70(%rbp), %rax
leaq -0x80(%rbp), %rcx
movq %rcx, 0x8(%rcx)
movq %rcx, (%rcx)
movq $0x0, (%rax)
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x43b8
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x43a8
movq -0x128(%rbp), %rdi
cmpq %r12, %rdi
je 0x43d4
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x43c4
movq %r15, -0x48(%rbp)
movq %r15, -0x50(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x40(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %rcx, 0x10(%rax)
movq (%r14), %rax
movq %rax, -0xb8(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %r14
cmpq %rax, %r14
je 0x4514
movq $0x0, -0xc0(%rbp)
movl -0x88(%rbp), %r15d
movq (%r13), %r13
movslq 0x10(%r13), %r12
testq %r12, %r12
je 0x44ba
movq -0xc8(%rbp), %rax
movl (%rax,%r12,4), %ebx
movl $0x18, %edi
callq 0x21b0
shlq $0x20, %r12
orq %r15, %r12
movq %r12, 0x10(%rax)
movq %rax, %rdi
movq -0x38(%rbp), %r12
movq %r12, %rsi
callq 0x2050
incq 0x10(%r12)
negl %ebx
jno 0x4507
movl $0x18, %edi
callq 0x21b0
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%rax)
movq %rax, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2050
incq -0x90(%rbp)
cmpl $0x0, -0xc0(%rbp)
jne 0x44ac
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xb8(%rbp)
movl $0x1, %eax
movq %rax, -0xc0(%rbp)
jmp 0x4507
movl $0x18, %edi
callq 0x21b0
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%rax)
movq %rax, %rdi
leaq -0x50(%rbp), %rsi
callq 0x2050
incq -0x40(%rbp)
movl 0x10(%r13), %ebx
movl $0x18, %edi
callq 0x21b0
shlq $0x20, %rbx
orq %r15, %rbx
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq -0x38(%rbp), %rbx
movq %rbx, %rsi
callq 0x2050
incq 0x10(%rbx)
movq (%r14), %r14
cmpq -0x30(%rbp), %r14
jne 0x4420
movq -0xd0(%rbp), %r12
cmpl -0xb0(%rbp), %r12d
movq -0x38(%rbp), %r14
leaq -0x50(%rbp), %r15
movq -0x88(%rbp), %rbx
je 0x46f8
cmpq %r15, -0x50(%rbp)
sete %al
cmpl %r12d, %ebx
jle 0x4612
testb %al, %al
jne 0x4612
leaq -0x110(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x4a5c
movq -0xa8(%rbp), %rdx
leal -0x1(%rbx), %r8d
leaq -0x68(%rbp), %rdi
leaq -0x110(%rbp), %r15
movq %r15, %rsi
movl %r12d, %ecx
callq 0x4248
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x459c
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x458c
leaq -0x68(%rbp), %r12
cmpq %r12, -0x68(%rbp)
je 0x4607
movq (%r14), %r13
movq %r13, %r15
movq (%r13), %r13
decq 0x10(%r14)
movq %r15, %rdi
callq 0x2230
movq %r15, %rdi
callq 0x2180
movq -0x68(%rbp), %rbx
movl $0x18, %edi
callq 0x21b0
movq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0x2050
incq 0x10(%r14)
movq -0x68(%rbp), %r15
decq -0x58(%rbp)
movq %r15, %rdi
callq 0x2230
movq %r15, %rdi
callq 0x2180
cmpq %r12, -0x68(%rbp)
jne 0x45a9
leaq -0x50(%rbp), %r15
movq -0x88(%rbp), %rbx
leaq -0xa0(%rbp), %rax
cmpq %rax, -0xa0(%rbp)
je 0x46f8
leaq -0xf8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x4a5c
incl %ebx
movq %rbx, %rcx
leaq -0x68(%rbp), %r13
leaq -0xf8(%rbp), %r15
movq %r13, %rdi
movq %r15, %rsi
movq -0xa8(%rbp), %rdx
movq -0xb0(%rbp), %r8
callq 0x4248
movq -0xf8(%rbp), %rdi
cmpq %r15, %rdi
je 0x467e
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x466e
movq -0x68(%rbp), %rax
cmpq %r13, %rax
movq -0xb8(%rbp), %rbx
je 0x46f4
cmpl $0x0, 0x14(%rax)
movq (%rbx), %r15
je 0x46d0
decq 0x10(%r14)
movq %rbx, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2180
movq -0x68(%rbp), %rbx
movl $0x18, %edi
callq 0x21b0
movq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0x2050
incq 0x10(%r14)
movq -0x68(%rbp), %r12
decq -0x58(%rbp)
movq %r12, %rdi
callq 0x2230
movq %r12, %rdi
callq 0x2180
movq -0x68(%rbp), %rax
movq %r15, %rbx
cmpq %r13, %rax
jne 0x468e
leaq -0x50(%rbp), %r15
movq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %r14
cmpq %r14, %rdi
je 0x471b
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x470b
movq -0x50(%rbp), %rdi
cmpq %r15, %rdi
je 0x4734
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x4724
leaq -0x80(%rbp), %rbx
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x4750
movq (%rdi), %r14
callq 0x2180
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x4740
movq -0x38(%rbp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, -0x30(%rbp)
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x484d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x477a
jmp 0x484d
jmp 0x4849
movq %rax, -0x30(%rbp)
movq -0xf8(%rbp), %rdi
cmpq %r15, %rdi
je 0x484d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x47a8
jmp 0x484d
jmp 0x4849
movq %rax, -0x30(%rbp)
movq -0x128(%rbp), %rdi
cmpq %r12, %rdi
je 0x488d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x47d6
jmp 0x488d
jmp 0x47f1
jmp 0x47f1
jmp 0x47f1
movq %rax, -0x30(%rbp)
jmp 0x488d
movq %rax, -0x30(%rbp)
jmp 0x48aa
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rdi
cmpq %r13, %rdi
je 0x484d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x4810
jmp 0x484d
movq %rax, -0x30(%rbp)
movq -0x68(%rbp), %rdi
cmpq %r12, %rdi
je 0x484d
leaq -0x68(%rbp), %r14
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4833
jmp 0x484d
jmp 0x4849
jmp 0x4849
movq %rax, -0x30(%rbp)
movq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %r14
cmpq %r14, %rdi
je 0x4870
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4860
movq -0x50(%rbp), %rdi
leaq -0x50(%rbp), %r14
cmpq %r14, %rdi
je 0x488d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x487d
movq -0x80(%rbp), %rdi
leaq -0x80(%rbp), %r14
cmpq %r14, %rdi
je 0x48aa
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x489a
movq -0x38(%rbp), %r14
movq (%r14), %rdi
cmpq %r14, %rdi
je 0x48c6
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x48b6
movq -0x30(%rbp), %rdi
callq 0x2280