// File: rocket323[P]fishdb/./pager.cpp
// Function: fishdb::Pager::FlushPage(std::shared_ptr<fishdb::MemPage>)
void Pager::FlushPage(std::shared_ptr<MemPage> mp)
{
    char buf[PG_SIZE * 100];
    assert(mp->header.type == TREE_PAGE);
    int64_t page_no = mp->header.page_no;
    int size = 0;
    mp->Serialize(buf, size);
    int data_size = size - PH_SIZE;
    int page_cnt = data_size / PAGE_CAPA + (data_size % PAGE_CAPA > 0);

    std::vector<std::shared_ptr<MemPage>> pages;
    mp->header.page_cnt = page_cnt;
    pages.push_back(mp);
    auto p = ReadPage(page_no);
    while (p && p->header.of_page_no > 0)
    {
        p = ReadPage(p->header.of_page_no);
        pages.push_back(p);
    }
    int diff = page_cnt - (int)pages.size();
    for (int i = 0; i < diff; ++i)
        pages.push_back(NewPage(OF_PAGE));

    // FIXME page leak
    assert((int)pages.size() >= page_cnt);
    for (int i = 0; i < page_cnt; ++i)
    {
        auto &p = pages[i];
        int64_t base = PH_SIZE + i * PAGE_CAPA;
        p->Clear();
        p->Feed(buf + base, PAGE_CAPA);
        if (i < page_cnt - 1)
        {
            p->header.of_page_no = pages[i + 1]->header.page_no;
        }
        else
            p->header.of_page_no = -1;
        WritePage(p);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc900, %rsp           # imm = 0xC900
movq %rsi, -0xc8c8(%rbp)
movq %rdi, %rax
movq -0xc8c8(%rbp), %rdi
movq %rdi, -0xc8c0(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8b8(%rbp)
callq 0x4190
movsbl 0x8(%rax), %eax
cmpl $0x2, %eax
jne 0xebb6
jmp 0xebd5
leaq 0x38a1(%rip), %rdi      # 0x1245e
leaq 0x37bf(%rip), %rsi      # 0x12383
movl $0x86, %edx
leaq 0x38ab(%rip), %rcx      # 0x1247b
callq 0x2150
movq -0xc8c0(%rbp), %rdi
callq 0x4190
movq -0xc8c0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0xc818(%rbp)
movl $0x0, -0xc81c(%rbp)
callq 0x4190
movq %rax, %rdi
leaq -0xc810(%rbp), %rsi
leaq -0xc81c(%rbp), %rdx
callq 0x114a0
movl -0xc81c(%rbp), %eax
addl $-0x28, %eax
movl %eax, -0xc820(%rbp)
movslq -0xc820(%rbp), %rax
movl %eax, %ecx
imulq $0x22b63cbf, %rax, %rax # imm = 0x22B63CBF
movq %rax, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rax
addl %edx, %eax
imull $0x1d8, %eax, %edx      # imm = 0x1D8
subl %edx, %ecx
testl %ecx, %ecx
setg %cl
movzbl %cl, %ecx
addl %ecx, %eax
movl %eax, -0xc824(%rbp)
leaq -0xc840(%rbp), %rdi
movq %rdi, -0xc8d0(%rbp)
callq 0x40d0
movq -0xc8c0(%rbp), %rdi
movw -0xc824(%rbp), %ax
movw %ax, -0xc8d2(%rbp)
callq 0x4190
movw -0xc8d2(%rbp), %cx
movq -0xc8d0(%rbp), %rdi
movq -0xc8c0(%rbp), %rsi
movw %cx, 0x24(%rax)
callq 0x4120
jmp 0xeca8
movq -0xc8b8(%rbp), %rsi
movq -0xc818(%rbp), %rdx
leaq -0xc860(%rbp), %rdi
callq 0xe870
jmp 0xecc4
jmp 0xecc6
leaq -0xc860(%rbp), %rdi
callq 0x50f0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0xc8d3(%rbp)
jne 0xece3
jmp 0xecfd
leaq -0xc860(%rbp), %rdi
callq 0x4190
cmpq $0x0, 0x18(%rax)
setg %al
movb %al, -0xc8d3(%rbp)
movb -0xc8d3(%rbp), %al
testb $0x1, %al
jne 0xed0c
jmp 0xeda6
leaq -0xc860(%rbp), %rdi
callq 0x4190
movq -0xc8b8(%rbp), %rsi
movq 0x18(%rax), %rdx
leaq -0xc870(%rbp), %rdi
callq 0xe870
jmp 0xed31
leaq -0xc860(%rbp), %rdi
movq %rdi, -0xc8e0(%rbp)
leaq -0xc870(%rbp), %rsi
movq %rsi, -0xc8e8(%rbp)
callq 0x41d0
movq -0xc8e8(%rbp), %rdi
callq 0x27e0
movq -0xc8e0(%rbp), %rsi
leaq -0xc840(%rbp), %rdi
callq 0x4120
jmp 0xed73
jmp 0xecc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc848(%rbp)
movl %eax, -0xc84c(%rbp)
jmp 0xf01f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc848(%rbp)
movl %eax, -0xc84c(%rbp)
jmp 0xf013
movl -0xc824(%rbp), %eax
movl %eax, -0xc8ec(%rbp)
leaq -0xc840(%rbp), %rdi
callq 0x4240
movq %rax, %rcx
movl -0xc8ec(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xc874(%rbp)
movl $0x0, -0xc878(%rbp)
movl -0xc878(%rbp), %eax
cmpl -0xc874(%rbp), %eax
jge 0xee56
movq -0xc8b8(%rbp), %rsi
leaq -0xc888(%rbp), %rdi
movl $0x3, %edx
callq 0xe300
jmp 0xee01
leaq -0xc840(%rbp), %rdi
leaq -0xc888(%rbp), %rsi
callq 0xf440
jmp 0xee16
leaq -0xc888(%rbp), %rdi
callq 0x27e0
movl -0xc878(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc878(%rbp)
jmp 0xedd9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc848(%rbp)
movl %eax, -0xc84c(%rbp)
leaq -0xc888(%rbp), %rdi
callq 0x27e0
jmp 0xf013
leaq -0xc840(%rbp), %rdi
callq 0x4240
cmpl -0xc824(%rbp), %eax
jl 0xee6c
jmp 0xee8b
leaq 0x3640(%rip), %rdi      # 0x124b3
leaq 0x3509(%rip), %rsi      # 0x12383
movl $0x9b, %edx
leaq 0x35f5(%rip), %rcx      # 0x1247b
callq 0x2150
movl $0x0, -0xc88c(%rbp)
movl -0xc88c(%rbp), %eax
cmpl -0xc824(%rbp), %eax
jge 0xeff2
movslq -0xc88c(%rbp), %rsi
leaq -0xc840(%rbp), %rdi
callq 0xf470
movq %rax, -0xc898(%rbp)
movl -0xc88c(%rbp), %eax
imull $0x1d8, %eax, %eax      # imm = 0x1D8
addl $0x28, %eax
cltq
movq %rax, -0xc8a0(%rbp)
movq -0xc898(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
callq 0x11450
jmp 0xeeef
movq -0xc898(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
movq -0xc8a0(%rbp), %rax
leaq -0xc810(%rbp,%rax), %rsi
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x11470
jmp 0xef19
movl -0xc88c(%rbp), %eax
movl -0xc824(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xef6f
movl -0xc88c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
leaq -0xc840(%rbp), %rdi
callq 0xf470
movq %rax, %rdi
callq 0x4190
movq (%rax), %rax
movq %rax, -0xc8f8(%rbp)
movq -0xc898(%rbp), %rdi
callq 0x4190
movq -0xc8f8(%rbp), %rcx
movq %rcx, 0x18(%rax)
jmp 0xef83
movq -0xc898(%rbp), %rdi
callq 0x4190
movq $-0x1, 0x18(%rax)
movq -0xc898(%rbp), %rsi
leaq -0xc8b0(%rbp), %rdi
movq %rdi, -0xc900(%rbp)
callq 0x40f0
movq -0xc8b8(%rbp), %rdi
movq -0xc900(%rbp), %rsi
callq 0xf040
jmp 0xefb2
leaq -0xc8b0(%rbp), %rdi
callq 0x27e0
movl -0xc88c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc88c(%rbp)
jmp 0xee95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc848(%rbp)
movl %eax, -0xc84c(%rbp)
leaq -0xc8b0(%rbp), %rdi
callq 0x27e0
jmp 0xf013
leaq -0xc860(%rbp), %rdi
callq 0x27e0
leaq -0xc840(%rbp), %rdi
callq 0x2b00
addq $0xc900, %rsp           # imm = 0xC900
popq %rbp
retq
leaq -0xc860(%rbp), %rdi
callq 0x27e0
leaq -0xc840(%rbp), %rdi
callq 0x2b00
movq -0xc848(%rbp), %rdi
callq 0x2370
nopw (%rax,%rax)