// File: zimpha[P]MiniSQL/bufferManager.cpp
// Function: BFM::BufferManagerWrite(Block const&)
void BFM::BufferManagerWrite(const Block &b)
{
    FILE *fp = fopen(b.fileName.c_str(), "rb");
    FILE *outFile;
    if (fp==NULL){
        outFile = fopen(b.fileName.c_str(), "wb");
    }
    else{
        fclose(fp);
        outFile = fopen(b.fileName.c_str(), "rb+");
    }
    if(outFile==NULL){
        cerr<<"ERROR: Open file "<<b.fileName<<" failed.\n";
        return;
    }
    fseek(outFile, b.offset, SEEK_SET);
    fwrite(&b.data, 1, BLOCKSIZE, outFile);
    fclose(outFile);

    tag T = make_pair(b.fileName, b.offset);
    tableIter tmp = table.find(T);
    if (tmp!=table.end()){ //in the buffer, which means not new written
        bufferIter victim = tmp->second;
        table.erase(table.find(T)); //remove from table
        buffer.erase(victim); //remove from buffer
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
addq $0x1000, %rdi           # imm = 0x1000
callq 0x3120
movq %rax, %rdi
leaq 0x17de4(%rip), %rsi     # 0x27251
callq 0x3370
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xf4a2
movq -0x10(%rbp), %rdi
addq $0x1000, %rdi           # imm = 0x1000
callq 0x3120
movq %rax, %rdi
leaq 0x17dd9(%rip), %rsi     # 0x27270
callq 0x3370
movq %rax, -0x20(%rbp)
jmp 0xf4ce
movq -0x18(%rbp), %rdi
callq 0x32d0
movq -0x10(%rbp), %rdi
addq $0x1000, %rdi           # imm = 0x1000
callq 0x3120
movq %rax, %rdi
leaq 0x17dae(%rip), %rsi     # 0x27273
callq 0x3370
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xf50f
movq 0x29b14(%rip), %rdi     # 0x38ff0
leaq 0x17d71(%rip), %rsi     # 0x27254
callq 0x32c0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x1000, %rsi           # imm = 0x1000
callq 0x3280
movq %rax, %rdi
leaq 0x17d61(%rip), %rsi     # 0x27266
callq 0x32c0
jmp 0xf65c
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x1020(%rax), %rsi
xorl %edx, %edx
callq 0x3340
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x1, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x3580
movq -0x20(%rbp), %rdi
callq 0x32d0
movq -0x10(%rbp), %rdx
movq %rdx, %rsi
addq $0x1000, %rsi           # imm = 0x1000
addq $0x1020, %rdx           # imm = 0x1020
leaq -0x48(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0xfe30
movq -0xa0(%rbp), %rsi
leaq 0x29d88(%rip), %rdi     # 0x39300
callq 0xf970
movq %rax, -0x98(%rbp)
jmp 0xf586
movq -0x98(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0x29d68(%rip), %rdi     # 0x39300
callq 0xfa30
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xfa00
testb $0x1, %al
jne 0xf5b7
jmp 0xf653
leaq -0x50(%rbp), %rdi
callq 0xfa60
movq 0x28(%rax), %rax
movq %rax, -0x70(%rbp)
leaq 0x29d31(%rip), %rdi     # 0x39300
leaq -0x48(%rbp), %rsi
callq 0xf970
movq %rax, -0xa8(%rbp)
jmp 0xf5e1
movq -0xa8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rsi
leaq 0x29d09(%rip), %rdi     # 0x39300
callq 0xfe70
movq %rax, -0xb0(%rbp)
jmp 0xf605
movq -0xb0(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xfb60
movq -0x88(%rbp), %rsi
leaq 0x29d02(%rip), %rdi     # 0x39330
callq 0xfb10
movq %rax, -0x90(%rbp)
jmp 0xf653
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xf9e0
jmp 0xf665
leaq -0x48(%rbp), %rdi
callq 0xf9e0
addq $0xb0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x34f0
nop