// File: /workspace/github_cpp_makefile_star2_O3/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 0x6c59
movq %rsi, %r15
movq (%rdi), %r14
leaq -0x84(%rbp), %r12
movl $0x0, (%r12)
leaq -0xc890(%rbp), %rsi
movq %r12, %rdx
callq 0x73e4
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 0x311a
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x67a6
leaq -0x70(%rbp), %r14
leaq -0x40(%rbp), %r15
leaq -0x50(%rbp), %r12
movq -0x50(%rbp), %rax
testq %rax, %rax
je 0x6aee
movq 0x18(%rax), %rdx
testq %rdx, %rdx
jle 0x6aee
movq %r14, %rdi
movq %rbx, %rsi
callq 0x67a6
movups -0x70(%rbp), %xmm0
movq -0x48(%rbp), %rdi
movups %xmm0, -0x50(%rbp)
testq %rdi, %rdi
je 0x6ae1
callq 0x277e
movq %r15, %rdi
movq %r12, %rsi
callq 0x311a
jmp 0x6aae
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x4, %rax
movl %r13d, %r12d
subl %eax, %r12d
jle 0x6b44
leaq -0x70(%rbp), %r14
leaq -0x40(%rbp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movl $0x3, %edx
callq 0x640c
movq %r15, %rdi
movq %r14, %rsi
callq 0x6f64
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6b33
callq 0x277e
decl %r12d
jne 0x6b0a
movq -0x38(%rbp), %rax
subq -0x40(%rbp), %rax
shrq $0x4, %rax
cmpl %eax, %r13d
jg 0x6c78
testl %r13d, %r13d
jle 0x6c30
leaq -0xc868(%rbp), %r14
leal -0x1(%r13), %eax
movq %rax, -0x80(%rbp)
movl %r13d, %eax
movq %rax, -0x78(%rbp)
movl $0x8, %r13d
xorl %r15d, %r15d
movq -0x40(%rbp), %r12
movq -0x8(%r12,%r13), %rdi
callq 0x73bc
movq -0x8(%r12,%r13), %rdi
movq %r14, %rsi
movl $0x1d8, %edx            # imm = 0x1D8
callq 0x73d2
movq $-0x1, %rax
cmpq -0x80(%rbp), %r15
jae 0x6bae
movq -0x40(%rbp), %rax
movq 0x8(%rax,%r13), %rax
movq (%rax), %rax
movq -0x8(%r12,%r13), %rcx
movq %rax, 0x18(%rcx)
movq %rcx, -0x60(%rbp)
movq (%r12,%r13), %r12
movq %r12, -0x58(%rbp)
testq %r12, %r12
je 0x6be7
movq 0x5401(%rip), %rax      # 0xbfd0
cmpb $0x0, (%rax)
je 0x6bf5
incl 0x8(%r12)
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
callq 0x6cda
jmp 0x6c10
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
callq 0x6cda
jmp 0x6c18
lock
incl 0x8(%r12)
movq -0x58(%rbp), %r12
movq %rbx, %rdi
leaq -0x60(%rbp), %rsi
callq 0x6cda
testq %r12, %r12
je 0x6c18
movq %r12, %rdi
callq 0x277e
incq %r15
addq $0x1d8, %r14            # imm = 0x1D8
addq $0x10, %r13
cmpq %r15, -0x78(%rbp)
jne 0x6b75
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x6c3e
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 0x1827(%rip), %rdi      # 0x8487
leaq 0x1745(%rip), %rsi      # 0x83ac
leaq 0x1836(%rip), %rcx      # 0x84a4
movl $0x86, %edx
callq 0x2140
leaq 0x185d(%rip), %rdi      # 0x84dc
leaq 0x1726(%rip), %rsi      # 0x83ac
leaq 0x1817(%rip), %rcx      # 0x84a4
movl $0x9b, %edx
callq 0x2140
jmp 0x6c99
movq %rax, %rbx
jmp 0x6cc8
jmp 0x6cb7
jmp 0x6cb7
jmp 0x6cb7
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x6cba
callq 0x277e
jmp 0x6cba
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x6cc8
callq 0x277e
leaq -0x40(%rbp), %rdi
callq 0x27f8
movq %rbx, %rdi
callq 0x2300
nop