// File: /workspace/github_cpp_makefile_star2_O3/jansucan[P]diff-dd/src/restore.cpp
// Function: restore(Options::Restore const&)
void
restore(const Options::Restore &opts)
{
    std::fstream diff_stream;
    diff_stream.open(opts.getDiffFilePath(),
                     std::ifstream::in | std::ifstream::binary);
    if (!diff_stream) {
        throw RestoreError("cannot open diff file");
    }

    FormatV2::Reader diff_reader(diff_stream, opts.getBufferSize());

    std::fstream out_file;
    out_file.open(opts.getOutFilePath(),
                  std::ios::in | std::ios::out | std::ios::binary);
    if (!out_file) {
        throw RestoreError("cannot open output file");
    }

    for (;;) {
        const uint64_t offset{diff_reader.readOffset()};
        if (diff_reader.eof()) {
            break;
        }

        if (!out_file.seekp(offset, std::ios_base::beg)) {
            throw RestoreError("cannot seek in output file");
        }

        uint64_t size{diff_reader.readSize()};

        while (size > 0) {
            const FormatV2::RecordData rd{diff_reader.readRecordData(size)};
            if (rd.size == 0) {
                break;
            }

            if (!out_file.write(rd.data.get(), rd.size)) {
                throw RestoreError("cannot write to output file");
            }

            size -= rd.size;
        }

        if (size > 0) {
            throw RestoreError("cannot read all the data of the record");
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
movq %rdi, %rbx
leaq 0x2a8(%rsp), %rdi
callq 0x32a0
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x6202
movq 0x98(%rsp), %rsi
leaq 0x2a8(%rsp), %rdi
movl $0xc, %edx
callq 0x31f0
leaq 0x98(%rsp), %rdi
callq 0x4bb4
movq 0x2a8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2c8(%rsp,%rax)
jne 0x752c
movq %rbx, %rdi
callq 0x61fe
movl %eax, %edx
leaq 0x40(%rsp), %rdi
leaq 0x2a8(%rsp), %rsi
callq 0x7720
leaq 0x98(%rsp), %rdi
callq 0x32a0
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x6214
movq (%rsp), %rsi
leaq 0x98(%rsp), %rdi
movl $0x1c, %edx
callq 0x31f0
movq %rsp, %rdi
callq 0x4bb4
movq 0x98(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xb8(%rsp,%rax)
jne 0x7597
leaq 0xa8(%rsp), %rbx
leaq 0x40(%rsp), %r14
movq %rsp, %r15
leaq 0x28(%rsp), %r12
movl $0x8, %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x3aa0
cmpq $0x8, %rax
jne 0x7421
testb $0x1, 0x90(%rsp)
jne 0x7429
movq (%rsp), %rsi
bswapq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3220
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x74cb
movl $0x4, %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x3aa0
cmpq $0x4, %rax
je 0x7358
movb $0x1, 0x90(%rsp)
movl (%rsp), %r13d
bswapl %r13d
testq %r13, %r13
je 0x72ef
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x7766
movq 0x28(%rsp), %rbp
testq %rbp, %rbp
je 0x73a3
movq 0x30(%rsp), %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x3300
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x746a
subq 0x28(%rsp), %r13
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x73b2
callq 0x4130
testq %rbp, %rbp
jne 0x735f
testq %r13, %r13
je 0x72ef
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1569(%rip), %rsi      # 0x8946
leaq 0x1588(%rip), %rdx      # 0x896c
movq %rsp, %rdi
callq 0x41b8
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x48ff(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x48cb(%rip), %rsi      # 0xbcd8
movq 0x4b9c(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x75f3
movb $0x1, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x3060
leaq 0x46fb(%rip), %rax      # 0xbb38
leaq 0x60(%rsp), %rdi
movq %rax, -0x20(%rdi)
callq 0x40a8
leaq 0x2a8(%rsp), %rdi
callq 0x3060
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x14a3(%rip), %rsi      # 0x892a
leaq 0x14b7(%rip), %rdx      # 0x8945
movq %rsp, %rdi
callq 0x41b8
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x4855(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x4821(%rip), %rsi      # 0xbcd8
movq 0x4af2(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x75f3
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1427(%rip), %rsi      # 0x890f
leaq 0x143a(%rip), %rdx      # 0x8929
movq %rsp, %rdi
callq 0x41b8
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x47f4(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x47c0(%rip), %rsi      # 0xbcd8
movq 0x4a91(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x75f3
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x13ad(%rip), %rsi      # 0x88f9
leaq 0x13bb(%rip), %rdx      # 0x890e
leaq 0x98(%rsp), %rdi
callq 0x41b8
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x4786(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x4752(%rip), %rsi      # 0xbcd8
movq 0x4a23(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x75f3
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xed9(%rip), %rsi       # 0x848d
leaq 0xee9(%rip), %rdx       # 0x84a4
movq %rsp, %rdi
callq 0x41b8
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x4728(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x46f4(%rip), %rsi      # 0xbcd8
movq 0x49c5(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x7674
jmp 0x7694
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x761a
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x7627
jmp 0x770b
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x770b
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4bb4
jmp 0x76e9
jmp 0x76e6
movq %rax, %rbx
jmp 0x76f6
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x4bb4
jmp 0x770b
jmp 0x7668
movq %rax, %rbx
jmp 0x770b
jmp 0x7674
jmp 0x7694
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x768d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x7697
jmp 0x76e9
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x76e9
jmp 0x76e6
jmp 0x76e6
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x76be
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
testb %bpl, %bpl
jne 0x76c8
jmp 0x76d5
movq %rax, %rbx
movq %r14, %rdi
callq 0x3190
jmp 0x76d5
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x76e9
callq 0x4130
jmp 0x76e9
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x3060
leaq 0x443b(%rip), %rax      # 0xbb38
leaq 0x60(%rsp), %rdi
movq %rax, -0x20(%rdi)
callq 0x40a8
leaq 0x2a8(%rsp), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3370