// File: akgupta007[P]DBMS-query-processing-Algotihms/buffer_manager.cpp
// Function: BufferManager::GetPage(PageDescriptor)
char* BufferManager::GetPage(PageDescriptor pd) {
	auto slot = hashTable.find(pd);
	if(slot!=hashTable.end()) {
		//already in buffer 
		int slotNo = slot->second;
		// no need to read the page
		// to establish replacement policy, make page MRU 
		usedList.remove(slotNo); 
		usedList.push_front(slotNo); // put at front
		return buffers[slotNo].data;
	}
	else {
		// page not in buffers
		// find a suitable slot to load it
		int slotNo = FindSlot();
		if(slotNo==-1) throw NoBufferSpaceException (); //error no free slot could be obtained 
		// read data to the slot 
		bool _read_res = ReadPage(pd,buffers[slotNo].data);
		if(!_read_res) // read failed 
			throw BufferManagerException("BufferManagerException : Read request failed");
		// insert into hashTable the corresponding slot
		hashTable.insert(make_pair(pd,slotNo));
		// initialize the rest of Frame elements
		InitializeBuffer(pd,slotNo);
		return buffers[slotNo].data;
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
movq %rdi, -0x88(%rbp)
addq $0x40, %rdi
leaq -0x10(%rbp), %rsi
callq 0x36e0
movq -0x88(%rbp), %rdi
movq %rax, -0x20(%rbp)
addq $0x40, %rdi
callq 0x3740
movq %rax, -0x28(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3710
testb $0x1, %al
jne 0x2756
jmp 0x27b0
leaq -0x20(%rbp), %rdi
callq 0x3770
movq -0x88(%rbp), %rdi
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
addq $0x18, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x37a0
movq -0x88(%rbp), %rdi
addq $0x18, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x3880
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x2c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x2921
movq -0x88(%rbp), %rdi
callq 0x2940
movl %eax, -0x30(%rbp)
cmpl $-0x1, -0x30(%rbp)
jne 0x280b
movl $0x8, %edi
callq 0x20e0
movq %rax, %rdi
movq %rdi, -0x90(%rbp)
xorl %esi, %esi
movl $0x8, %edx
callq 0x2100
movq -0x90(%rbp), %rdi
callq 0x38c0
movq -0x90(%rbp), %rdi
leaq 0xd469(%rip), %rsi      # 0xfc68
leaq 0x10fa(%rip), %rdx      # 0x3900
callq 0x2250
movq -0x88(%rbp), %rdi
movq -0x10(%rbp), %rax
movq %rax, -0x3c(%rbp)
movq 0x30(%rdi), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdx
movq -0x3c(%rbp), %rsi
callq 0x2b70
andb $0x1, %al
movb %al, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
jne 0x289d
movl $0x10, %edi
callq 0x20e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x77c2(%rip), %rsi      # 0xa021
callq 0x3920
jmp 0x2866
movq -0x98(%rbp), %rdi
leaq 0xd40c(%rip), %rsi      # 0xfc80
leaq 0x10f5(%rip), %rdx      # 0x3970
callq 0x2250
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0x2130
jmp 0x292e
movq -0x88(%rbp), %rax
addq $0x40, %rax
movq %rax, -0xa0(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x39d0
movq -0xa0(%rbp), %rdi
movl %edx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x3990
movq -0x88(%rbp), %rdi
movq %rax, -0x78(%rbp)
movb %dl, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movl -0x30(%rbp), %edx
movq -0x80(%rbp), %rsi
callq 0x2bf0
movq -0x88(%rbp), %rax
movq 0x30(%rax), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2270
nopw (%rax,%rax)