// File: /workspace/github_c_makefile_star2_O0/DavidSM64[P]DKR-Decompressor/DKRCompression.cpp
// Function: DKRCompression::compressBuffer(std::vector<unsigned char, std::allocator<unsigned char>>)
std::vector<uint8_t> DKRCompression::compressBuffer(std::vector<uint8_t> data) 
{
    int uncompressedSize = data.size();
    int compressedSize = 0;
    
    uint8_t* compressed = dkr_gzip_compress(&data[0], uncompressedSize, 9, &compressedSize);
    
    std::vector<uint8_t> dkr_compressed(compressed + 10, compressed + compressedSize - 8);
    
    dkr_compressed.insert(dkr_compressed.begin(), 0x09); // gzip compression level?
    dkr_compressed.insert(dkr_compressed.begin(), (uncompressedSize >> 24) & 0xFF);
    dkr_compressed.insert(dkr_compressed.begin(), (uncompressedSize >> 16) & 0xFF);
    dkr_compressed.insert(dkr_compressed.begin(), (uncompressedSize >> 8) & 0xFF);
    dkr_compressed.insert(dkr_compressed.begin(), uncompressedSize & 0xFF);
    
    while((dkr_compressed.size() & 0xF) != 0) {
        dkr_compressed.push_back(0); // Pad out compressed file to be 16-byte aligned.
    }
    
    return dkr_compressed;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdx, -0x110(%rbp)
movq %rdi, %rax
movq -0x110(%rbp), %rdi
movq %rax, -0x108(%rbp)
movq %rax, %rcx
movq %rcx, -0x118(%rbp)
movq %rax, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
callq 0x3970
movq -0x110(%rbp), %rdi
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
xorl %eax, %eax
movl %eax, %esi
callq 0x4f30
movq %rax, %rdi
movl -0x1c(%rbp), %esi
movl $0x9, %edx
leaq -0x20(%rbp), %rcx
callq 0xb410
movq %rax, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x28(%rbp), %rax
movq %rax, %rcx
addq $0xa, %rcx
movq %rcx, -0x100(%rbp)
movslq -0x20(%rbp), %rcx
leaq -0x8(%rax,%rcx), %rax
movq %rax, -0xf8(%rbp)
leaq -0x2a(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x3290
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
movq -0xf0(%rbp), %rcx
callq 0x4f50
jmp 0x47b9
leaq -0x2a(%rbp), %rdi
callq 0x3330
movq -0x108(%rbp), %rdi
callq 0x3220
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x5010
movq -0x108(%rbp), %rdi
movb $0x9, -0x51(%rbp)
movq -0x48(%rbp), %rsi
leaq -0x51(%rbp), %rdx
callq 0x4fd0
movq %rax, -0x120(%rbp)
jmp 0x4800
movq -0x108(%rbp), %rdi
movq -0x120(%rbp), %rax
movq %rax, -0x60(%rbp)
callq 0x3220
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x5010
movq -0x108(%rbp), %rdi
movb -0x19(%rbp), %al
movb %al, -0x71(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x71(%rbp), %rdx
callq 0x4fd0
movq %rax, -0x128(%rbp)
jmp 0x484b
movq -0x108(%rbp), %rdi
movq -0x128(%rbp), %rax
movq %rax, -0x80(%rbp)
callq 0x3220
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x5010
movq -0x108(%rbp), %rdi
movb -0x1a(%rbp), %al
movb %al, -0x91(%rbp)
movq -0x88(%rbp), %rsi
leaq -0x91(%rbp), %rdx
callq 0x4fd0
movq %rax, -0x130(%rbp)
jmp 0x48a8
movq -0x108(%rbp), %rdi
movq -0x130(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x3220
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x5010
movq -0x108(%rbp), %rdi
movb -0x1b(%rbp), %al
movb %al, -0xb1(%rbp)
movq -0xa8(%rbp), %rsi
leaq -0xb1(%rbp), %rdx
callq 0x4fd0
movq %rax, -0x138(%rbp)
jmp 0x4908
movq -0x108(%rbp), %rdi
movq -0x138(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x3220
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x5010
movq -0x108(%rbp), %rdi
movb -0x1c(%rbp), %al
movb %al, -0xd1(%rbp)
movq -0xc8(%rbp), %rsi
leaq -0xd1(%rbp), %rdx
callq 0x4fd0
movq %rax, -0x140(%rbp)
jmp 0x4968
movq -0x140(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x108(%rbp), %rdi
callq 0x3970
andq $0xf, %rax
cmpq $0x0, %rax
je 0x49db
movq -0x108(%rbp), %rdi
movb $0x0, -0xe1(%rbp)
leaq -0xe1(%rbp), %rsi
callq 0x5050
jmp 0x49a8
jmp 0x4976
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x2a(%rbp), %rdi
callq 0x3330
jmp 0x4a01
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x3180
jmp 0x4a01
movb $0x1, -0x29(%rbp)
testb $0x1, -0x29(%rbp)
jne 0x49f1
movq -0x108(%rbp), %rdi
callq 0x3180
movq -0x118(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2340
nopw (%rax,%rax)