// File: BU-DiSC[P]BF-Shared-Hashing/db.cpp
// Function: db::GetLevel(int, BFHash&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, bool*)
inline string db::GetLevel( int i, BFHash & bfHash, string & key, bool * result )
{
	// Binary search for fense pointer
	//auto bs_start = high_resolution_clock::now();
	int bf_no = binary_search(key, fence_pointers[i]);
	//auto bs_end = high_resolution_clock::now();
	//bs_duration += duration_cast<microseconds>(bs_end - bs_start);	

 		//auto other_start = high_resolution_clock::now();

	// no matching sst
	if(bf_no < 0){
		*result = false;
 		//auto other_end = high_resolution_clock::now();
		//other_duration += duration_cast<microseconds>(other_end - other_start);
		return "";
	}
	//auto hash_start = high_resolution_clock::now();
	vector<uint64_t>* hash_digests = bfHash.getLevelwiseHashDigest(i);
	//auto hash_end = high_resolution_clock::now();
	//hash_duration += duration_cast<microseconds>(hash_end - hash_start);	


	bool bf_result = QueryFilter( i, bf_no, hash_digests, bf_prime[i][bf_no]);
		
	//cout << "QueryFilter result : " << i << " " << bf_no << " " << bf_result << endl;

	if( bf_result==false ){
		total_n++;
		//n_vec[i]++;
		*result = false;
 		//auto other_end = high_resolution_clock::now();
		//other_duration += duration_cast<microseconds>(other_end - other_start);
		return "";
	}

	int index_pos = GetFromIndex( i, bf_no, key );

	bool data_result = false;
	string data = GetFromData( i, bf_no, index_pos, key, &data_result );

	// false positive
	if (data_result == false){
		total_fp++;
		//fp_vec[i]++;
		total_n++;
		//n_vec[i]++;
	}else{

		total_p++;
	}
	

	*result = data_result;
 		//auto other_end = high_resolution_clock::now();
		//other_duration += duration_cast<microseconds>(other_end - other_start);
	return data;
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x50(%rsp)
movq %rdi, %rax
movq %rax, 0x58(%rsp)
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movl %edx, 0x124(%rsp)
movq %rcx, 0x118(%rsp)
movq %r8, 0x110(%rsp)
movq %r9, 0x108(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x110(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x6260
movq 0x60(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0x124(%rsp), %rsi
callq 0x11460
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %rax, %rdx
callq 0xe520
movl %eax, 0x74(%rsp)
jmp 0x11f0b
leaq 0xe0(%rsp), %rdi
callq 0x6340
movl 0x74(%rsp), %eax
movl %eax, 0x104(%rsp)
cmpl $0x0, 0x104(%rsp)
jge 0x11fc4
movq 0x108(%rsp), %rax
movb $0x0, (%rax)
leaq 0xd3(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x6860
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rdx
leaq 0x5246b(%rip), %rsi     # 0x643ca
callq 0x66f0
jmp 0x11f66
leaq 0xd3(%rsp), %rdi
callq 0x6570
jmp 0x12270
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x6340
jmp 0x1227d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xd3(%rsp), %rdi
callq 0x6570
jmp 0x1227d
movq 0x118(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x1e3e0
movq 0x60(%rsp), %rdi
movq %rax, 0xc8(%rsp)
movl 0x124(%rsp), %esi
movl 0x104(%rsp), %edx
movq 0xc8(%rsp), %rcx
movq 0x158(%rdi), %rax
movslq 0x124(%rsp), %r8
movq (%rax,%r8,8), %rax
movslq 0x104(%rsp), %r8
movq (%rax,%r8,8), %r8
callq 0x15060
andb $0x1, %al
movb %al, 0xc7(%rsp)
movb 0xc7(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x120c0
movq 0x60(%rsp), %rax
movq 0x198(%rax), %rcx
incq %rcx
movq %rcx, 0x198(%rax)
movq 0x108(%rsp), %rax
movb $0x0, (%rax)
leaq 0xc6(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x6860
movq 0x50(%rsp), %rdi
movq 0x40(%rsp), %rdx
leaq 0x52349(%rip), %rsi     # 0x643ca
callq 0x66f0
jmp 0x12088
leaq 0xc6(%rsp), %rdi
callq 0x6570
jmp 0x12270
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xc6(%rsp), %rdi
callq 0x6570
jmp 0x1227d
movl 0x124(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x110(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x6260
movq 0x60(%rsp), %rdi
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movq 0x30(%rsp), %rcx
callq 0x151f0
movl %eax, 0x3c(%rsp)
jmp 0x1210d
leaq 0xa0(%rsp), %rdi
callq 0x6340
movl 0x3c(%rsp), %eax
movl %eax, 0xc0(%rsp)
movb $0x0, 0x9f(%rsp)
movb $0x0, 0x9e(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x104(%rsp), %eax
movl %eax, 0x18(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x110(%rsp), %rsi
leaq 0x78(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6260
movq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rsi
movl 0x14(%rsp), %edx
movl 0x18(%rsp), %ecx
movl 0x1c(%rsp), %r8d
movq 0x20(%rsp), %r9
movq %rsp, %rax
leaq 0x9f(%rsp), %r10
movq %r10, (%rax)
callq 0x152c0
jmp 0x1219e
leaq 0x78(%rsp), %rdi
callq 0x6340
movb 0x9f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x12227
movq 0x60(%rsp), %rax
movq 0x1a8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x1a8(%rax)
movq 0x198(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x198(%rax)
jmp 0x1223e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x6340
jmp 0x1227d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x6340
jmp 0x1227d
movq 0x60(%rsp), %rax
movq 0x1a0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x1a0(%rax)
movb 0x9f(%rsp), %cl
movq 0x108(%rsp), %rax
andb $0x1, %cl
movb %cl, (%rax)
movb $0x1, 0x9e(%rsp)
testb $0x1, 0x9e(%rsp)
jne 0x1226e
movq 0x50(%rsp), %rdi
callq 0x6340
jmp 0x12270
movq 0x58(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xd8(%rsp), %rdi
callq 0x6850
nopw (%rax,%rax)