// File: /workspace/github_c_makefile_star2_O2/DavidSM64[P]DKR-Decompressor/dkr_decompressor.cpp
// Function: main
int main(int argc, char *argv[]) 
{
    if(argc == 1) 
    {
        printHelp();
        return 1;
    }
    
    
    std::string option = std::string(argv[1]);
    
    bool fileOptionCheck = ((option == "-d" || option == "-c") && argc != 4);
    bool romOptionCheck = ((option == "-r") && argc != 6);
    bool invalidOptionCheck = (option != "-d" && option != "-c" && option != "-r");
    
    if(fileOptionCheck || romOptionCheck || invalidOptionCheck) 
    {
        printHelp();
        return 1;
    }
    
    DKRCompression compression;
    
    std::string inputFilename = std::string(argv[2]);
    std::string outputFilename = std::string(argv[3]);
    std::vector<uint8_t> inputBinary, outputBinary;
    
    if(compression.readBinaryFile(inputBinary, inputFilename))
    {
        if (option == "-c") 
        {
            outputBinary = compression.compressBuffer(inputBinary);
        } 
        else if (option == "-d") 
        {
            outputBinary = compression.decompressBuffer(inputBinary);
        }
        else if (option == "-r") 
        {
            int romOffset = std::stol(std::string(argv[4]), 0, 16);
            int length = std::stol(std::string(argv[5]), 0, 16);
            
            std::vector<uint8_t> subsection(inputBinary.begin() + romOffset, inputBinary.begin() + romOffset + length);
            
            outputBinary = compression.decompressBuffer(subsection);
        }
        
        compression.writeBinaryFile(outputBinary, outputFilename);
    }
    else 
    {
        std::cout << "Could not read file: " << inputFilename << std::endl;
        return 1;
    }
    
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1f0, %rsp            # imm = 0x1F0
cmpl $0x1, %edi
jne 0x24c5
callq 0x243c
pushq $0x1
popq %rbx
jmp 0x2881
movq %rsi, %rbx
movl %edi, %r14d
movq 0x8(%rsi), %rsi
leaq -0xa0(%rbp), %r15
leaq -0x208(%rbp), %rdx
movq %r15, %rdi
callq 0x295a
leaq 0x4c2e(%rip), %rsi      # 0x711a
movq %r15, %rdi
callq 0x2995
testb %al, %al
jne 0x250f
leaq 0x4c1e(%rip), %rsi      # 0x711d
leaq -0xa0(%rbp), %rdi
callq 0x2995
testb %al, %al
je 0x2519
cmpl $0x4, %r14d
setne %r12b
jmp 0x251c
xorl %r12d, %r12d
leaq 0x4bfd(%rip), %rsi      # 0x7120
movq %r15, %rdi
callq 0x2995
cmpl $0x6, %r14d
setne %r14b
andb %al, %r14b
leaq 0x4bdd(%rip), %rsi      # 0x711a
movq %r15, %rdi
callq 0x29a5
testb %al, %al
je 0x2575
leaq 0x4bcd(%rip), %rsi      # 0x711d
leaq -0xa0(%rbp), %rdi
callq 0x29a5
testb %al, %al
je 0x2575
leaq 0x4bb9(%rip), %rsi      # 0x7120
leaq -0xa0(%rbp), %rdi
callq 0x29a5
jmp 0x2577
xorl %eax, %eax
orb %r14b, %r12b
jne 0x2678
testb %al, %al
jne 0x2678
leaq -0x208(%rbp), %rdi
callq 0x2d44
movq 0x10(%rbx), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x120(%rbp), %rdx
callq 0x295a
movq 0x18(%rbx), %rsi
leaq -0x120(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x295a
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
andq $0x0, -0x70(%rbp)
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
leaq -0x180(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x21f0
leaq -0x208(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x2fa6
movl %eax, %r14d
leaq -0x180(%rbp), %rdi
callq 0x20c0
testb %r14b, %r14b
je 0x2685
leaq 0x4b04(%rip), %rsi      # 0x711d
leaq -0xa0(%rbp), %rdi
callq 0x2995
testb %al, %al
je 0x26b7
leaq -0x100(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x29b2
leaq -0x40(%rbp), %rdi
leaq -0x208(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x2d80
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x2c64
movq %rbx, %rdi
callq 0x2bd2
leaq -0x100(%rbp), %rdi
jmp 0x2802
callq 0x243c
pushq $0x1
popq %rbx
jmp 0x2875
movq 0x7944(%rip), %rdi      # 0x9fd0
leaq 0x4a90(%rip), %rsi      # 0x7123
callq 0x2160
leaq -0x140(%rbp), %rsi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20d0
pushq $0x1
popq %rbx
jmp 0x283f
leaq 0x4a5c(%rip), %rsi      # 0x711a
leaq -0xa0(%rbp), %rdi
callq 0x2995
testb %al, %al
je 0x2719
leaq -0xe8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x29b2
leaq -0x40(%rbp), %rdi
leaq -0x208(%rbp), %rsi
leaq -0xe8(%rbp), %rdx
callq 0x2e7c
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rbx
movq %rbx, %rsi
callq 0x2c64
movq %rbx, %rdi
callq 0x2bd2
leaq -0xe8(%rbp), %rdi
jmp 0x2802
leaq 0x4a00(%rip), %rsi      # 0x7120
leaq -0xa0(%rbp), %rdi
callq 0x2995
testb %al, %al
je 0x2807
movq 0x20(%rbx), %rsi
leaq -0x40(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x295a
leaq -0x40(%rbp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x2a05
movq %rax, %r14
leaq -0x40(%rbp), %rdi
callq 0x20c0
movq 0x28(%rbx), %rsi
leaq -0x40(%rbp), %rdi
leaq -0xb8(%rbp), %rdx
callq 0x295a
leaq -0x40(%rbp), %rdi
pushq $0x10
popq %rdx
xorl %esi, %esi
callq 0x2a05
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x20c0
movslq %r14d, %rsi
addq -0x80(%rbp), %rsi
movslq %ebx, %rdx
addq %rsi, %rdx
leaq -0x40(%rbp), %rdi
leaq -0xb8(%rbp), %rcx
callq 0x2a26
leaq -0xd0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x29b2
leaq -0xb8(%rbp), %rdi
leaq -0x208(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x2e7c
leaq -0x60(%rbp), %rdi
leaq -0xb8(%rbp), %rbx
movq %rbx, %rsi
callq 0x2c64
movq %rbx, %rdi
callq 0x2bd2
leaq -0xd0(%rbp), %rdi
callq 0x2bd2
leaq -0x40(%rbp), %rdi
callq 0x2bd2
leaq -0x160(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x21f0
leaq -0x208(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x2eec
leaq -0x160(%rbp), %rdi
callq 0x20c0
xorl %ebx, %ebx
leaq -0x60(%rbp), %rdi
callq 0x2bd2
leaq -0x80(%rbp), %rdi
callq 0x2bd2
leaq -0x120(%rbp), %rdi
callq 0x20c0
leaq -0x140(%rbp), %rdi
callq 0x20c0
leaq -0x208(%rbp), %rdi
callq 0x2d76
leaq -0xa0(%rbp), %rdi
callq 0x20c0
movl %ebx, %eax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x2bd2
jmp 0x28a7
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
jmp 0x28d6
jmp 0x290d
jmp 0x28b3
jmp 0x290d
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
jmp 0x28f5
jmp 0x290d
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
jmp 0x28d6
jmp 0x290d
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x2bd2
jmp 0x2910
jmp 0x290d
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
jmp 0x28f5
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
callq 0x20c0
jmp 0x2910
movq %rax, %rbx
jmp 0x292e
movq %rax, %rbx
jmp 0x293a
jmp 0x2908
movq %rax, %rbx
jmp 0x2946
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x2bd2
leaq -0x80(%rbp), %rdi
callq 0x2bd2
leaq -0x120(%rbp), %rdi
callq 0x20c0
leaq -0x140(%rbp), %rdi
callq 0x20c0
leaq -0x208(%rbp), %rdi
callq 0x2d76
leaq -0xa0(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2280