// File: zimpha[P]MiniSQL/bufferManager.cpp
// Function: BFM::BufferManagerRead(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, long)
bufferIter BFM::BufferManagerRead(const string &fileName, long offset)
{
	tableIter it = table.find(make_pair(fileName,offset));
	if (it!=table.end()){ //already in the buffer
        Block tmp = *(it->second);
        buffer.erase(it->second); //move to the back of the buffer list
        buffer.push_back(tmp);

        bufferIter B = buffer.end();
        B--;
        tag T = make_pair(tmp.fileName, tmp.offset);
        table.find(T)->second = B; //update table
		return B;
	}
	else{ //not in the buffer

        FILE *inFile = fopen(fileName.c_str(), "rb");
        if(inFile==NULL){
            cerr<<"ERROR: Open file "<<fileName<<" failed.\n";
        }
        fseek(inFile, offset, SEEK_SET);
        Block tmp;
        tmp.fileName = fileName;
        tmp.offset = offset;
        tmp.status = 0;
        fread(&tmp.data, 1, BLOCKSIZE, inFile);
        fclose(inFile);

        buffer.push_back(tmp); //push into buffer

        tag T = make_pair(tmp.fileName, tmp.offset);
        bufferIter B = buffer.end();
        B--;
        table.insert(make_pair(T,B)); //update table

        if (buffer.size()>BUFFERSIZE){ //we should use LRU replace
            bufferIter victim = buffer.begin();
            while (victim->status==1) victim++; //find the least used unpin block
            BufferManagerWrite(*victim); //writeback
        }

        return B;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x21f0, %rsp           # imm = 0x21F0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x21b0(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x50(%rbp), %rdi
movq %rdi, -0x21a8(%rbp)
leaq -0x20(%rbp), %rdx
callq 0xf9a0
movq -0x21a8(%rbp), %rsi
leaq 0x2a2f8(%rip), %rdi     # 0x39300
callq 0xf970
movq %rax, -0x21a0(%rbp)
jmp 0xf016
movq -0x21a0(%rbp), %rax
movq %rax, -0x28(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xf9e0
leaq 0x2a2cf(%rip), %rdi     # 0x39300
callq 0xfa30
movq %rax, -0x68(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xfa00
testb $0x1, %al
jne 0xf050
jmp 0xf1be
leaq -0x28(%rbp), %rdi
movq %rdi, -0x21c8(%rbp)
callq 0xfa60
movq %rax, %rdi
addq $0x28, %rdi
callq 0xfa80
movq %rax, %rsi
leaq -0x1098(%rbp), %rdi
movq %rdi, -0x21b8(%rbp)
callq 0xfaa0
movq -0x21c8(%rbp), %rdi
callq 0xfa60
movq %rax, %rsi
addq $0x28, %rsi
leaq -0x10a0(%rbp), %rdi
callq 0xfb60
movq -0x10a0(%rbp), %rsi
leaq 0x2a281(%rip), %rdi     # 0x39330
movq %rdi, -0x21c0(%rbp)
callq 0xfb10
movq -0x21c0(%rbp), %rdi
movq -0x21b8(%rbp), %rsi
movq %rax, -0x10a8(%rbp)
callq 0xfb80
jmp 0xf0d7
leaq 0x2a252(%rip), %rdi     # 0x39330
callq 0xfbc0
movq %rax, -0x8(%rbp)
leaq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xfbf0
movq %rax, -0x10b0(%rbp)
leaq -0x98(%rbp), %rsi
leaq -0x78(%rbp), %rdx
leaq -0x10d8(%rbp), %rdi
callq 0xfc20
jmp 0xf112
leaq 0x2a1e7(%rip), %rdi     # 0x39300
leaq -0x10d8(%rbp), %rsi
callq 0xf970
movq %rax, -0x21d0(%rbp)
jmp 0xf12e
movq -0x21d0(%rbp), %rax
movq %rax, -0x10e0(%rbp)
leaq -0x10e0(%rbp), %rdi
callq 0xfa60
movq -0x8(%rbp), %rcx
movq %rcx, 0x28(%rax)
leaq -0x10d8(%rbp), %rdi
callq 0xf9e0
leaq -0x1098(%rbp), %rdi
callq 0xfc60
jmp 0xf425
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0xf9e0
jmp 0xf432
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xf1ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x10d8(%rbp), %rdi
callq 0xf9e0
leaq -0x1098(%rbp), %rdi
callq 0xfc60
jmp 0xf432
movq -0x18(%rbp), %rdi
callq 0x3120
movq %rax, %rdi
leaq 0x18080(%rip), %rsi     # 0x27251
callq 0x3370
movq %rax, -0x10e8(%rbp)
cmpq $0x0, -0x10e8(%rbp)
jne 0xf215
movq 0x29e02(%rip), %rdi     # 0x38ff0
leaq 0x1805f(%rip), %rsi     # 0x27254
callq 0x32c0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x3280
movq %rax, %rdi
leaq 0x18056(%rip), %rsi     # 0x27266
callq 0x32c0
movq -0x10e8(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x3340
leaq -0x2118(%rbp), %rdi
callq 0xfc90
movq -0x18(%rbp), %rsi
leaq -0x1118(%rbp), %rdi
callq 0x3050
jmp 0xf245
movq -0x20(%rbp), %rax
movq %rax, -0x10f8(%rbp)
movl $0x0, -0x10f0(%rbp)
movq -0x10e8(%rbp), %rcx
leaq -0x2118(%rbp), %rdi
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3450
jmp 0xf279
movq -0x10e8(%rbp), %rdi
callq 0x32d0
jmp 0xf287
leaq 0x2a0a2(%rip), %rdi     # 0x39330
leaq -0x2118(%rbp), %rsi
callq 0xfb80
jmp 0xf29c
leaq -0x1118(%rbp), %rsi
leaq -0x10f8(%rbp), %rdx
leaq -0x2140(%rbp), %rdi
callq 0xfc20
jmp 0xf2b8
leaq 0x2a071(%rip), %rdi     # 0x39330
callq 0xfbc0
movq %rax, -0x8(%rbp)
leaq -0x8(%rbp), %rdi
movq %rdi, -0x21d8(%rbp)
xorl %esi, %esi
callq 0xfbf0
movq -0x21d8(%rbp), %rdx
movq %rax, -0x2148(%rbp)
leaq -0x2178(%rbp), %rdi
leaq -0x2140(%rbp), %rsi
callq 0xfd20
jmp 0xf2fd
leaq 0x29ffc(%rip), %rdi     # 0x39300
leaq -0x2178(%rbp), %rsi
callq 0xfce0
movb %dl, -0x21e1(%rbp)
movq %rax, -0x21e0(%rbp)
jmp 0xf31f
movb -0x21e1(%rbp), %al
movq -0x21e0(%rbp), %rcx
movq %rcx, -0x2188(%rbp)
movb %al, -0x2180(%rbp)
leaq -0x2178(%rbp), %rdi
callq 0xfd60
leaq 0x29fe4(%rip), %rdi     # 0x39330
callq 0xfd80
cmpq $0x400, %rax            # imm = 0x400
jbe 0xf3f1
leaq 0x29fcc(%rip), %rdi     # 0x39330
callq 0xfdb0
movq %rax, -0x2190(%rbp)
leaq -0x2190(%rbp), %rdi
callq 0xfde0
cmpl $0x1, 0x1028(%rax)
jne 0xf3d2
leaq -0x2190(%rbp), %rdi
xorl %esi, %esi
callq 0xfe00
movq %rax, -0x2198(%rbp)
jmp 0xf370
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xf417
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xf40b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x2178(%rbp), %rdi
callq 0xfd60
jmp 0xf40b
leaq -0x2190(%rbp), %rdi
callq 0xfa80
movq -0x21b0(%rbp), %rdi
movq %rax, %rsi
callq 0xf440
jmp 0xf3ef
jmp 0xf3f1
leaq -0x2140(%rbp), %rdi
callq 0xf9e0
leaq -0x2118(%rbp), %rdi
callq 0xfc60
jmp 0xf425
leaq -0x2140(%rbp), %rdi
callq 0xf9e0
leaq -0x2118(%rbp), %rdi
callq 0xfc60
jmp 0xf432
movq -0x8(%rbp), %rax
addq $0x21f0, %rsp           # imm = 0x21F0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x34f0
nopl (%rax,%rax)