// File: /workspace/github_cpp_makefile_star2_O2/divinity76[P]pi_compression/./compressor/decompressor.cpp
// Function: main
int main(int argc, char *argv[])
{
    if(argc !=4)
    {
        std::cerr << "usage: " << argv[0] << " \"pi_file.WTF\" \"file_to_decompress\" \"save_path\"" << std::endl;
        std::cerr << "(" << (argc-1) << " arguments provided but exactly 3 arguments required.)" << std::endl;
        exit(EXIT_FAILURE);
    }
    const std::string pi_file=argv[1];
    const uint_fast32_t max_loop=(uint_fast32_t)std::min(uint64_t(UINT32_MAX),uint64_t(std::filesystem::file_size(pi_file)));
    if(max_loop < UINT32_MAX)
    {
        //std::cerr << "Warning: pi file is small, it should be slightly larger than "<< UINT32_MAX << " bytes for better compression! (UINT32_MAX)" << std::endl;
    }
    const std::string pi_binary=file_get_contents(pi_file);
    assert(max_loop <= pi_binary.size());
    const std::string file_to_decompress=argv[2];
    const size_t file_to_decompress_size=std::filesystem::file_size(file_to_decompress);
    std::string decompress_binary=file_get_contents(file_to_decompress);
    if((decompress_binary.size()%5)!=0){
        std::cerr << "error: compressed file size must be a multiple of 5 bytes, but it's not! (" << decompress_binary.size() << " bytes)" << std::endl;
        exit(EXIT_FAILURE);
    }
    const std::string save_path=argv[3];
    if(std::filesystem::exists(save_path))
    {
        std::cerr << "Error: save file already exists! delete it first" << std::endl;
        exit(EXIT_FAILURE);
    }
    std::ofstream outfile (save_path,std::ofstream::binary);
    outfile.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
    while(decompress_binary.size()>0){
        assert(decompress_binary.size()>=5);
        uint32_t sector;
        decompress_binary.copy((char*)&sector,sizeof(sector),0);
        sector=LETOHE32(sector);
        decompress_binary.erase(0,sizeof(sector));
        uint8_t length;
        decompress_binary.copy((char*)&length,sizeof(length),0);
        decompress_binary.erase(0,sizeof(length));
        outfile.write(&pi_binary[sector],length);
    }
    assert(decompress_binary.size()==0); 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, %rbx
cmpl $0x4, %edi
jne 0x25ce
movq 0x8(%rbx), %rsi
leaq 0x88(%rsp), %r14
leaq 0xa8(%rsp), %rdx
movq %r14, %rdi
callq 0x280c
leaq 0xa8(%rsp), %rdi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0x2848
leaq 0xa8(%rsp), %rdi
callq 0x2180
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0x28b0
leaq 0x68(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x275e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r14
cmovbq %r14, %rax
cmpq 0x70(%rsp), %rax
ja 0x2642
movq 0x10(%rbx), %rsi
leaq 0x48(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x280c
leaq 0xa8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x2848
leaq 0xa8(%rsp), %rdi
callq 0x2180
leaq 0xa8(%rsp), %rdi
callq 0x28b0
leaq 0x8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x275e
movq 0x10(%rsp), %rax
pushq $0x5
popq %rcx
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x265e
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x280c
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0x2848
leaq 0xa8(%rsp), %rdi
callq 0x28c6
movl %eax, %ebx
leaq 0xa8(%rsp), %rdi
callq 0x28b0
testb %bl, %bl
jne 0x2697
leaq 0xa8(%rsp), %rbx
leaq 0x28(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2210
movq 0xa8(%rsp), %rax
addq -0x18(%rax), %rbx
pushq $0x5
popq %rsi
movq %rbx, %rdi
callq 0x20a0
leaq 0x8(%rsp), %rbx
leaq 0x4(%rsp), %r14
pushq $0x4
popq %r15
leaq 0x3(%rsp), %r12
pushq $0x1
popq %r13
leaq 0xa8(%rsp), %rbp
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x255b
cmpq $0x4, %rax
jbe 0x25b1
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x2030
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x2050
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x2030
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2050
movl 0x4(%rsp), %esi
addq 0x68(%rsp), %rsi
movzbl 0x3(%rsp), %edx
movq %rbp, %rdi
callq 0x2170
jmp 0x24f5
leaq 0xa8(%rsp), %rdi
callq 0x21a0
leaq 0x28(%rsp), %rdi
callq 0x20d0
leaq 0x8(%rsp), %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
leaq 0x68(%rsp), %rdi
callq 0x20d0
leaq 0x88(%rsp), %rdi
callq 0x20d0
xorl %eax, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb91(%rip), %rdi       # 0x3149
leaq 0xad1(%rip), %rsi       # 0x3090
leaq 0xae8(%rip), %rcx       # 0x30ae
pushq $0x56
popq %rdx
callq 0x20f0
movl %edi, %ebp
movq 0x2a19(%rip), %r14      # 0x4ff0
leaq 0xa26(%rip), %rsi       # 0x3004
movq %r14, %rdi
callq 0x2150
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0xa14(%rip), %rsi       # 0x300c
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20e0
leaq 0xaff(%rip), %rsi       # 0x310e
movq %r14, %rdi
callq 0x2150
decl %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x21e0
leaq 0xa12(%rip), %rsi       # 0x303c
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20e0
pushq $0x1
popq %rdi
callq 0x2160
leaq 0xa2a(%rip), %rdi       # 0x3073
leaq 0xa40(%rip), %rsi       # 0x3090
leaq 0xa57(%rip), %rcx       # 0x30ae
pushq $0x45
jmp 0x25c8
movq 0x298b(%rip), %rdi      # 0x4ff0
leaq 0xa59(%rip), %rsi       # 0x30c5
callq 0x2150
movq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x2100
leaq 0xa8b(%rip), %rsi       # 0x3110
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20e0
jmp 0x263a
movq 0x2952(%rip), %rdi      # 0x4ff0
leaq 0xa73(%rip), %rsi       # 0x3118
callq 0x2150
movq %rax, %rdi
callq 0x20e0
jmp 0x263a
jmp 0x26ce
jmp 0x26d3
jmp 0x2711
jmp 0x26ce
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x28b0
jmp 0x2721
movq %rax, %rbx
jmp 0x2721
movq %rax, %rbx
jmp 0x272b
jmp 0x26ec
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x28b0
jmp 0x2735
movq %rax, %rbx
jmp 0x2735
movq %rax, %rbx
jmp 0x273f
jmp 0x270a
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x28b0
jmp 0x2749
movq %rax, %rbx
jmp 0x2749
jmp 0x2711
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x21a0
leaq 0x28(%rsp), %rdi
callq 0x20d0
leaq 0x8(%rsp), %rdi
callq 0x20d0
leaq 0x48(%rsp), %rdi
callq 0x20d0
leaq 0x68(%rsp), %rdi
callq 0x20d0
leaq 0x88(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x21f0