// File: /workspace/github_cpp_makefile_star2_O3/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 0x61a8
movq (%r14), %rsi
leaq 0x610(%rsp), %rdi
movl $0xc, %edx
callq 0x3390
leaq 0x408(%rsp), %rdi
callq 0x4bb4
movq 0x610(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x630(%rsp,%rax)
jne 0x48a8
leaq 0x208(%rsp), %rdi
movq %rbx, %rsi
callq 0x61ba
movq 0x208(%rsp), %rsi
leaq 0x408(%rsp), %rdi
movl $0xc, %edx
callq 0x3390
leaq 0x208(%rsp), %rdi
callq 0x4bb4
movq 0x408(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x428(%rsp,%rax)
jne 0x4916
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x61cc
movq 0x98(%rsp), %rsi
leaq 0x208(%rsp), %rdi
movl $0x34, %edx
callq 0x3260
leaq 0x98(%rsp), %rdi
callq 0x4bb4
movq 0x208(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x228(%rsp,%rax)
jne 0x4981
movq %rbx, %rdi
callq 0x61a4
leaq 0x98(%rsp), %rdi
leaq 0x408(%rsp), %rsi
leaq 0x610(%rsp), %rdx
movl $0xc, %r8d
movl %eax, %ecx
callq 0x4bec
movq %rbx, %rdi
callq 0x61a4
movl %eax, %edx
leaq 0x8(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x4c90
leaq 0x48(%rsp), %rbx
leaq 0x98(%rsp), %r14
leaq 0x30(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4cc0
movq 0x88(%rsp), %r13
movq 0x90(%rsp), %r12
movq %r12, %rbp
subq %r13, %rbp
je 0x47a3
movq %r15, %rdi
movq %rbx, %rsi
callq 0x53dc
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x531c
movq %r15, %rdi
callq 0x5714
movl $0x28, %ebp
movq 0x48(%rsp,%rbp), %rdi
testq %rdi, %rdi
je 0x47b7
callq 0x4130
addq $-0x20, %rbp
cmpq $-0x18, %rbp
jne 0x47a8
cmpq %r13, %r12
jne 0x475a
leaq 0x8(%rsp), %rdi
callq 0x3e56
movl $0x140, %ebx            # imm = 0x140
movq 0x98(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x47e7
callq 0x4130
addq $-0x20, %rbx
cmpq $0x100, %rbx            # imm = 0x100
jne 0x47d5
movq 0x190(%rsp), %rdi
testq %rdi, %rdi
je 0x4806
callq 0x4130
movq 0x170(%rsp), %rdi
testq %rdi, %rdi
je 0x4818
callq 0x4130
leaq 0x7319(%rip), %r14      # 0xbb38
leaq 0x120(%rsp), %rbx
movq %r14, -0x20(%rbx)
movq %rbx, %rdi
callq 0x40a8
movq %r14, -0x80(%rbx)
leaq 0xc0(%rsp), %rdi
callq 0x40a8
movq 0x775d(%rip), %rax      # 0xbfa8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x210(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x208(%rsp,%rcx)
callq 0x31d0
leaq 0x300(%rsp), %rdi
callq 0x30d0
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
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x418(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3b98(%rip), %rsi      # 0x8460
leaq 0x3ba7(%rip), %rdx      # 0x8476
leaq 0x408(%rsp), %rdi
callq 0x41b8
movb $0x1, %bpl
leaq 0x408(%rsp), %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x72d2(%rip), %rax      # 0xbbc8
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x725e(%rip), %rsi      # 0xbb60
movq 0x76a7(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x49ea
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x218(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3b41(%rip), %rsi      # 0x8477
leaq 0x3b4f(%rip), %rdx      # 0x848c
leaq 0x208(%rsp), %rdi
callq 0x41b8
movb $0x1, %bpl
leaq 0x208(%rsp), %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x7264(%rip), %rax      # 0xbbc8
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x71f0(%rip), %rsi      # 0xbb60
movq 0x7639(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x49ea
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3aec(%rip), %rsi      # 0x848d
leaq 0x3afc(%rip), %rdx      # 0x84a4
leaq 0x98(%rsp), %rdi
callq 0x41b8
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x71f9(%rip), %rax      # 0xbbc8
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x7185(%rip), %rsi      # 0xbb60
movq 0x75ce(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x4a0a
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x4a17
jmp 0x4b3b
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x4b3b
movq %rax, %rbx
movq 0x208(%rsp), %rdi
cmpq %r15, %rdi
je 0x4a44
movq 0x218(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x4a51
jmp 0x4b48
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x4b48
movq %rax, %rbx
movq 0x408(%rsp), %rdi
cmpq %r15, %rdi
je 0x4a7e
movq 0x418(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x4a8b
jmp 0x4b55
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x4b55
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x4bb4
jmp 0x4b48
movq %rax, %rbx
jmp 0x4b48
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x4bb4
jmp 0x4b55
movq %rax, %rbx
jmp 0x4b55
movq %rax, %rbx
leaq 0x408(%rsp), %rdi
callq 0x4bb4
jmp 0x4b62
movq %rax, %rbx
jmp 0x4b2e
movq %rax, %rbx
jmp 0x4b3b
movq %rax, %rbx
jmp 0x4b00
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5714
movl $0x28, %r14d
movq 0x48(%rsp,%r14), %rdi
testq %rdi, %rdi
je 0x4b15
callq 0x4130
addq $-0x20, %r14
cmpq $-0x18, %r14
jne 0x4b06
jmp 0x4b24
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3e56
leaq 0x98(%rsp), %rdi
callq 0x5762
leaq 0x208(%rsp), %rdi
callq 0x3330
leaq 0x408(%rsp), %rdi
callq 0x3080
leaq 0x610(%rsp), %rdi
callq 0x3080
movq %rbx, %rdi
callq 0x3370