// File: /workspace/github_c_makefile_star2_O1/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 0x2c1b
movq %rsi, %rbx
movl %edi, %r14d
movq 0x8(%rsi), %rsi
leaq -0x100(%rbp), %r15
leaq -0x228(%rbp), %rdx
movq %r15, %rdi
callq 0x2dc6
leaq 0x4b69(%rip), %rsi      # 0x711a
movq %r15, %rdi
callq 0x2080
testl %eax, %eax
je 0x25d9
leaq 0x4b59(%rip), %rsi      # 0x711d
leaq -0x100(%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 -0x100(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x2644
leaq 0x4aef(%rip), %rsi      # 0x7120
leaq -0x100(%rbp), %rdi
callq 0x2080
testl %eax, %eax
setne %al
jmp 0x2646
xorl %eax, %eax
orb %r14b, %r12b
jne 0x28e1
testb %al, %al
jne 0x28e1
leaq -0x228(%rbp), %rdi
callq 0x30b4
movq 0x10(%rbx), %rsi
leaq -0x140(%rbp), %rdi
leaq -0x160(%rbp), %rdx
callq 0x2dc6
movq 0x18(%rbx), %rsi
leaq -0x160(%rbp), %rdi
leaq -0x40(%rbp), %rdx
callq 0x2dc6
xorl %eax, %eax
movq %rax, -0x70(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq %rax, -0x50(%rbp)
movaps %xmm0, -0x60(%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 0x2ede
leaq -0x228(%rbp), %rdi
leaq -0x80(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x335c
movl %eax, %r14d
movq -0x1a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2700
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x2190
testb %r14b, %r14b
je 0x28f0
leaq 0x4a0d(%rip), %rsi      # 0x711d
leaq -0x100(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x2956
leaq 0x49ef(%rip), %rsi      # 0x711a
leaq -0x100(%rbp), %rdi
callq 0x2080
testl %eax, %eax
je 0x2988
leaq 0x49da(%rip), %rsi      # 0x7120
leaq -0x100(%rbp), %rdi
callq 0x2080
testl %eax, %eax
jne 0x2b2c
movq 0x20(%rbx), %rsi
leaq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x2dc6
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2e03
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 -0xa0(%rbp), %rdx
callq 0x2dc6
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2e03
movq %rax, %rbx
movq -0x40(%rbp), %rdi
cmpq %r15, %rdi
je 0x27d6
movq -0x30(%rbp), %rsi
incq %rsi
callq 0x2190
movslq %r14d, %rsi
addq -0x80(%rbp), %rsi
movslq %ebx, %rdx
addq %rsi, %rdx
leaq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rcx
callq 0x2e9c
movq -0x38(%rbp), %rsi
subq -0x40(%rbp), %rsi
xorps %xmm0, %xmm0
leaq -0x120(%rbp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x3012
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 -0xa0(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x3206
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movups -0x98(%rbp), %xmm0
movups %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0xa0(%rbp)
movq $0x0, -0x90(%rbp)
testq %rdi, %rdi
je 0x2895
subq %rdi, %rsi
callq 0x2190
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x28b0
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x120(%rbp), %rdi
testq %rdi, %rdi
je 0x28cb
movq -0x110(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2b2c
movq -0x30(%rbp), %rsi
jmp 0x2b24
movl $0x1, %ebx
callq 0x245c
jmp 0x2bfc
movq 0x76e1(%rip), %rdi      # 0x9fd8
leaq 0x4825(%rip), %rsi      # 0x7123
movl $0x15, %edx
callq 0x21c0
movq -0x140(%rbp), %rsi
movq -0x138(%rbp), %rdx
movq 0x76bb(%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 0x2b88
movq -0x78(%rbp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xe0(%rbp)
movq $0x0, -0xd0(%rbp)
subq -0x80(%rbp), %rbx
je 0x29c1
js 0x2c2d
movq %rbx, %rdi
callq 0x2180
movq %rax, %r14
jmp 0x29c4
movq -0x78(%rbp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0xc0(%rbp)
movq $0x0, -0xb0(%rbp)
subq -0x80(%rbp), %rbx
je 0x2a77
js 0x2c32
movq %rbx, %rdi
callq 0x2180
movq %rax, %r14
jmp 0x2a7a
xorl %r14d, %r14d
movq %r14, -0xe0(%rbp)
movq %r14, -0xd8(%rbp)
addq %r14, %rbx
movq %rbx, -0xd0(%rbp)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rbx
subq %rsi, %rbx
je 0x29f4
movq %r14, %rdi
movq %rbx, %rdx
callq 0x2270
addq %rbx, %r14
leaq -0xe0(%rbp), %rdx
movq %r14, 0x8(%rdx)
leaq -0x40(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x30f2
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movups -0x38(%rbp), %xmm0
movups %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
testq %rdi, %rdi
je 0x2a46
subq %rdi, %rsi
callq 0x2190
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2a5b
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x2b2c
movq -0xd0(%rbp), %rsi
jmp 0x2b24
xorl %r14d, %r14d
movq %r14, -0xc0(%rbp)
movq %r14, -0xb8(%rbp)
addq %r14, %rbx
movq %rbx, -0xb0(%rbp)
movq -0x80(%rbp), %rsi
movq -0x78(%rbp), %rbx
subq %rsi, %rbx
je 0x2aaa
movq %r14, %rdi
movq %rbx, %rdx
callq 0x2270
addq %rbx, %r14
leaq -0xc0(%rbp), %rdx
movq %r14, 0x8(%rdx)
leaq -0x40(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x3206
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movups -0x38(%rbp), %xmm0
movups %xmm0, -0x58(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
testq %rdi, %rdi
je 0x2afc
subq %rdi, %rsi
callq 0x2190
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2b11
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2b2c
movq -0xb0(%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 0x2ede
leaq -0x228(%rbp), %rdi
leaq -0x60(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x3276
movq -0x180(%rbp), %rdi
xorl %ebx, %ebx
cmpq %r14, %rdi
je 0x2b88
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x2190
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2b9d
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x2bb2
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
leaq -0x150(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2bd1
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2bf0
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x228(%rbp), %rdi
callq 0x30e8
leaq -0xf0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2c1b
movq -0xf0(%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 0x2c5a
movq -0x110(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
jmp 0x2c5a
movq %rax, %rbx
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2d2a
movq -0x30(%rbp), %rsi
jmp 0x2cd5
jmp 0x2d27
movq %rax, %rbx
movq -0x40(%rbp), %rdi
cmpq %r15, %rdi
jne 0x2c9c
jmp 0x2d2a
jmp 0x2d27
movq %rax, %rbx
leaq -0x30(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d2a
movq -0x30(%rbp), %rsi
jmp 0x2d08
jmp 0x2d27
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
testq %rdi, %rdi
je 0x2d2a
movq -0xb0(%rbp), %rsi
jmp 0x2cd5
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x2d2a
movq -0xd0(%rbp), %rsi
subq %rdi, %rsi
jmp 0x2d0b
movq %rax, %rbx
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x2d2a
movq -0x170(%rbp), %rsi
jmp 0x2d08
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2d2a
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x2190
jmp 0x2d2a
movq %rax, %rbx
jmp 0x2d73
movq %rax, %rbx
jmp 0x2d92
jmp 0x2d1e
movq %rax, %rbx
jmp 0x2d9e
jmp 0x2d27
jmp 0x2d27
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2d3f
movq -0x50(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x2d54
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x2190
leaq -0x150(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d73
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x130(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2d92
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x2190
leaq -0x228(%rbp), %rdi
callq 0x30e8
leaq -0xf0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x2dbd
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22a0
nop