// File: akgupta007[P]DBMS-query-processing-Algotihms/buffer_manager.cpp
// Function: BufferManager::FindSlot()
int BufferManager::FindSlot() {
	// if free slot available
	if(!freeList.empty()) {
		int slot = freeList.front();
		freeList.pop_front();
		usedList.push_front(slot);
		return slot;
	}
	else {
		// since used list has MRU in front 
		// start search for a unpinned page from last 
		for(auto slot = usedList.rbegin();slot!=usedList.rend();slot++) {
			if(!buffers[*slot].pinned) {
				// page will be replaced
				// if dirty write to the file
				if(buffers[*slot].dirty) {
					int _res = WritePage(buffers[*slot].pageDescriptor,buffers[*slot].data);
					if(_res<0) continue;
					buffers[*slot].dirty=false;
				}
				//remove from hash table , and usedList
				hashTable.erase(buffers[*slot].pageDescriptor);
				// move the slot to MRU (front of used list)
				int slot_val = *slot;
				usedList.remove(slot_val);
				usedList.push_front(slot_val);
				return slot_val;
			}
		}
	}
	return -1; // no slot available
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x48(%rbp)
callq 0x3c20
testb $0x1, %al
jne 0x2993
movq -0x48(%rbp), %rdi
callq 0x3c40
movq -0x48(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x14(%rbp)
callq 0x3c70
movq -0x48(%rbp), %rdi
addq $0x18, %rdi
leaq -0x14(%rbp), %rsi
callq 0x3880
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b5d
movq -0x48(%rbp), %rsi
addq $0x18, %rsi
leaq -0x20(%rbp), %rdi
callq 0x3ca0
movq -0x48(%rbp), %rsi
addq $0x18, %rsi
leaq -0x28(%rbp), %rdi
callq 0x3d20
leaq -0x20(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3cf0
testb $0x1, %al
jne 0x29cb
jmp 0x2b54
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x50(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq %rax, %rcx
movq -0x50(%rbp), %rax
movslq (%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x1(%rax)
jne 0x2b3e
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x58(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq %rax, %rcx
movq -0x58(%rbp), %rax
movslq (%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
testb $0x1, (%rax)
je 0x2acb
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x68(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq -0x68(%rbp), %rcx
movq %rax, %rdx
movq -0x48(%rbp), %rax
movslq (%rdx), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x4(%rcx), %rcx
movq %rcx, -0x34(%rbp)
movq 0x30(%rax), %rax
movq %rax, -0x60(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movq -0x60(%rbp), %rax
movslq (%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdx
movq -0x34(%rbp), %rsi
callq 0x3130
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x2aa2
jmp 0x2b40
movq -0x48(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x70(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq %rax, %rcx
movq -0x70(%rbp), %rax
movslq (%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $0x0, (%rax)
movq -0x48(%rbp), %rax
movq %rax, %rcx
addq $0x40, %rcx
movq %rcx, -0x78(%rbp)
movq 0x30(%rax), %rax
movq %rax, -0x80(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdi
movslq (%rax), %rax
imulq $0x18, %rax, %rax
addq %rax, %rsi
addq $0x4, %rsi
callq 0x3b40
leaq -0x20(%rbp), %rdi
callq 0x3d70
movq -0x48(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x38(%rbp)
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
callq 0x37a0
movq -0x48(%rbp), %rdi
addq $0x18, %rdi
leaq -0x38(%rbp), %rsi
callq 0x3880
movl -0x38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b5d
jmp 0x2b40
leaq -0x40(%rbp), %rdi
leaq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x3da0
jmp 0x29a4
jmp 0x2b56
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)