// File: /workspace/github_cpp_makefile_star2_O3/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, -0x88(%rbp)
movq %rdi, %r14
movq %rdi, 0x8(%rdi)
movq %rdi, (%rdi)
movq $0x0, 0x10(%rdi)
leaq -0xe0(%rbp), %rdi
movq %r14, -0x30(%rbp)
callq 0x35ec
movq %rbx, -0xb0(%rbp)
movq %r12, -0xd0(%rbp)
leal (%rbx,%r12), %eax
movl %eax, %ebx
shrl $0x1f, %ebx
addl %eax, %ebx
sarl %ebx
leaq -0x78(%rbp), %r13
movq %r13, 0x8(%r13)
movq %r13, (%r13)
movq $0x0, 0x10(%r13)
leaq -0x48(%rbp), %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x37f0
movups -0x48(%rbp), %xmm0
movaps %xmm0, -0xe0(%rbp)
movq %r15, %rdi
movq %rbx, -0x80(%rbp)
movl %ebx, %esi
callq 0x38a8
leaq -0xe0(%rbp), %rdi
callq 0x3638
movq %rax, -0xc8(%rbp)
leaq -0x128(%rbp), %rdi
movq -0x88(%rbp), %rsi
callq 0x49dc
movq %r15, -0xa8(%rbp)
leaq -0x48(%rbp), %r15
leaq -0x128(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
movq -0xc8(%rbp), %rdx
callq 0x3ee6
movq -0x78(%rbp), %rdi
cmpq %r13, %rdi
je 0x42e7
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r13, %rbx
jne 0x42d7
movq %r13, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq %r15, %rax
je 0x4328
movq %rax, -0x78(%rbp)
leaq -0x38(%rbp), %rax
movq -0x8(%rax), %rcx
movq %rcx, -0x70(%rbp)
movq %r13, (%rcx)
movq -0x78(%rbp), %rcx
movq %r13, 0x8(%rcx)
movq (%rax), %rcx
movq %rcx, -0x68(%rbp)
movq %r15, -0x8(%rax)
movq %r15, -0x10(%rax)
jmp 0x4333
leaq -0x68(%rbp), %rax
leaq -0x78(%rbp), %rcx
movq %rcx, (%rcx)
movq $0x0, (%rax)
movq -0x128(%rbp), %rdi
cmpq %r12, %rdi
je 0x4356
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x4346
movq %r15, -0x40(%rbp)
movq %r15, -0x48(%rbp)
xorl %ecx, %ecx
movq %rcx, -0x38(%rbp)
leaq -0xa0(%rbp), %rax
movq %rax, 0x8(%rax)
movq %rax, (%rax)
movq %rcx, 0x10(%rax)
movq (%r14), %rax
movq %rax, -0xb8(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %r14
cmpq %rax, %r14
je 0x4499
movq $0x0, -0xc0(%rbp)
movl -0x80(%rbp), %r15d
movq (%r13), %r13
movslq 0x10(%r13), %r12
testq %r12, %r12
je 0x443c
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 -0x30(%rbp), %r12
movq %r12, %rsi
callq 0x2050
incq 0x10(%r12)
negl %ebx
jno 0x4489
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 0x442e
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xb8(%rbp)
movl $0x1, %eax
movq %rax, -0xc0(%rbp)
jmp 0x4489
movl $0x18, %edi
callq 0x21b0
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%rax)
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2050
incq -0x38(%rbp)
movl 0x10(%r13), %ebx
movl $0x18, %edi
callq 0x21b0
shlq $0x20, %rbx
orq %r15, %rbx
movq %rbx, 0x10(%rax)
movq %rax, %rdi
movq -0x30(%rbp), %rbx
movq %rbx, %rsi
callq 0x2050
incq 0x10(%rbx)
movq (%r14), %r14
cmpq -0x88(%rbp), %r14
jne 0x43a2
movq -0xb0(%rbp), %rbx
movq -0xd0(%rbp), %r13
cmpl %ebx, %r13d
movq -0x30(%rbp), %r14
leaq -0x48(%rbp), %r15
movq -0x80(%rbp), %r12
je 0x4680
cmpq %r15, -0x48(%rbp)
sete %al
cmpl %r13d, %r12d
jle 0x459b
testb %al, %al
jne 0x459b
leaq -0x110(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x49dc
leal -0x1(%r12), %r8d
leaq -0x60(%rbp), %r12
leaq -0x110(%rbp), %r15
movq %r12, %rdi
movq %r15, %rsi
movq -0xa8(%rbp), %rdx
movl %r13d, %ecx
callq 0x41e8
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x4525
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x4515
cmpq %r12, -0x60(%rbp)
je 0x458c
movq (%r14), %r13
movq %r13, %r15
movq (%r13), %r13
decq 0x10(%r14)
movq %r15, %rdi
callq 0x2230
movq %r15, %rdi
callq 0x2180
movq -0x60(%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 -0x60(%rbp), %r15
decq -0x50(%rbp)
movq %r15, %rdi
callq 0x2230
movq %r15, %rdi
callq 0x2180
cmpq %r12, -0x60(%rbp)
jne 0x452e
leaq -0x48(%rbp), %r15
movq -0xb0(%rbp), %rbx
movq -0x80(%rbp), %r12
leaq -0xa0(%rbp), %rax
cmpq %rax, -0xa0(%rbp)
je 0x4680
leaq -0xf8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x49dc
incl %r12d
leaq -0x60(%rbp), %rdi
leaq -0xf8(%rbp), %r15
movq %r15, %rsi
movq -0xa8(%rbp), %rdx
movl %r12d, %ecx
movl %ebx, %r8d
callq 0x41e8
movq -0xf8(%rbp), %rdi
cmpq %r15, %rdi
je 0x4601
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x45f1
movq -0x60(%rbp), %r13
leaq -0x60(%rbp), %r12
cmpq %r12, %r13
movq -0xb8(%rbp), %rbx
je 0x467c
cmpl $0x0, 0x14(%r13)
movq (%rbx), %r15
je 0x465c
decq 0x10(%r14)
movq %rbx, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2180
movq -0x60(%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 -0x60(%rbp), %r13
decq -0x50(%rbp)
movq %r13, %rdi
callq 0x2230
movq %r13, %rdi
callq 0x2180
movq -0x60(%rbp), %r13
movq %r15, %rbx
cmpq %r12, %r13
jne 0x4615
leaq -0x48(%rbp), %r15
movq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %r14
cmpq %r14, %rdi
je 0x46a3
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4693
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x46bc
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x46ac
leaq -0x78(%rbp), %rbx
movq (%rbx), %rdi
cmpq %rbx, %rdi
je 0x46d8
movq (%rdi), %r14
callq 0x2180
movq %r14, %rdi
cmpq %rbx, %r14
jne 0x46c8
movq -0x30(%rbp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r13
movq -0x110(%rbp), %rdi
cmpq %r15, %rdi
je 0x47cd
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x4701
jmp 0x47cd
jmp 0x47ca
movq %rax, %r13
movq -0xf8(%rbp), %rdi
cmpq %r15, %rdi
je 0x47cd
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r15, %rbx
jne 0x472e
jmp 0x47cd
jmp 0x47ca
movq %rax, %r13
movq -0x128(%rbp), %rdi
cmpq %r12, %rdi
je 0x480d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x475b
jmp 0x480d
jmp 0x4776
jmp 0x4776
jmp 0x4776
movq %rax, %r13
jmp 0x480d
movq %rax, %r13
jmp 0x482a
movq %rax, %r13
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x47cd
leaq -0x60(%rbp), %r14
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4796
jmp 0x47cd
movq %rax, %r13
movq -0x60(%rbp), %rdi
cmpq %r12, %rdi
je 0x47cd
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r12, %rbx
jne 0x47b4
jmp 0x47cd
jmp 0x47ca
jmp 0x47ca
movq %rax, %r13
movq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %r14
cmpq %r14, %rdi
je 0x47f0
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x47e0
movq -0x48(%rbp), %rdi
leaq -0x48(%rbp), %r14
cmpq %r14, %rdi
je 0x480d
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x47fd
movq -0x78(%rbp), %rdi
leaq -0x78(%rbp), %r14
cmpq %r14, %rdi
je 0x482a
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x481a
movq -0x30(%rbp), %r14
movq (%r14), %rdi
cmpq %r14, %rdi
je 0x4846
movq (%rdi), %rbx
callq 0x2180
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4836
movq %r13, %rdi
callq 0x2280