// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rsi, %r15
leaq -0x10b0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3380
movq %r14, 0x20(%rbx)
leaq 0x11868(%rip), %rdi     # 0x192a0
movq %rbx, %rsi
callq 0x8118
movq %rax, %rbx
leaq -0x10b0(%rbp), %rdi
callq 0x34f8
leaq 0x11852(%rip), %rax     # 0x192a8
cmpq %rax, %rbx
je 0x7ace
movq 0x48(%rbx), %rsi
addq $0x10, %rsi
leaq -0x10b0(%rbp), %r14
movq %r14, %rdi
callq 0x7f48
movq 0x48(%rbx), %rsi
leaq 0x11853(%rip), %rbx     # 0x192d0
movq %rbx, %rdi
callq 0x7f9e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7fb6
movq 0x11841(%rip), %r14     # 0x192d8
leaq -0xb0(%rbp), %rbx
leaq -0x90(%rbp), %rdx
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x80bc
leaq 0x117e8(%rip), %rdi     # 0x192a0
leaq -0x58(%rbp), %rsi
callq 0x8118
movq %r14, 0x48(%rax)
leaq -0x58(%rbp), %rdi
jmp 0x7c0d
movq (%r15), %rdi
leaq 0x9795(%rip), %rsi      # 0x1126d
callq 0x3300
movq %rax, %r12
testq %rax, %rax
jne 0x7b12
movq 0x11504(%rip), %rdi     # 0x18ff0
leaq 0x977d(%rip), %rsi      # 0x11270
callq 0x3280
movq %rax, %rdi
movq %r15, %rsi
callq 0x3250
leaq 0x9778(%rip), %rsi      # 0x11282
movq %rax, %rdi
callq 0x3280
movq %r12, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x32e0
leaq -0xb0(%rbp), %rbx
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
andq $0x0, 0x10(%rax)
leaq -0x10b0(%rbp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x3160
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3150
leaq -0x10b0(%rbp), %r13
movq %r14, 0x1020(%r13)
andl $0x0, 0x1028(%r13)
pushq $0x1
popq %rsi
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
movq %r12, %rcx
callq 0x33c0
movq %r12, %rdi
callq 0x3290
leaq 0x1173c(%rip), %r15     # 0x192d0
movq %r15, %rdi
movq %r13, %rsi
callq 0x7fb6
leaq -0x90(%rbp), %rdx
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x80bc
movq 0x1171f(%rip), %r14     # 0x192d8
leaq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x80da
leaq -0x58(%rbp), %rsi
movq %r14, 0x28(%rsi)
leaq 0x116cb(%rip), %rdi     # 0x192a0
callq 0x830a
leaq -0x58(%rbp), %rdi
callq 0x34f8
cmpq $0x401, 0x116f2(%rip) # imm = 0x401
jb 0x7c09
movq (%r15), %r15
cmpl $0x1, 0x1038(%r15)
je 0x7bf0
addq $0x10, %r15
movq %r15, %rsi
callq 0x7c8a
leaq -0x80(%rbp), %rdi
callq 0x34f8
movq %rbx, %rdi
callq 0x34f8
movq %r14, %rax
addq $0x1088, %rsp           # imm = 0x1088
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7c3f
movq %rax, %r14
leaq -0x58(%rbp), %rdi
callq 0x34f8
jmp 0x7c42
movq %rax, %r14
leaq -0x80(%rbp), %rdi
callq 0x34f8
jmp 0x7c6e
jmp 0x7c6b
movq %rax, %r14
leaq -0x58(%rbp), %rdi
callq 0x34f8
jmp 0x7c62
jmp 0x7c5f
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
jmp 0x7c7d
movq %rax, %r14
movq %rbx, %rdi
jmp 0x7c7d
movq %rax, %r14
leaq -0x10b0(%rbp), %rdi
callq 0x34f8
movq %r14, %rdi
callq 0x3450