// File: 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
subq $0x240, %rsp            # imm = 0x240
movq %rsi, -0x1f0(%rbp)
movq %rdi, -0x1e8(%rbp)
movq %rdi, %rax
movq %rax, -0x1e0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl %r8d, -0x20(%rbp)
movb $0x0, -0x21(%rbp)
callq 0x8490
leaq -0x40(%rbp), %rdi
callq 0x6740
jmp 0x7bca
movl -0x1c(%rbp), %eax
movl -0x20(%rbp), %ecx
addl %ecx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, -0x50(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x35b0
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
leaq -0x78(%rbp), %rdi
callq 0x6b20
jmp 0x7bf9
movups -0x78(%rbp), %xmm0
movaps %xmm0, -0x40(%rbp)
movq -0x18(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x6c50
jmp 0x7c0f
leaq -0x40(%rbp), %rdi
callq 0x6800
movq %rax, -0x1f8(%rbp)
jmp 0x7c21
movq -0x1f0(%rbp), %rsi
movq -0x1f8(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x84b0
jmp 0x7c41
movq -0x80(%rbp), %rdx
leaq -0x98(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x7790
jmp 0x7c5a
leaq -0x68(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x8570
leaq -0x98(%rbp), %rdi
callq 0x3670
leaq -0xb0(%rbp), %rdi
callq 0x3670
leaq -0xc8(%rbp), %rdi
callq 0x35b0
leaq -0xe0(%rbp), %rdi
callq 0x35b0
movq -0x1e8(%rbp), %rdi
callq 0x85a0
movq %rax, -0xf0(%rbp)
movl $0x0, -0xf4(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3ac0
movq -0x1f0(%rbp), %rdi
movq %rax, -0x100(%rbp)
callq 0x3ac0
movq %rax, -0x108(%rbp)
movq -0x1f0(%rbp), %rdi
callq 0x3af0
movq %rax, -0x110(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x3bb0
testb $0x1, %al
jne 0x7d09
jmp 0x7ed8
leaq -0x100(%rbp), %rdi
callq 0x3be0
cmpl $0x0, (%rax)
jne 0x7dd4
leaq -0x108(%rbp), %rdi
callq 0x3be0
movq %rax, %rsi
leaq -0xc8(%rbp), %rdi
callq 0x3610
jmp 0x7d3b
leaq -0x100(%rbp), %rdi
callq 0x3be0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x8610
movq %rax, -0x200(%rbp)
jmp 0x7d5c
movq -0x1e8(%rbp), %rdi
movq -0x200(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x118(%rbp), %rsi
callq 0x85d0
jmp 0x7d7f
jmp 0x7eb9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x824f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8228
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3670
jmp 0x8228
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x8210
movq -0x80(%rbp), %rax
movq %rax, -0x210(%rbp)
leaq -0x100(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x3be0
movq -0x218(%rbp), %rdi
movq %rax, %rcx
movq -0x210(%rbp), %rax
movslq (%rcx), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0xe4(%rbp)
callq 0x3be0
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x8610
movq %rax, -0x208(%rbp)
jmp 0x7e29
movq -0x1e8(%rbp), %rdi
movq -0x208(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x120(%rbp), %rsi
callq 0x85d0
jmp 0x7e4c
cmpl $0x80000000, -0xe4(%rbp) # imm = 0x80000000
jne 0x7eb7
leaq -0x108(%rbp), %rdi
callq 0x3be0
movq %rax, %rsi
leaq -0xe0(%rbp), %rdi
callq 0x3610
jmp 0x7e75
cmpl $0x0, -0xf4(%rbp)
jne 0x7eb5
movq -0x1e8(%rbp), %rdi
movl $0x1, -0xf4(%rbp)
callq 0x8640
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x8670
jmp 0x7eb7
jmp 0x7eb9
jmp 0x7ebb
leaq -0x100(%rbp), %rdi
callq 0x3c00
leaq -0x108(%rbp), %rdi
callq 0x3c00
jmp 0x7cda
movl -0x1c(%rbp), %eax
cmpl -0x20(%rbp), %eax
jne 0x7ef3
movb $0x1, -0x21(%rbp)
movl $0x1, -0x12c(%rbp)
jmp 0x81e7
movl -0x50(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x8068
leaq -0xc8(%rbp), %rdi
callq 0x3c20
testb $0x1, %al
jne 0x8066
leaq -0x160(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x84b0
jmp 0x7f28
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
movl -0x50(%rbp), %r8d
decl %r8d
leaq -0x148(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x7b80
jmp 0x7f4b
leaq -0x160(%rbp), %rdi
callq 0x3670
movq -0x1e8(%rbp), %rdi
callq 0x85a0
movq %rax, -0x168(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x8690
xorb $-0x1, %al
testb $0x1, %al
jne 0x7f81
jmp 0x805a
leaq -0x178(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movq %rsi, -0x228(%rbp)
callq 0x8700
movq -0x1e8(%rbp), %rdi
movq -0x178(%rbp), %rsi
callq 0x86b0
movq -0x228(%rbp), %rsi
movq %rax, -0x170(%rbp)
movq -0x170(%rbp), %rax
movq %rax, -0x168(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x8700
leaq -0x148(%rbp), %rdi
callq 0x8790
movq -0x1e8(%rbp), %rdi
movq %rax, %rdx
movq -0x180(%rbp), %rsi
callq 0x8720
movq %rax, -0x220(%rbp)
jmp 0x8001
movq -0x220(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x87c0
jmp 0x7f6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x3670
jmp 0x8210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x87f0
jmp 0x8210
leaq -0x148(%rbp), %rdi
callq 0x87f0
jmp 0x8068
leaq -0xe0(%rbp), %rdi
callq 0x3c20
testb $0x1, %al
jne 0x81d9
leaq -0x1b8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x84b0
jmp 0x8091
movq -0x18(%rbp), %rdx
movl -0x50(%rbp), %ecx
incl %ecx
movl -0x20(%rbp), %r8d
leaq -0x1a0(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x7b80
jmp 0x80b3
leaq -0x1b8(%rbp), %rdi
callq 0x3670
leaq -0x1a0(%rbp), %rdi
callq 0x8690
xorb $-0x1, %al
testb $0x1, %al
jne 0x80d6
jmp 0x81cd
leaq -0x1a0(%rbp), %rdi
callq 0x8790
cmpl $0x0, 0x4(%rax)
je 0x81b0
leaq -0x1c8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movq %rsi, -0x238(%rbp)
callq 0x8700
movq -0x1e8(%rbp), %rdi
movq -0x1c8(%rbp), %rsi
callq 0x86b0
movq -0x238(%rbp), %rsi
movq %rax, -0x1c0(%rbp)
movq -0x1c0(%rbp), %rax
movq %rax, -0xf0(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x8700
leaq -0x1a0(%rbp), %rdi
callq 0x8790
movq -0x1e8(%rbp), %rdi
movq %rax, %rdx
movq -0x1d0(%rbp), %rsi
callq 0x8720
movq %rax, -0x230(%rbp)
jmp 0x816c
movq -0x230(%rbp), %rax
movq %rax, -0x1d8(%rbp)
jmp 0x81bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1b8(%rbp), %rdi
callq 0x3670
jmp 0x8210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1a0(%rbp), %rdi
callq 0x87f0
jmp 0x8210
leaq -0xf0(%rbp), %rdi
callq 0x8810
leaq -0x1a0(%rbp), %rdi
callq 0x87c0
jmp 0x80bf
leaq -0x1a0(%rbp), %rdi
callq 0x87f0
movb $0x1, -0x21(%rbp)
movl $0x1, -0x12c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x3670
leaq -0xc8(%rbp), %rdi
callq 0x3670
leaq -0x68(%rbp), %rdi
callq 0x3670
testb $0x1, -0x21(%rbp)
jne 0x823f
jmp 0x8233
leaq -0xe0(%rbp), %rdi
callq 0x3670
leaq -0xc8(%rbp), %rdi
callq 0x3670
leaq -0x68(%rbp), %rdi
callq 0x3670
jmp 0x824f
movq -0x1e8(%rbp), %rdi
callq 0x87f0
movq -0x1e0(%rbp), %rax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
movq -0x1e8(%rbp), %rdi
callq 0x87f0
movq -0x48(%rbp), %rdi
callq 0x2270
nopw %cs:(%rax,%rax)