// File: /workspace/github_cpp_makefile_star2_O1/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 0x6236
movq 0x98(%rsp), %rsi
leaq 0x2a8(%rsp), %rdi
movl $0xc, %edx
callq 0x31f0
leaq 0x98(%rsp), %rdi
callq 0x4bb2
movq 0x2a8(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x2c8(%rsp,%rax)
jne 0x7474
movq %rbx, %rdi
callq 0x6232
movl %eax, %edx
leaq 0x40(%rsp), %rdi
leaq 0x2a8(%rsp), %rsi
callq 0x7676
leaq 0x98(%rsp), %rdi
callq 0x32a0
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x6248
movq (%rsp), %rsi
leaq 0x98(%rsp), %rdi
movl $0x1c, %edx
callq 0x31f0
movq %rsp, %rdi
callq 0x4bb2
movq 0x98(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0xb8(%rsp,%rax)
jne 0x74df
leaq 0xa8(%rsp), %rbx
leaq 0x40(%rsp), %r14
movq %rsp, %r13
leaq 0x28(%rsp), %r12
movl $0x8, %esi
movq %r14, %rdi
movq %r13, %rdx
callq 0x3ab0
cmpq $0x8, %rax
je 0x7249
movb $0x1, 0x90(%rsp)
movb 0x90(%rsp), %r15b
testb %r15b, %r15b
jne 0x7310
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 0x73b2
movl $0x4, %esi
movq %r14, %rdi
movq %r13, %rdx
callq 0x3ab0
cmpq $0x4, %rax
je 0x729b
movb $0x1, 0x90(%rsp)
movl (%rsp), %r13d
bswapl %r13d
testq %r13, %r13
je 0x72f8
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x76bc
movq 0x28(%rsp), %rbp
testq %rbp, %rbp
je 0x72e2
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 0x7351
subq 0x28(%rsp), %r13
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x72f1
callq 0x413a
testq %rbp, %rbp
jne 0x72a2
jmp 0x72fb
xorl %r13d, %r13d
testq %r13, %r13
jne 0x7413
testb %r15b, %r15b
movq %rsp, %r13
je 0x722b
leaq 0x98(%rsp), %rdi
callq 0x3060
leaq 0x47dc(%rip), %rax      # 0xbb00
leaq 0x60(%rsp), %rdi
movq %rax, -0x20(%rdi)
callq 0x40b8
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 0x15bc(%rip), %rsi      # 0x892a
leaq 0x15d0(%rip), %rdx      # 0x8945
movq %rsp, %rdi
callq 0x41c2
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x496e(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x493a(%rip), %rsi      # 0xbcd8
movq 0x4c0b(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x753b
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1540(%rip), %rsi      # 0x890f
leaq 0x1553(%rip), %rdx      # 0x8929
movq %rsp, %rdi
callq 0x41c2
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x490d(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x48d9(%rip), %rsi      # 0xbcd8
movq 0x4baa(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x753b
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1516(%rip), %rsi      # 0x8946
leaq 0x1535(%rip), %rdx      # 0x896c
movq %rsp, %rdi
callq 0x41c2
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x48ac(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x4878(%rip), %rsi      # 0xbcd8
movq 0x4b49(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x753b
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0xa8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1465(%rip), %rsi      # 0x88f9
leaq 0x1473(%rip), %rdx      # 0x890e
leaq 0x98(%rsp), %rdi
callq 0x41c2
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x483e(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x480a(%rip), %rsi      # 0xbcd8
movq 0x4adb(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x753b
movl $0x10, %edi
callq 0x3130
movq %rax, %r14
leaq 0x10(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xf91(%rip), %rsi       # 0x848d
leaq 0xfa1(%rip), %rdx       # 0x84a4
movq %rsp, %rdi
callq 0x41c2
movb $0x1, %bpl
movq %rsp, %rsi
movq %r14, %rdi
callq 0x32d0
leaq 0x47e0(%rip), %rax      # 0xbd00
movq %rax, (%r14)
xorl %ebp, %ebp
leaq 0x47ac(%rip), %rsi      # 0xbcd8
movq 0x4a7d(%rip), %rdx      # 0xbfb0
movq %r14, %rdi
callq 0x3360
jmp 0x75c3
jmp 0x75de
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r15, %rdi
je 0x756d
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3240
jmp 0x756d
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x7660
movq %r14, %rdi
callq 0x3190
jmp 0x7660
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4bb2
jmp 0x763e
jmp 0x763b
movq %rax, %rbx
jmp 0x764b
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x4bb2
jmp 0x7660
jmp 0x75b7
movq %rax, %rbx
jmp 0x7660
jmp 0x75c3
jmp 0x75de
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x75e4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
jmp 0x75e4
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x763e
movq %r14, %rdi
callq 0x3190
jmp 0x763e
jmp 0x763b
jmp 0x763b
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r15, %rdi
je 0x7618
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3240
jmp 0x7618
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x762a
movq %r14, %rdi
callq 0x3190
jmp 0x762a
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x763e
callq 0x413a
jmp 0x763e
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x3060
leaq 0x44ae(%rip), %rax      # 0xbb00
leaq 0x60(%rsp), %rdi
movq %rax, -0x20(%rdi)
callq 0x40b8
leaq 0x2a8(%rsp), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3370
nop