// File: /workspace/github_cpp_makefile_star2_O1/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 %r12
pushq %rbx
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rsi, %rbx
cmpl $0x4, %edi
jne 0x26bd
movq 0x8(%rbx), %rsi
leaq 0x90(%rsp), %r14
leaq 0xb0(%rsp), %rdx
movq %r14, %rdi
callq 0x29b8
leaq 0xb0(%rsp), %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0x29f6
leaq 0xb0(%rsp), %rdi
callq 0x21a0
movq %rax, %r14
leaq 0xb0(%rsp), %rdi
callq 0x2a76
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x28d6
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r14
cmovbq %r14, %rax
cmpq 0x78(%rsp), %rax
ja 0x2733
movq 0x10(%rbx), %rsi
leaq 0x50(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x29b8
leaq 0xb0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl $0x2, %edx
callq 0x29f6
leaq 0xb0(%rsp), %rdi
callq 0x21a0
leaq 0xb0(%rsp), %rdi
callq 0x2a76
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x28d6
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq 0x18(%rsp), %rax
movabsq $0x3333333333333334, %rcx # imm = 0x3333333333333334
cmpq %rcx, %rax
jae 0x2752
movq 0x18(%rbx), %rsi
leaq 0x30(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x29b8
leaq 0xb0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl $0x2, %edx
callq 0x29f6
leaq 0xb0(%rsp), %rdi
callq 0x20a0
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2a76
testb %bl, %bl
je 0x24f3
movzbl %bl, %eax
cmpl $0xff, %eax
jne 0x279c
leaq 0xb0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movl $0x4, %edx
callq 0x2240
movq 0xb0(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rdi
addq $0xb0, %rdi
movl $0x5, 0x1c(%rdi)
movl 0x20(%rdi), %esi
callq 0x2200
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x25c1
leaq 0x10(%rsp), %rbx
leaq 0xc(%rsp), %r14
leaq 0xb(%rsp), %r15
leaq 0xb0(%rsp), %r12
cmpq $0x4, %rax
jbe 0x269e
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2030
movl $0x4, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2050
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x2030
movl $0x1, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2050
movl 0xc(%rsp), %esi
addq 0x70(%rsp), %rsi
movzbl 0xb(%rsp), %edx
movq %r12, %rdi
callq 0x2190
movq 0x18(%rsp), %rax
testq %rax, %rax
jne 0x2555
movq 0x2a00(%rip), %rax      # 0x4fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0xb8(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0xb0(%rsp,%rcx)
callq 0x2130
leaq 0x1a8(%rsp), %rdi
callq 0x2090
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2614
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x262f
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x264a
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x266b
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0xa0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x268c
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2160
xorl %eax, %eax
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaa4(%rip), %rdi       # 0x3149
leaq 0x9e4(%rip), %rsi       # 0x3090
leaq 0x9fb(%rip), %rcx       # 0x30ae
movl $0x56, %edx
callq 0x20e0
movl %edi, %ebp
movq 0x292a(%rip), %r14      # 0x4ff0
leaq 0x937(%rip), %rsi       # 0x3004
movq %r14, %rdi
callq 0x2150
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0x925(%rip), %rsi       # 0x300c
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20c0
leaq 0xa10(%rip), %rsi       # 0x310e
movq %r14, %rdi
callq 0x2150
decl %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x2210
leaq 0x923(%rip), %rsi       # 0x303c
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20c0
movl $0x1, %edi
callq 0x2180
leaq 0x939(%rip), %rdi       # 0x3073
leaq 0x94f(%rip), %rsi       # 0x3090
leaq 0x966(%rip), %rcx       # 0x30ae
movl $0x45, %edx
callq 0x20e0
movq 0x2897(%rip), %rdi      # 0x4ff0
leaq 0x965(%rip), %rsi       # 0x30c5
movl $0x4a, %edx
callq 0x2170
movq 0x18(%rsp), %rsi
movq 0x287a(%rip), %rdi      # 0x4ff0
callq 0x20f0
movq %rax, %rbx
leaq 0x98b(%rip), %rsi       # 0x3110
movl $0x7, %edx
movq %rax, %rdi
callq 0x2170
movq %rbx, %rdi
callq 0x20c0
jmp 0x2729
movq 0x284d(%rip), %rdi      # 0x4ff0
leaq 0x96e(%rip), %rsi       # 0x3118
movl $0x30, %edx
callq 0x2170
movq 0x2835(%rip), %rdi      # 0x4ff0
callq 0x20c0
jmp 0x2729
jmp 0x27df
jmp 0x27e4
jmp 0x282b
jmp 0x27df
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2a76
jmp 0x283b
movq %rax, %rbx
jmp 0x283b
movq %rax, %rbx
jmp 0x2856
jmp 0x27fd
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2a76
jmp 0x2871
movq %rax, %rbx
jmp 0x2871
movq %rax, %rbx
jmp 0x288c
jmp 0x2821
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2a76
jmp 0x28ad
movq %rax, %rbx
jmp 0x28ad
jmp 0x282b
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x21c0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2856
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2871
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x288c
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28ad
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x2160
leaq 0xa0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x28ce
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x2220