// File: /workspace/github_cpp_makefile_star2_O2/jansucan[P]diff-dd/src/create.cpp
// Function: create(Options::Create const&)
void
create(const Options::Create &opts)
{
    std::ifstream in_istream{opts.getInFilePath(),
                             std::ifstream::in | std::ifstream::binary};
    if (!in_istream) {
        throw BufferedStream::Error("cannot open input file");
    }

    std::ifstream base_istream{opts.getBaseFilePath(),
                               std::ifstream::in | std::ifstream::binary};
    if (!base_istream) {
        throw BufferedStream::Error("cannot open base file");
    }

    // When backing up, the output file is truncated to hold the new data
    std::ofstream out_ostream{opts.getOutFilePath(), std::ofstream::out |
                                                         std::ofstream::trunc |
                                                         std::ofstream::binary};
    if (!out_ostream) {
        throw BufferedStream::Error("cannot open output file");
    }

    DiffFinder diff_finder(base_istream, in_istream, opts.getBufferSize(),
                           FormatV2::RecordHeaderSize);
    FormatV2::Writer diff_writer(out_ostream, opts.getBufferSize());

    for (;;) {
        const Diff diff{diff_finder.findNextDiff()};
        if (diff.isEmpty()) {
            break;
        }

        diff_writer.writeDiffRecord(diff.getStart(), diff.getSize(),
                                    diff.getData());

        // Here, the diff is destructed and page data reference counters
        // decremented
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x818, %rsp            # imm = 0x818
movq %rdi, %rbx
leaq 0x408(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5b9c
movq (%r14), %rsi
leaq 0x610(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x3360
leaq 0x408(%rsp), %rdi
callq 0x49f0
movq 0x610(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x630(%rsp,%rax)
jne 0x4762
leaq 0x208(%rsp), %rdi
movq %rbx, %rsi
callq 0x5bae
movq 0x208(%rsp), %rsi
leaq 0x408(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x3360
leaq 0x208(%rsp), %rdi
callq 0x49f0
movq 0x408(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x428(%rsp,%rax)
jne 0x47b9
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x5bc0
movq 0x98(%rsp), %rsi
leaq 0x208(%rsp), %rdi
pushq $0x34
popq %rdx
callq 0x3230
leaq 0x98(%rsp), %rdi
callq 0x49f0
movq 0x208(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x228(%rsp,%rax)
jne 0x480d
movq %rbx, %rdi
callq 0x5b98
leaq 0x98(%rsp), %rdi
leaq 0x408(%rsp), %rsi
leaq 0x610(%rsp), %rdx
pushq $0xc
popq %r8
movl %eax, %ecx
callq 0x4a06
movq %rbx, %rdi
callq 0x5b98
movl %eax, %edx
leaq 0x8(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x4a78
leaq 0x48(%rsp), %rbx
leaq 0x98(%rsp), %r14
leaq 0x30(%rsp), %r15
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4aa8
movq 0x88(%rsp), %r13
movq 0x90(%rsp), %rbp
subq %r13, %rbp
je 0x4708
movq %r15, %rdi
movq %rbx, %rsi
callq 0x4e68
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x4dea
movq %r15, %rdi
callq 0x50aa
movq %rbx, %rdi
callq 0x580c
jmp 0x46ba
leaq 0x48(%rsp), %rdi
callq 0x580c
leaq 0x8(%rsp), %rdi
callq 0x3c40
leaq 0x98(%rsp), %rdi
callq 0x50cc
leaq 0x208(%rsp), %rdi
callq 0x3300
leaq 0x408(%rsp), %rdi
callq 0x3080
leaq 0x610(%rsp), %rdi
callq 0x3080
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x3cd0(%rip), %rsi      # 0x8444
leaq 0x408(%rsp), %rdi
leaq 0x208(%rsp), %rdx
callq 0x3e0e
movb $0x1, %bpl
leaq 0x408(%rsp), %rsi
movq %r14, %rdi
callq 0x3e4a
xorl %ebp, %ebp
leaq 0x838b(%rip), %rsi      # 0xcb30
movq 0x8804(%rip), %rdx      # 0xcfb0
movq %r14, %rdi
callq 0x3330
jmp 0x485c
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x3c90(%rip), %rsi      # 0x845b
leaq 0x208(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x3e0e
movb $0x1, %bpl
leaq 0x208(%rsp), %rsi
movq %r14, %rdi
callq 0x3e4a
xorl %ebp, %ebp
leaq 0x8334(%rip), %rsi      # 0xcb30
movq 0x87ad(%rip), %rdx      # 0xcfb0
movq %r14, %rdi
callq 0x3330
jmp 0x485c
pushq $0x10
popq %rdi
callq 0x3130
movq %rax, %r14
leaq 0x3c52(%rip), %rsi      # 0x8471
leaq 0x98(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x3e0e
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r14, %rdi
callq 0x3e4a
xorl %ebp, %ebp
leaq 0x82e3(%rip), %rsi      # 0xcb30
movq 0x875c(%rip), %rdx      # 0xcfb0
movq %r14, %rdi
callq 0x3330
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x3398
testb %bpl, %bpl
jne 0x4879
jmp 0x495c
movq %rax, %rbx
movq %r14, %rdi
callq 0x3180
jmp 0x495c
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x3398
testb %bpl, %bpl
jne 0x48a3
jmp 0x4969
movq %rax, %rbx
movq %r14, %rdi
callq 0x3180
jmp 0x4969
movq %rax, %rbx
leaq 0x408(%rsp), %rdi
callq 0x3398
testb %bpl, %bpl
jne 0x48cd
jmp 0x4976
movq %rax, %rbx
movq %r14, %rdi
callq 0x3180
jmp 0x4976
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x49f0
jmp 0x4969
movq %rax, %rbx
jmp 0x4969
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x49f0
jmp 0x4976
movq %rax, %rbx
jmp 0x4976
movq %rax, %rbx
leaq 0x408(%rsp), %rdi
callq 0x49f0
jmp 0x4983
movq %rax, %rbx
jmp 0x494f
movq %rax, %rbx
jmp 0x495c
movq %rax, %rbx
jmp 0x4936
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x50aa
leaq 0x48(%rsp), %rdi
callq 0x580c
jmp 0x4945
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3c40
leaq 0x98(%rsp), %rdi
callq 0x50cc
leaq 0x208(%rsp), %rdi
callq 0x3300
leaq 0x408(%rsp), %rdi
callq 0x3080
leaq 0x610(%rsp), %rdi
callq 0x3080
movq %rbx, %rdi
callq 0x3340
nop