// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq 0x4b68(%rip), %rdi      # 0x6fd0
leaq 0x1be9(%rip), %rsi      # 0x4058
callq 0x2100
movq %rax, %rdi
movq %r14, %rsi
callq 0x20f0
leaq 0x1bdb(%rip), %rsi      # 0x4061
movq %rax, %rdi
callq 0x2100
leaq 0x30(%rsp), %rdi
pushq $0xc
popq %rdx
movq %r14, %rsi
callq 0x21c0
leaq 0xa8(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x2568
leaq 0x30(%rsp), %rdi
callq 0x2140
leaq 0x1bc4(%rip), %r14      # 0x4088
cmpl $0x42, %eax
jne 0x259f
leaq 0x30(%rsp), %rdi
callq 0x2140
cmpl $0x4d, %eax
jne 0x259f
leaq 0x30(%rsp), %rdi
callq 0x23dc
leaq 0x30(%rsp), %rdi
callq 0x23dc
leaq 0x30(%rsp), %rdi
callq 0x23dc
cmpl $0x1a, %eax
jne 0x257d
leaq 0x30(%rsp), %rdi
callq 0x23dc
cmpl $0xc, %eax
jne 0x2586
leaq 0x30(%rsp), %rdi
callq 0x241e
movl %eax, %r14d
movl %eax, 0x4(%r15)
leaq 0x30(%rsp), %rdi
callq 0x241e
movl %eax, %ebx
movl %eax, (%r15)
leaq 0x30(%rsp), %rdi
callq 0x241e
cmpl $0x1, %eax
jne 0x258f
leaq 0x30(%rsp), %rdi
callq 0x241e
cmpl $0x18, %eax
jne 0x2598
testl %ebx, %ebx
sete %al
testl %r14d, %r14d
sete %cl
orb %al, %cl
je 0x25d7
leaq 0x1bab(%rip), %r14      # 0x4111
jmp 0x259f
movq 0x4a61(%rip), %rdi      # 0x6fd0
leaq 0x1af0(%rip), %rsi      # 0x4066
callq 0x2100
jmp 0x25b8
leaq 0x1b1e(%rip), %r14      # 0x40a2
jmp 0x259f
leaq 0x1b2d(%rip), %r14      # 0x40ba
jmp 0x259f
leaq 0x1b3e(%rip), %r14      # 0x40d4
jmp 0x259f
leaq 0x1b57(%rip), %r14      # 0x40f6
movq 0x4a2a(%rip), %rdi      # 0x6fd0
movq %r14, %rsi
callq 0x2100
leaq 0x30(%rsp), %rdi
callq 0x2040
leaq 0x30(%rsp), %rdi
callq 0x2060
movq %r15, %rax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x20(%rsp)
addl $0x7, %ebx
shrl $0x3, %ebx
movl %ebx, 0x10(%r15)
leal 0x7(%r14), %eax
shrl $0x3, %eax
movl %eax, 0x14(%r15)
movl %eax, 0xc(%rsp)
imull %eax, %ebx
imulq $0x300, %rbx, %r13      # imm = 0x300
movq 0x49b2(%rip), %rsi      # 0x6fb8
movq %r13, %rdi
callq 0x21e0
testq %rax, %rax
je 0x271f
movq %r15, 0x18(%rsp)
testl %ebx, %ebx
movq %rax, %rbx
je 0x264a
addq $-0x300, %r13           # imm = 0xFD00
movl $0x300, %ecx            # imm = 0x300
movq %r13, %rax
xorl %edx, %edx
divq %rcx
subq %rdx, %r13
addq %rcx, %r13
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x20d0
movq 0x18(%rsp), %rax
movq %rbx, 0x28(%rsp)
movq %rbx, 0x8(%rax)
movl %r14d, %eax
andl $0x3, %eax
movl %eax, 0x8(%rsp)
leaq 0x30(%rsp), %r13
movq 0x20(%rsp), %rax
movl %eax, %ebp
decl %ebp
cmpl 0x20(%rsp), %ebp
jae 0x270b
movl %ebp, %eax
shrl $0x3, %eax
leal (,%rbp,8), %ecx
andl $0x38, %ecx
movl %ecx, 0x10(%rsp)
imull 0xc(%rsp), %eax
movl %eax, 0x14(%rsp)
xorl %ebx, %ebx
cmpl %ebx, %r14d
je 0x26f4
movq %r14, %r15
movq %r13, %rdi
callq 0x2140
movl %ebx, %r14d
andl $0x7, %r14d
orl 0x10(%rsp), %r14d
movl %ebx, %ecx
shrl $0x3, %ecx
addl 0x14(%rsp), %ecx
imulq $0x300, %rcx, %r12      # imm = 0x300
addq 0x28(%rsp), %r12
movl %eax, 0x200(%r12,%r14,4)
movq %r13, %rdi
callq 0x2140
movl %eax, 0x100(%r12,%r14,4)
movq %r13, %rdi
callq 0x2140
movl %eax, (%r12,%r14,4)
incl %ebx
movq %r15, %r14
jmp 0x2698
movl 0x8(%rsp), %ebx
subl $0x1, %ebx
jb 0x266e
movq %r13, %rdi
callq 0x2140
jmp 0x26f8
leaq 0x30(%rsp), %rdi
callq 0x2040
movq 0x18(%rsp), %r15
jmp 0x25b8
andq $0x0, 0x8(%r15)
leaq 0x1a02(%rip), %r14      # 0x412d
jmp 0x259f
jmp 0x2736
jmp 0x2736
jmp 0x2736
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x21f0