// File: /workspace/github_c_makefile_star2_O3/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 $0x210, %rsp            # imm = 0x210
cmpl $0x1, %edi
jne 0x258a
callq 0x245c
movl $0x1, %ebx
jmp 0x2bb2
movq %rsi, %rbx
movl %edi, %r14d
movq 0x8(%rsi), %rsi
leaq -0xe0(%rbp), %r15
leaq -0x228(%rbp), %rdx
movq %r15, %rdi
callq 0x2d5c
leaq 0x4b69(%rip), %rsi      # 0x711a
movq %r15, %rdi
callq 0x2080
testl %eax, %eax
je 0x25d9
leaq 0x4b59(%rip), %rsi      # 0x711d
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x25d9
xorl %r12d, %r12d
jmp 0x25e1
cmpl $0x4, %r14d
setne %r12b
leaq 0x4b38(%rip), %rsi      # 0x7120
movq %r15, %rdi
callq 0x2080
testl %eax, %eax
sete %al
cmpl $0x6, %r14d
setne %r14b
andb %al, %r14b
leaq 0x4b13(%rip), %rsi      # 0x711a
movq %r15, %rdi
callq 0x2080
testl %eax, %eax
je 0x2644
leaq 0x4b03(%rip), %rsi      # 0x711d
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x2644
leaq 0x4aef(%rip), %rsi      # 0x7120
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
setne %al
jmp 0x2646
xorl %eax, %eax
orb %r14b, %r12b
jne 0x28b1
testb %al, %al
jne 0x28b1
leaq -0x228(%rbp), %rdi
callq 0x3050
movq 0x10(%rbx), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x160(%rbp), %rdx
callq 0x2d5c
movq 0x18(%rbx), %rsi
leaq -0x160(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x2d5c
xorl %eax, %eax
movq %rax, -0x50(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x60(%rbp)
movq %rax, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
leaq -0x190(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x140(%rbp), %rsi
movq -0x138(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x2e74
leaq -0x228(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x32f8
movl %eax, %r14d
movq -0x1a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2700
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x2190
testb %r14b, %r14b
je 0x28c0
leaq 0x4a0d(%rip), %rsi      # 0x711d
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x2926
leaq 0x49ef(%rip), %rsi      # 0x711a
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x296c
leaq 0x49da(%rip), %rsi      # 0x7120
leaq -0xe0(%rbp), %rdi
callq 0x2080
testl %eax, %eax
jne 0x2ac3
movq 0x20(%rbx), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x2d5c
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2d99
movq %rax, %r14
leaq -0x30(%rbp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0x279a
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2190
movq 0x28(%rbx), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x2d5c
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2d99
movq %rax, %rbx
movq -0x40(%rbp), %rdi
cmpq %r15, %rdi
je 0x27d6
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2190
movslq %r14d, %rsi
addq -0x60(%rbp), %rsi
movslq %ebx, %rdx
addq %rsi, %rdx
leaq -0x40(%rbp), %rdi
leaq -0x100(%rbp), %rcx
callq 0x2e32
movq -0x38(%rbp), %rsi
subq -0x40(%rbp), %rsi
xorps %xmm0, %xmm0
leaq -0x120(%rbp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x2fb0
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %r14
movq -0x120(%rbp), %rbx
subq %rsi, %r14
je 0x2834
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2270
addq %r14, %rbx
leaq -0x120(%rbp), %rdx
movq %rbx, 0x8(%rdx)
leaq -0x100(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x31a2
movq -0x100(%rbp), %rax
movq %rax, -0x80(%rbp)
movups -0xf8(%rbp), %xmm0
movups %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x100(%rbp)
movq $0x0, -0xf0(%rbp)
movq -0x120(%rbp), %rdi
testq %rdi, %rdi
je 0x289b
movq -0x110(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2ac3
movq -0x30(%rbp), %rsi
jmp 0x2abb
movl $0x1, %ebx
callq 0x245c
jmp 0x2b93
movq 0x7711(%rip), %rdi      # 0x9fd8
leaq 0x4855(%rip), %rsi      # 0x7123
movl $0x15, %edx
callq 0x21c0
movq -0x140(%rbp), %rsi
movq -0x138(%rbp), %rdx
movq 0x76eb(%rip), %rdi      # 0x9fd8
callq 0x21c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2120
jmp 0x2b1f
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movq $0x0, -0xb0(%rbp)
subq %rsi, %rbx
je 0x29b9
js 0x2bc4
movq %rbx, %rdi
callq 0x2180
movq %rax, %r14
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r15
subq %rsi, %r15
jmp 0x29bf
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
subq %rsi, %rbx
je 0x2a3d
js 0x2bc9
movq %rbx, %rdi
callq 0x2180
movq %rax, %r14
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, %r15
subq %rsi, %r15
jmp 0x2a43
xorl %r14d, %r14d
movq %rbx, %r15
movq %r14, -0xc0(%rbp)
movq %r14, -0xb8(%rbp)
addq %r14, %rbx
movq %rbx, -0xb0(%rbp)
cmpq %rsi, %rax
je 0x29e7
movq %r14, %rdi
movq %r15, %rdx
callq 0x2270
addq %r15, %r14
leaq -0xc0(%rbp), %rdx
movq %r14, 0x8(%rdx)
leaq -0x40(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x308e
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movups -0x38(%rbp), %xmm0
movups %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2ac3
movq -0xb0(%rbp), %rsi
jmp 0x2abb
xorl %r14d, %r14d
movq %rbx, %r15
movq %r14, -0xa0(%rbp)
movq %r14, -0x98(%rbp)
addq %r14, %rbx
movq %rbx, -0x90(%rbp)
cmpq %rsi, %rax
je 0x2a6b
movq %r14, %rdi
movq %r15, %rdx
callq 0x2270
addq %r15, %r14
leaq -0xa0(%rbp), %rdx
movq %r14, 0x8(%rdx)
leaq -0x40(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x31a2
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movups -0x38(%rbp), %xmm0
movups %xmm0, -0x78(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x2ac3
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
leaq -0x170(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0x160(%rbp), %rsi
movq -0x158(%rbp), %rdx
addq %rsi, %rdx
leaq -0x180(%rbp), %rdi
callq 0x2e74
leaq -0x228(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x3212
movq -0x180(%rbp), %rdi
xorl %ebx, %ebx
cmpq %r14, %rdi
je 0x2b1f
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x2190
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x2b34
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2b49
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
leaq -0x150(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2b68
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2b87
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x228(%rbp), %rdi
callq 0x3084
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2bb2
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2190
movl %ebx, %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
callq 0x20a0
movq %rax, %rbx
movq -0x120(%rbp), %rdi
testq %rdi, %rdi
je 0x2bf1
movq -0x110(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
jmp 0x2bf1
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2cc1
movq -0x30(%rbp), %rsi
jmp 0x2c6c
jmp 0x2cbe
movq %rax, %rbx
movq -0x40(%rbp), %rdi
cmpq %r15, %rdi
jne 0x2c33
jmp 0x2cc1
jmp 0x2cbe
movq %rax, %rbx
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2cc1
movq -0x30(%rbp), %rsi
jmp 0x2c9f
jmp 0x2cbe
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x2cc1
movq -0x90(%rbp), %rsi
jmp 0x2c6c
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2cc1
movq -0xb0(%rbp), %rsi
subq %rdi, %rsi
jmp 0x2ca2
movq %rax, %rbx
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x2cc1
movq -0x170(%rbp), %rsi
jmp 0x2c9f
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2cc1
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x2190
jmp 0x2cc1
movq %rax, %rbx
jmp 0x2d0a
movq %rax, %rbx
jmp 0x2d29
jmp 0x2cb5
movq %rax, %rbx
jmp 0x2d35
jmp 0x2cbe
jmp 0x2cbe
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x2cd6
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2ceb
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
leaq -0x150(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d0a
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d29
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x228(%rbp), %rdi
callq 0x3084
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d54
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22a0