// File: /workspace/github_c_makefile_star2_O0/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
subq $0x370, %rsp            # imm = 0x370
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x1, -0x8(%rbp)
jne 0x2600
callq 0x2540
movl $0x1, -0x4(%rbp)
jmp 0x2f16
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x2350
movq -0x2d8(%rbp), %rsi
movq -0x2d0(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x2f30
jmp 0x2638
leaq -0x31(%rbp), %rdi
callq 0x2210
leaq 0x9ad2(%rip), %rsi      # 0xc11a
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x2d9(%rbp)
jmp 0x2659
movb -0x2d9(%rbp), %al
testb $0x1, %al
jne 0x2692
jmp 0x2665
leaq 0x9ab1(%rip), %rsi      # 0xc11d
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x2da(%rbp)
jmp 0x267d
movb -0x2da(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2db(%rbp)
jne 0x2692
jmp 0x269f
cmpl $0x4, -0x8(%rbp)
setne %al
movb %al, -0x2db(%rbp)
movb -0x2db(%rbp), %al
andb $0x1, %al
movb %al, -0x45(%rbp)
leaq 0x9a6f(%rip), %rsi      # 0xc120
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x2dc(%rbp)
jmp 0x26c2
movb -0x2dc(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2dd(%rbp)
jne 0x26d7
jmp 0x26e4
cmpl $0x6, -0x8(%rbp)
setne %al
movb %al, -0x2dd(%rbp)
movb -0x2dd(%rbp), %al
andb $0x1, %al
movb %al, -0x46(%rbp)
leaq 0x9a24(%rip), %rsi      # 0xc11a
leaq -0x30(%rbp), %rdi
callq 0x3010
movb %al, -0x2de(%rbp)
jmp 0x2707
movb -0x2de(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2df(%rbp)
jne 0x271c
jmp 0x276f
leaq 0x99fa(%rip), %rsi      # 0xc11d
leaq -0x30(%rbp), %rdi
callq 0x3010
movb %al, -0x2e0(%rbp)
jmp 0x2734
movb -0x2e0(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2df(%rbp)
jne 0x2749
jmp 0x276f
leaq 0x99d0(%rip), %rsi      # 0xc120
leaq -0x30(%rbp), %rdi
callq 0x3010
movb %al, -0x2e1(%rbp)
jmp 0x2761
movb -0x2e1(%rbp), %al
movb %al, -0x2df(%rbp)
jmp 0x276f
movb -0x2df(%rbp), %al
andb $0x1, %al
movb %al, -0x47(%rbp)
testb $0x1, -0x45(%rbp)
jne 0x278c
testb $0x1, -0x46(%rbp)
jne 0x278c
testb $0x1, -0x47(%rbp)
je 0x27d1
callq 0x2540
jmp 0x2793
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x2f00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x2210
jmp 0x2f22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2f0b
leaq -0xd8(%rbp), %rdi
callq 0x4680
jmp 0x27df
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2f8(%rbp)
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x2f0(%rbp)
callq 0x2350
movq -0x2f8(%rbp), %rsi
movq -0x2f0(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x2f30
jmp 0x281d
leaq -0xf9(%rbp), %rdi
callq 0x2210
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x308(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x300(%rbp)
callq 0x2350
movq -0x308(%rbp), %rsi
movq -0x300(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x2f30
jmp 0x2867
leaq -0x121(%rbp), %rdi
callq 0x2210
leaq -0x140(%rbp), %rdi
callq 0x3040
leaq -0x158(%rbp), %rdi
callq 0x3040
leaq -0x178(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x20d0
jmp 0x28a0
leaq -0xd8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x178(%rbp), %rdx
callq 0x4ca0
movb %al, -0x309(%rbp)
jmp 0x28c2
leaq -0x178(%rbp), %rdi
callq 0x2120
movb -0x309(%rbp), %al
testb $0x1, %al
jne 0x28dd
jmp 0x2e19
leaq 0x9839(%rip), %rsi      # 0xc11d
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x30a(%rbp)
jmp 0x28f5
movb -0x30a(%rbp), %al
testb $0x1, %al
jne 0x2904
jmp 0x29ea
leaq -0x1a8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x3060
jmp 0x2919
leaq -0x190(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0x1a8(%rbp), %rdx
callq 0x46f0
jmp 0x2935
leaq -0x158(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x3150
leaq -0x190(%rbp), %rdi
callq 0x3180
leaq -0x1a8(%rbp), %rdi
callq 0x3180
jmp 0x2dbd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xf9(%rbp), %rdi
callq 0x2210
jmp 0x2ef2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x2210
jmp 0x2ee6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2ec2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x2120
jmp 0x2ec2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x3180
jmp 0x2ec2
leaq 0x9729(%rip), %rsi      # 0xc11a
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x30b(%rbp)
jmp 0x2a02
movb -0x30b(%rbp), %al
testb $0x1, %al
jne 0x2a0e
jmp 0x2a8c
leaq -0x1d8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x3060
jmp 0x2a23
leaq -0x1c0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdx
callq 0x4a10
jmp 0x2a3f
leaq -0x158(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x3150
leaq -0x1c0(%rbp), %rdi
callq 0x3180
leaq -0x1d8(%rbp), %rdi
callq 0x3180
jmp 0x2dbb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x3180
jmp 0x2ec2
leaq 0x968d(%rip), %rsi      # 0xc120
leaq -0x30(%rbp), %rdi
callq 0x2fe0
movb %al, -0x30c(%rbp)
jmp 0x2aa4
movb -0x30c(%rbp), %al
testb $0x1, %al
jne 0x2ab3
jmp 0x2db9
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x320(%rbp)
leaq -0x201(%rbp), %rdi
movq %rdi, -0x318(%rbp)
callq 0x2350
movq -0x320(%rbp), %rsi
movq -0x318(%rbp), %rdx
leaq -0x200(%rbp), %rdi
callq 0x2f30
jmp 0x2af1
xorl %eax, %eax
movl %eax, %esi
leaq -0x200(%rbp), %rdi
movl $0x10, %edx
callq 0x31e0
movq %rax, -0x328(%rbp)
jmp 0x2b0f
movq -0x328(%rbp), %rax
movl %eax, -0x33c(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x2120
leaq -0x201(%rbp), %rdi
callq 0x2210
movl -0x33c(%rbp), %eax
movl %eax, -0x1dc(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x338(%rbp)
leaq -0x229(%rbp), %rdi
movq %rdi, -0x330(%rbp)
callq 0x2350
movq -0x338(%rbp), %rsi
movq -0x330(%rbp), %rdx
leaq -0x228(%rbp), %rdi
callq 0x2f30
jmp 0x2b7e
xorl %eax, %eax
movl %eax, %esi
leaq -0x228(%rbp), %rdi
movl $0x10, %edx
callq 0x31e0
movq %rax, -0x348(%rbp)
jmp 0x2b9c
movq -0x348(%rbp), %rax
movl %eax, -0x35c(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x2120
leaq -0x229(%rbp), %rdi
callq 0x2210
movl -0x35c(%rbp), %eax
movl %eax, -0x208(%rbp)
leaq -0x140(%rbp), %rdi
movq %rdi, -0x358(%rbp)
callq 0x3220
movq %rax, -0x258(%rbp)
movslq -0x1dc(%rbp), %rsi
leaq -0x258(%rbp), %rdi
callq 0x3250
movq -0x358(%rbp), %rdi
movq %rax, -0x250(%rbp)
callq 0x3220
movq %rax, -0x270(%rbp)
movslq -0x1dc(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x3250
movq %rax, -0x268(%rbp)
movslq -0x208(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x3250
movq %rax, -0x260(%rbp)
leaq -0x271(%rbp), %rdi
movq %rdi, -0x350(%rbp)
callq 0x3290
movq -0x350(%rbp), %rcx
movq -0x250(%rbp), %rsi
movq -0x260(%rbp), %rdx
leaq -0x248(%rbp), %rdi
callq 0x32b0
jmp 0x2c7e
leaq -0x271(%rbp), %rdi
callq 0x3330
leaq -0x2a8(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x3060
jmp 0x2c9f
leaq -0x290(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0x2a8(%rbp), %rdx
callq 0x4a10
jmp 0x2cbb
leaq -0x158(%rbp), %rdi
leaq -0x290(%rbp), %rsi
callq 0x3150
leaq -0x290(%rbp), %rdi
callq 0x3180
leaq -0x2a8(%rbp), %rdi
callq 0x3180
leaq -0x248(%rbp), %rdi
callq 0x3180
jmp 0x2db9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2d1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x2120
leaq -0x201(%rbp), %rdi
callq 0x2210
jmp 0x2ec2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2d54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x2120
leaq -0x229(%rbp), %rdi
callq 0x2210
jmp 0x2ec2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x271(%rbp), %rdi
callq 0x3330
jmp 0x2ec2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2da8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x3180
leaq -0x248(%rbp), %rdi
callq 0x3180
jmp 0x2ec2
jmp 0x2dbb
jmp 0x2dbd
leaq -0x2c8(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x20d0
jmp 0x2dd2
leaq -0xd8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x2c8(%rbp), %rdx
callq 0x4b30
jmp 0x2dee
leaq -0x2c8(%rbp), %rdi
callq 0x2120
jmp 0x2e76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x2c8(%rbp), %rdi
callq 0x2120
jmp 0x2ec2
movq 0xd1b0(%rip), %rdi      # 0xffd0
leaq 0x92fc(%rip), %rsi      # 0xc123
callq 0x21c0
movq %rax, -0x368(%rbp)
jmp 0x2e35
movq -0x368(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x21b0
movq %rax, -0x370(%rbp)
jmp 0x2e51
movq -0x370(%rbp), %rdi
movq 0xd161(%rip), %rsi      # 0xffc0
callq 0x21f0
jmp 0x2e66
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x2e84
movl $0x0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x3180
leaq -0x140(%rbp), %rdi
callq 0x3180
leaq -0x120(%rbp), %rdi
callq 0x2120
leaq -0xf8(%rbp), %rdi
callq 0x2120
leaq -0xd8(%rbp), %rdi
callq 0x46d0
jmp 0x2f00
leaq -0x158(%rbp), %rdi
callq 0x3180
leaq -0x140(%rbp), %rdi
callq 0x3180
leaq -0x120(%rbp), %rdi
callq 0x2120
leaq -0xf8(%rbp), %rdi
callq 0x2120
leaq -0xd8(%rbp), %rdi
callq 0x46d0
jmp 0x2f0b
leaq -0x30(%rbp), %rdi
callq 0x2120
jmp 0x2f16
leaq -0x30(%rbp), %rdi
callq 0x2120
jmp 0x2f22
movl -0x4(%rbp), %eax
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2340
nopl (%rax,%rax)