// File: /workspace/github_cpp_makefile_star2_O2/dannye[P]jed/src/decoder.cpp
// Function: readScans(BitReader&, JPGImage*)
void readScans(BitReader& bitReader, JPGImage* const image) {
    // decode first scan
    readStartOfScan(bitReader, image);
    if (!image->valid) {
        return;
    }
    printScanInfo(image);
    decodeHuffmanData(bitReader, image);

    byte last = bitReader.readByte();
    byte current = bitReader.readByte();

    // decode additional scans, if any
    while (image->valid) {
        if (!bitReader.hasBits()) {
            std::cout << "Error - File ended prematurely\n";
            image->valid = false;
            return;
        }
        if (last != 0xFF) {
            std::cout << "Error - Expected a marker\n";
            image->valid = false;
            return;
        }

        // end of image
        if (current == EOI) {
            break;
        }
        // huffman tables (progressive only)
        else if (current == DHT && image->frameType == SOF2) {
            readHuffmanTable(bitReader, image);
        }
        // additional scans (progressive only)
        else if (current == SOS && image->frameType == SOF2) {
            readStartOfScan(bitReader, image);
            if (!image->valid) {
                return;
            }
            printScanInfo(image);
            decodeHuffmanData(bitReader, image);
        }
        // new restart interval (progressive only)
        else if (current == DRI && image->frameType == SOF2) {
            readRestartInterval(bitReader, image);
        }
        // restart marker, perhaps from the very end of previous scan
        else if (current >= RST0 && current <= RST7) {
            // RSTN has no size
        }
        // ignore multiple 0xFF's in a row
        else if (current == 0xFF) {
            current = bitReader.readByte();
            continue;
        }
        else {
            std::cout << "Error - Invalid marker: 0x" << std::hex << (uint)current << std::dec << '\n';
            image->valid = false;
            return;
        }
        last = bitReader.readByte();
        current = bitReader.readByte();
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
callq 0x2964
cmpb $0x1, 0x2088(%rbx)
jne 0x3960
movq %rbx, %rdi
callq 0x30f8
movq %r14, %rdi
movq %rbx, %rsi
callq 0x396b
movq %r14, %rdi
callq 0x5000
movl %eax, %ebp
movq %r14, %rdi
callq 0x5000
cmpb $0x1, 0x2088(%rbx)
jne 0x3960
movq 0x8(%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x28(%r14,%rcx)
jne 0x38e1
cmpb $-0x1, %bpl
jne 0x38f1
movzbl %al, %r15d
cmpb $-0x3c, %r15b
je 0x389b
cmpl $0xda, %r15d
je 0x38cd
cmpl $0xdd, %r15d
je 0x38b4
cmpl $0xd9, %r15d
je 0x3960
movl %eax, %ecx
andb $-0x8, %cl
cmpb $-0x30, %cl
je 0x3822
movb $-0x1, %bpl
cmpb $-0x1, %al
je 0x382c
jmp 0x3906
cmpb $-0x3e, 0x2065(%rbx)
jne 0x3906
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2800
jmp 0x3822
cmpb $-0x3e, 0x2065(%rbx)
jne 0x3906
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d5a
jmp 0x3822
cmpb $-0x3e, 0x2065(%rbx)
jne 0x3906
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x37fd
movq 0x56f0(%rip), %rdi      # 0x8fd8
leaq 0x2f0a(%rip), %rsi      # 0x67f9
jmp 0x38ff
movq 0x56e0(%rip), %rdi      # 0x8fd8
leaq 0x2f1a(%rip), %rsi      # 0x6819
callq 0x20e0
jmp 0x3959
movq 0x56cb(%rip), %rdi      # 0x8fd8
leaq 0x2ff6(%rip), %rsi      # 0x690a
callq 0x20e0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movq %r15, %rsi
callq 0x20a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
pushq $0xa
popq %rsi
movq %rax, %rdi
callq 0x2120
movb $0x0, 0x2088(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq