// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc868, %rsp           # imm = 0xC868
movq %rdi, -0x38(%rbp)
movq (%rsi), %rdi
cmpb $0x2, 0x8(%rdi)
jne 0x60f4
movq %rsi, %r15
movq (%rdi), %r14
leaq -0x7c(%rbp), %r12
andl $0x0, (%r12)
leaq -0xc890(%rbp), %rsi
movq %r12, %rdx
callq 0x6a44
movl (%r12), %eax
addl $-0x28, %eax
movl $0x1d8, %ecx            # imm = 0x1D8
cltd
idivl %ecx
xorl %ecx, %ecx
testl %edx, %edx
setg %cl
addl %eax, %ecx
leaq -0x50(%rbp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq (%r15), %rax
movq %rcx, -0x30(%rbp)
movw %cx, 0x24(%rax)
movq %r15, %rsi
callq 0x2e7e
leaq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %r14, %rdx
callq 0x5d7e
leaq -0x70(%rbp), %r14
leaq -0x78(%rbp), %r15
leaq -0x60(%rbp), %r12
leaq -0x50(%rbp), %r13
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x5fc7
movq 0x18(%rax), %rdx
testq %rdx, %rdx
jle 0x5fc7
movq %r15, %rdi
movq -0x38(%rbp), %rsi
callq 0x5d7e
movq %r12, %rdi
movq %r15, %rsi
callq 0x30d2
movq %r14, %rdi
callq 0x2688
movq %r13, %rdi
movq %r12, %rsi
callq 0x2e7e
jmp 0x5f89
leaq -0x50(%rbp), %r14
movq 0x8(%r14), %rax
subq (%r14), %rax
shrq $0x4, %rax
movq -0x30(%rbp), %rcx
subl %eax, %ecx
leaq -0x70(%rbp), %r15
xorl %ebx, %ebx
testl %ecx, %ecx
cmovgl %ecx, %ebx
leaq -0x78(%rbp), %r12
pushq $0x3
popq %r13
subl $0x1, %ebx
jb 0x6018
movq %r12, %rdi
movq -0x38(%rbp), %rsi
movl %r13d, %edx
callq 0x5ad2
movq %r14, %rdi
movq %r12, %rsi
callq 0x6576
movq %r15, %rdi
callq 0x2688
jmp 0x5fef
movq -0x48(%rbp), %rax
subq -0x50(%rbp), %rax
shrq $0x4, %rax
movq -0x30(%rbp), %rcx
cmpl %eax, %ecx
jg 0x6113
movq %rcx, %rax
leaq -0xc868(%rbp), %r14
decl %eax
xorl %r15d, %r15d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movq %rcx, -0x30(%rbp)
cltq
movq %rax, -0x68(%rbp)
leaq -0x90(%rbp), %r12
xorl %ebx, %ebx
cmpq %rbx, -0x30(%rbp)
je 0x60d0
movq -0x50(%rbp), %r13
movq (%r13,%r15), %rdi
callq 0x6a20
addq %r15, %r13
movq (%r13), %rdi
movq %r14, %rsi
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x6a32
pushq $-0x1
popq %rax
cmpq -0x68(%rbp), %rbx
jge 0x6095
movq -0x50(%rbp), %rax
movq 0x10(%rax,%r15), %rax
movq (%rax), %rax
movq (%r13), %rcx
movq %rax, 0x18(%rcx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x2ee6
movq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x616a
leaq -0x88(%rbp), %rdi
callq 0x2688
incq %rbx
addq $0x10, %r15
addq $0x1d8, %r14            # imm = 0x1D8
jmp 0x6058
leaq -0x58(%rbp), %rdi
callq 0x2688
leaq -0x50(%rbp), %rdi
callq 0x2726
addq $0xc868, %rsp           # imm = 0xC868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x134f(%rip), %rdi      # 0x744a
leaq 0x126d(%rip), %rsi      # 0x736f
leaq 0x135e(%rip), %rcx      # 0x7467
movl $0x86, %edx
callq 0x2140
leaq 0x1385(%rip), %rdi      # 0x749f
leaq 0x124e(%rip), %rsi      # 0x736f
leaq 0x133f(%rip), %rcx      # 0x7467
movl $0x9b, %edx
callq 0x2140
jmp 0x6134
movq %rax, %rbx
jmp 0x6158
jmp 0x614c
jmp 0x614c
jmp 0x614c
movq %rax, %rbx
movq %r15, %rdi
callq 0x2688
jmp 0x614f
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x2688
leaq -0x50(%rbp), %rdi
callq 0x2726
movq %rbx, %rdi
callq 0x2310
nop