// File: /workspace/github_cpp_makefile_star2_O1/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, %rbx
movq (%rsi), %rdi
cmpb $0x2, 0x8(%rdi)
jne 0x6c8c
movq %rsi, %r15
movq (%rdi), %r14
leaq -0x84(%rbp), %r12
movl $0x0, (%r12)
leaq -0xc890(%rbp), %rsi
movq %r12, %rdx
callq 0x742c
movl (%r12), %eax
movl %eax, %ecx
addl $-0x28, %ecx
movslq %ecx, %rcx
imulq $0x22b63cbf, %rcx, %rcx # imm = 0x22B63CBF
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
imull $0x1d8, %ecx, %edx      # imm = 0x1D8
negl %edx
addl %edx, %eax
addl $-0x28, %eax
xorl %r13d, %r13d
testl %eax, %eax
setg %r13b
addl %ecx, %r13d
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq (%r15), %rax
movw %r13w, 0x24(%rax)
movq %r15, %rsi
callq 0x30f6
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x67b8
leaq -0x50(%rbp), %r14
leaq -0x40(%rbp), %r15
leaq -0x60(%rbp), %r12
movq -0x60(%rbp), %rax
testq %rax, %rax
je 0x6b3f
movq 0x18(%rax), %rdx
testq %rdx, %rdx
jle 0x6b3f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x67b8
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq -0x58(%rbp), %rdi
movups %xmm0, -0x60(%rbp)
testq %rdi, %rdi
je 0x6b24
callq 0x277e
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x6b32
callq 0x277e
movq %r15, %rdi
movq %r12, %rsi
callq 0x30f6
jmp 0x6aea
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x4, %rax
movl %r13d, %r12d
subl %eax, %r12d
jle 0x6b89
leaq -0x50(%rbp), %r14
leaq -0x40(%rbp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x63b2
movq %r15, %rdi
movq %r14, %rsi
callq 0x6f9c
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x6b84
callq 0x277e
decl %r12d
jne 0x6b5b
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x4, %rax
cmpl %eax, %r13d
jg 0x6cab
testl %r13d, %r13d
jle 0x6c63
leaq -0xc868(%rbp), %r14
leal -0x1(%r13), %eax
cltq
movq %rax, -0x70(%rbp)
movl %r13d, %eax
movq %rax, -0x68(%rbp)
movl $0x8, %r15d
xorl %r12d, %r12d
movq -0x40(%rbp), %r13
movq -0x8(%r13,%r15), %rdi
callq 0x7404
movq -0x8(%r13,%r15), %rdi
movq %r14, %rsi
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x741a
movq $-0x1, %rax
cmpq -0x70(%rbp), %r12
jge 0x6c01
movq -0x40(%rbp), %rax
movq 0x8(%rax,%r15), %rax
movq (%rax), %rax
movq -0x8(%r13,%r15), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, -0x80(%rbp)
movq (%r13,%r15), %rax
movq %rax, -0x78(%rbp)
testq %rax, %rax
je 0x6c31
movq 0x53ad(%rip), %rcx      # 0xbfd0
cmpb $0x0, (%rcx)
je 0x6c2d
incl 0x8(%rax)
jmp 0x6c31
lock
incl 0x8(%rax)
movq %rbx, %rdi
leaq -0x80(%rbp), %rsi
callq 0x6d0c
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x6c4b
callq 0x277e
incq %r12
addq $0x1d8, %r14            # imm = 0x1D8
addq $0x10, %r15
cmpq %r12, -0x68(%rbp)
jne 0x6bc8
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x6c71
callq 0x277e
leaq -0x40(%rbp), %rdi
callq 0x27f8
addq $0xc868, %rsp           # imm = 0xC868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17f8(%rip), %rdi      # 0x848b
leaq 0x1716(%rip), %rsi      # 0x83b0
leaq 0x1807(%rip), %rcx      # 0x84a8
movl $0x86, %edx
callq 0x2140
leaq 0x182e(%rip), %rdi      # 0x84e0
leaq 0x16f7(%rip), %rsi      # 0x83b0
leaq 0x17e8(%rip), %rcx      # 0x84a8
movl $0x9b, %edx
callq 0x2140
jmp 0x6ccc
movq %rax, %rbx
jmp 0x6cfb
jmp 0x6cea
jmp 0x6cea
jmp 0x6cea
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x6ced
callq 0x277e
jmp 0x6ced
movq %rax, %rbx
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x6cfb
callq 0x277e
leaq -0x40(%rbp), %rdi
callq 0x27f8
movq %rbx, %rdi
callq 0x2300