// File: /workspace/github_cpp_makefile_star2_O3/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 0x88b4
movq %rbx, 0x20(%r15)
leaq 0x11d8e(%rip), %rdi     # 0x1b2b0
movq %r15, %rsi
callq 0x9d5e
movq %rax, %r15
movq -0x10b0(%rbp), %rdi
cmpq %r12, %rdi
je 0x953e
callq 0x3270
leaq 0x11d73(%rip), %rax     # 0x1b2b8
cmpq %rax, %r15
je 0x965b
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 0x88b4
movl 0x1038(%r14), %eax
movl %eax, 0x18(%r12)
movq 0x1030(%r14), %rax
movq %rax, 0x10(%r12)
movq 0x48(%r15), %rsi
leaq 0x11d2a(%rip), %r14     # 0x1b2e0
movq %r14, %rdi
callq 0x9e20
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9e5c
leaq 0x11d10(%rip), %rsi     # 0x1b2e0
movq %rax, %rdi
callq 0x3060
incq 0x11d11(%rip)           # 0x1b2f0
movq 0x11d02(%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 0x88b4
movq -0x90(%rbp), %rax
leaq -0x58(%rbp), %rsi
movq %rax, 0x20(%rsi)
leaq 0x11c92(%rip), %rdi     # 0x1b2b0
callq 0x9d5e
movq %rbx, 0x48(%rax)
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x9635
callq 0x3270
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x9646
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 0xac32(%rip), %rsi      # 0x14297
callq 0x3330
movq %rax, %r15
testq %rax, %rax
jne 0x96b0
movq 0x11977(%rip), %r12     # 0x1aff0
leaq 0xac1a(%rip), %rsi      # 0x1429a
movl $0x11, %edx
movq %r12, %rdi
callq 0x3310
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r12, %rdi
callq 0x3310
leaq 0xac09(%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 0x11ba5(%rip), %rdi     # 0x1b2e0
movq %r14, %rsi
callq 0x9e5c
leaq 0x11b96(%rip), %r14     # 0x1b2e0
movq %rax, %rdi
movq %r14, %rsi
callq 0x3060
incq 0x11b94(%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 0x88b4
movq -0x90(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x11b58(%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 0x88b4
movq -0x60(%rbp), %rax
leaq -0x58(%rbp), %rsi
movq %rax, 0x20(%rsi)
movq %rbx, 0x28(%rsi)
leaq 0x11aec(%rip), %rdi     # 0x1b2b0
callq 0x9ee6
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x97d7
callq 0x3270
cmpq $0x401, 0x11b0e(%rip) # imm = 0x401
jb 0x97fd
movq (%r14), %r14
cmpl $0x1, 0x1038(%r14)
je 0x97e4
addq $0x10, %r14
movq %r14, %rsi
callq 0x9898
movq -0x80(%rbp), %rdi
cmpq %r15, %rdi
je 0x980b
callq 0x3270
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x9641
jmp 0x9646
jmp 0x9835
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x9838
callq 0x3270
jmp 0x9838
movq %rax, %rbx
movq -0x80(%rbp), %rdi
cmpq %r15, %rdi
je 0x986e
callq 0x3270
jmp 0x986e
jmp 0x986b
movq %rax, %rbx
movq -0x58(%rbp), %rdi
cmpq %r14, %rdi
je 0x9862
callq 0x3270
jmp 0x9862
jmp 0x985f
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
jmp 0x9886
movq %rax, %rbx
movq -0xb0(%rbp), %rdi
cmpq %r13, %rdi
jne 0x988b
jmp 0x9890
movq %rax, %rbx
movq -0x10b0(%rbp), %rdi
cmpq %r12, %rdi
je 0x9890
callq 0x3270
movq %rbx, %rdi
callq 0x3480