// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1088, %rsp           # imm = 0x1088
movq %rdx, %rbx
movq %rsi, %r14
leaq -0x10a0(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq -0x10b0(%rbp), %r15
movq %r15, %rdi
callq 0x8766
movq %rbx, 0x20(%r15)
leaq 0x11ee2(%rip), %rdi     # 0x1b2b0
movq %r15, %rsi
callq 0x9c06
movq %rax, %r15
movq -0x10b0(%rbp), %rdi
cmpq %r12, %rdi
je 0x93ea
callq 0x3270
leaq 0x11ec7(%rip), %rax     # 0x1b2b8
cmpq %rax, %r15
je 0x9507
movq 0x48(%r15), %r14
leaq 0x10(%r14), %rsi
leaq -0x10b0(%rbp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
callq 0x3210
leaq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0x1010(%r14), %rsi
movq 0x1018(%r14), %rdx
addq %rsi, %rdx
callq 0x8766
movl 0x1038(%r14), %eax
movl %eax, 0x18(%r12)
movq 0x1030(%r14), %rax
movq %rax, 0x10(%r12)
movq 0x48(%r15), %rsi
leaq 0x11e7e(%rip), %r14     # 0x1b2e0
movq %r14, %rdi
callq 0x9cca
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9d06
leaq 0x11e64(%rip), %rsi     # 0x1b2e0
movq %rax, %rdi
callq 0x3060
incq 0x11e65(%rip)           # 0x1b2f0
movq 0x11e56(%rip), %rbx     # 0x1b2e8
leaq -0x48(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x8766
movq -0x90(%rbp), %rax
leaq -0x58(%rbp), %rsi
movq %rax, 0x20(%rsi)
leaq 0x11de6(%rip), %rdi     # 0x1b2b0
callq 0x9c06
movq %rbx, 0x48(%rax)
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x94e1
callq 0x3270
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x94f2
callq 0x3270
movq %rbx, %rax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rdi
leaq 0xad86(%rip), %rsi      # 0x14297
callq 0x3330
movq %rax, %r15
testq %rax, %rax
jne 0x955c
movq 0x11acb(%rip), %r12     # 0x1aff0
leaq 0xad6e(%rip), %rsi      # 0x1429a
movl $0x11, %edx
movq %r12, %rdi
callq 0x3310
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r12, %rdi
callq 0x3310
leaq 0xad5d(%rip), %rsi      # 0x142ac
movl $0x9, %edx
movq %rax, %rdi
callq 0x3310
xorl %r13d, %r13d
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32f0
leaq -0xb0(%rbp), %r12
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %r13, -0x8(%rax)
movb $0x0, (%rax)
movq %r13, 0x10(%rax)
movq %rax, %r13
leaq -0x10b0(%rbp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x3160
movq %r12, %rdi
movq %r14, %rsi
callq 0x3150
leaq -0x10b0(%rbp), %r14
movq %rbx, 0x1020(%r14)
movl $0x0, 0x1028(%r14)
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
movq %r15, %rcx
callq 0x33d0
movq %r15, %rdi
callq 0x3290
leaq 0x11cf9(%rip), %rdi     # 0x1b2e0
movq %r14, %rsi
callq 0x9d06
leaq 0x11cea(%rip), %r14     # 0x1b2e0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3060
incq 0x11ce8(%rip)           # 0x1b2f0
leaq -0x70(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %rdi
callq 0x8766
movq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x11cac(%rip), %rbx     # 0x1b2e8
leaq -0x48(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rdx
addq %rsi, %rdx
leaq -0x58(%rbp), %rdi
callq 0x8766
movq -0x60(%rbp), %rax
leaq -0x58(%rbp), %rsi
movq %rax, 0x20(%rsi)
movq %rbx, 0x28(%rsi)
leaq 0x11c40(%rip), %rdi     # 0x1b2b0
callq 0x9d90
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x9683
callq 0x3270
cmpq $0x401, 0x11c62(%rip) # imm = 0x401
jb 0x96a9
movq (%r14), %r14
cmpl $0x1, 0x1038(%r14)
je 0x9690
addq $0x10, %r14
movq %r14, %rsi
callq 0x9744
movq -0x80(%rbp), %rdi
cmpq %r15, %rdi
je 0x96b7
callq 0x3270
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x94ed
jmp 0x94f2
jmp 0x96e1
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x96e4
callq 0x3270
jmp 0x96e4
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r15, %rdi
je 0x971a
callq 0x3270
jmp 0x971a
jmp 0x9717
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x970e
callq 0x3270
jmp 0x970e
jmp 0x970b
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
jmp 0x9732
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x9737
jmp 0x973c
movq %rax, %rbx
movq -0x10b0(%rbp), %rdi
cmpq %r12, %rdi
je 0x973c
callq 0x3270
movq %rbx, %rdi
callq 0x3480