// File: /workspace/github_cpp_makefile_star2_O3/dannye[P]jed/src/encoder.cpp
// Function: readBMP(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
BMPImage readBMP(const std::string& filename) {
    BMPImage image;

    // open file
    std::cout << "Reading " << filename << "...\n";
    std::ifstream inFile(filename, std::ios::in | std::ios::binary);
    if (!inFile.is_open()) {
        std::cout << "Error - Error opening input file\n";
        return image;
    }

    if (inFile.get() != 'B' || inFile.get() != 'M') {
        std::cout << "Error - Invalid BMP file\n";
        inFile.close();
        return image;
    }

    getInt(inFile); // size
    getInt(inFile); // nothing
    if (getInt(inFile) != 0x1A) {
        std::cout << "Error - Invalid offset\n";
        inFile.close();
        return image;
    }
    if (getInt(inFile) != 12) {
        std::cout << "Error - Invalid DIB size\n";
        inFile.close();
        return image;
    }
    image.width = getShort(inFile);
    image.height = getShort(inFile);
    if (getShort(inFile) != 1) {
        std::cout << "Error - Invalid number of planes\n";
        inFile.close();
        return image;
    }
    if (getShort(inFile) != 24) {
        std::cout << "Error - Invalid bit depth\n";
        inFile.close();
        return image;
    }

    if (image.height == 0 || image.width == 0) {
        std::cout << "Error - Invalid dimensions\n";
        inFile.close();
        return image;
    }

    image.blockHeight = (image.height + 7) / 8;
    image.blockWidth = (image.width + 7) / 8;

    image.blocks = new (std::nothrow) Block[image.blockHeight * image.blockWidth];
    if (image.blocks == nullptr) {
        std::cout << "Error - Memory error\n";
        inFile.close();
        return image;
    }

    const uint paddingSize = image.width % 4;

    for (uint y = image.height - 1; y < image.height; --y) {
        const uint blockRow = y / 8;
        const uint pixelRow = y % 8;
        for (uint x = 0; x < image.width; ++x) {
            const uint blockColumn = x / 8;
            const uint pixelColumn = x % 8;
            const uint blockIndex = blockRow * image.blockWidth + blockColumn;
            const uint pixelIndex = pixelRow * 8 + pixelColumn;
            image.blocks[blockIndex].b[pixelIndex] = inFile.get();
            image.blocks[blockIndex].g[pixelIndex] = inFile.get();
            image.blocks[blockIndex].r[pixelIndex] = inFile.get();
        }
        for (uint i = 0; i < paddingSize; ++i) {
            inFile.get();
        }
    }

    inFile.close();
    return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rsi, %r14
movq %rdi, %r13
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
movq 0x4b8d(%rip), %r15      # 0x6fd8
leaq 0x2c6e(%rip), %rsi      # 0x50c0
movl $0x8, %edx
movq %r15, %rdi
callq 0x2120
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x2120
leaq 0x2c54(%rip), %rsi      # 0x50c9
movl $0x4, %edx
movq %rax, %rdi
callq 0x2120
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
movl $0xc, %edx
callq 0x2190
leaq 0xa8(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x25b3
leaq 0x30(%rsp), %rdi
callq 0x2130
movl $0x19, %r14d
leaq 0x2c30(%rip), %r15      # 0x50f0
cmpl $0x42, %eax
jne 0x2601
leaq 0x30(%rsp), %rdi
callq 0x2130
cmpl $0x4d, %eax
jne 0x2601
leaq 0x30(%rsp), %rdi
callq 0x23bc
leaq 0x30(%rsp), %rdi
callq 0x23bc
leaq 0x30(%rsp), %rdi
callq 0x23bc
cmpl $0x1a, %eax
jne 0x25cd
leaq 0x30(%rsp), %rdi
callq 0x23bc
cmpl $0xc, %eax
jne 0x25dc
leaq 0x30(%rsp), %rdi
callq 0x2130
movl %eax, 0x4(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x2130
movl %eax, %ebp
shll $0x8, %ebp
addl 0x4(%rsp), %ebp
movl %ebp, 0x4(%r13)
leaq 0x30(%rsp), %rdi
callq 0x2130
movl %eax, %r12d
leaq 0x30(%rsp), %rdi
callq 0x2130
movl %eax, %r15d
shll $0x8, %r15d
addl %r12d, %r15d
movl %r15d, (%r13)
leaq 0x30(%rsp), %rdi
callq 0x2130
movl %eax, %r12d
leaq 0x30(%rsp), %rdi
callq 0x2130
shll $0x8, %eax
addl %r12d, %eax
cmpl $0x1, %eax
jne 0x25e5
leaq 0x30(%rsp), %rdi
callq 0x23fe
cmpl $0x18, %eax
jne 0x25f4
testl %r15d, %r15d
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
je 0x263c
movl $0x1b, %r14d
leaq 0x2bc8(%rip), %r15      # 0x5179
jmp 0x2601
movq 0x4a1e(%rip), %rdi      # 0x6fd8
leaq 0x2b0d(%rip), %rsi      # 0x50ce
movl $0x21, %edx
callq 0x2120
jmp 0x261d
movl $0x17, %r14d
leaq 0x2b30(%rip), %r15      # 0x510a
jmp 0x2601
leaq 0x2b3f(%rip), %r15      # 0x5122
jmp 0x2601
movl $0x21, %r14d
leaq 0x2b4a(%rip), %r15      # 0x513c
jmp 0x2601
movl $0x1a, %r14d
leaq 0x2b5d(%rip), %r15      # 0x515e
movq 0x49d0(%rip), %rdi      # 0x6fd8
movq %r15, %rsi
movq %r14, %rdx
callq 0x2120
leaq 0x30(%rsp), %rdi
callq 0x2040
leaq 0x30(%rsp), %rdi
callq 0x2060
movq %r13, %rax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal 0x7(%r15), %ebx
shrl $0x3, %ebx
movl %ebx, 0x10(%r13)
leal 0x7(%rbp), %eax
shrl $0x3, %eax
movl %eax, 0x14(%r13)
movl %eax, 0x8(%rsp)
imull %eax, %ebx
movq %rbx, %rax
shlq $0x8, %rax
movq %r13, %r12
leaq (%rax,%rax,2), %r13
movq 0x494b(%rip), %rsi      # 0x6fb8
movq %r13, %rdi
callq 0x21b0
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2797
movq %r12, 0x10(%rsp)
testl %ebx, %ebx
movq 0x18(%rsp), %rbx
je 0x26c5
addq $-0x300, %r13           # imm = 0xFD00
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
movq %r13, %rax
mulq %rcx
shrq $0x9, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x8, %rdx
addq $0x300, %rdx            # imm = 0x300
movq %rbx, %rdi
xorl %esi, %esi
callq 0x20a0
movq 0x10(%rsp), %rax
movq %rbx, 0x8(%rax)
andl $0x3, 0x4(%rsp)
leal -0x1(%r15), %r12d
leaq 0x30(%rsp), %r13
movq %r15, 0x20(%rsp)
movl %r12d, %r15d
shrl $0x3, %r15d
movq %r12, 0x28(%rsp)
leal (,%r12,8), %eax
andl $0x38, %eax
movl %eax, 0xc(%rsp)
imull 0x8(%rsp), %r15d
xorl %ebx, %ebx
movq %r13, %rdi
callq 0x2130
movl %ebx, %r14d
andl $0x7, %r14d
orl 0xc(%rsp), %r14d
movl %ebx, %ecx
shrl $0x3, %ecx
addl %r15d, %ecx
leaq (%rcx,%rcx,2), %r12
shlq $0x8, %r12
addq 0x18(%rsp), %r12
movl %eax, 0x200(%r12,%r14,4)
movq %r13, %rdi
callq 0x2130
movl %eax, 0x100(%r12,%r14,4)
movq %r13, %rdi
callq 0x2130
movl %eax, (%r12,%r14,4)
incl %ebx
cmpl %ebx, %ebp
jne 0x2704
movl 0x4(%rsp), %eax
movl %eax, %ebx
testl %eax, %eax
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r12
je 0x2777
movq %r13, %rdi
callq 0x2130
decl %ebx
jne 0x276b
decl %r12d
cmpl %r15d, %r12d
jb 0x26e1
leaq 0x30(%rsp), %rdi
callq 0x2040
movq 0x10(%rsp), %r13
jmp 0x261d
movq $0x0, 0x8(%r12)
movl $0x15, %r14d
leaq 0x29e8(%rip), %r15      # 0x5195
movq %r12, %r13
jmp 0x2601
jmp 0x27bb
jmp 0x27bb
jmp 0x27bb
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x21c0