// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x10, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x2b81
cmpb $0x1, 0x2088(%rbx)
jne 0x45fc
movq %rbx, %rdi
callq 0x3b37
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4609
xorl %r12d, %r12d
movb %r12b, 0x1(%r14)
leaq 0x8(%r14), %r15
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x1, 0x2088(%rbx)
jne 0x45fc
movq 0x8(%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x28(%r14,%rcx)
jne 0x45c8
cmpb $-0x1, %bpl
jne 0x45dd
movzbl %al, %ecx
cmpl $0xd9, %ecx
jle 0x44bd
cmpl $0xda, %ecx
je 0x44df
cmpl $0xdd, %ecx
jne 0x4521
cmpb $-0x3e, 0x2065(%rbx)
jne 0x4560
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3616
jmp 0x452b
cmpl $0xc4, %ecx
jne 0x4515
cmpb $-0x3e, 0x2065(%rbx)
jne 0x4560
movq %r14, %rdi
movq %rbx, %rsi
callq 0x297d
jmp 0x452b
cmpb $-0x3e, 0x2065(%rbx)
jne 0x4560
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2b81
cmpb $0x1, 0x2088(%rbx)
jne 0x45fc
movq %rbx, %rdi
callq 0x3b37
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4609
jmp 0x452b
cmpl $0xd9, %ecx
je 0x45fc
movl %eax, %ecx
andb $-0x8, %cl
cmpb $-0x30, %cl
jne 0x4559
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x0, 0x2088(%rbx)
jne 0x446a
jmp 0x45fc
movb $-0x1, %bpl
cmpb $-0x1, %al
je 0x453a
movzbl %al, %r14d
movq 0x5a6d(%rip), %r15      # 0x9fd8
leaq 0x3438(%rip), %rsi      # 0x79aa
movl $0x1a, %edx
movq %r15, %rdi
callq 0x2130
movq (%r15), %rax
movq -0x18(%rax), %rax
movl 0x18(%r15,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r15,%rax)
movq %r15, %rdi
movq %r14, %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)
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
jmp 0x45f0
movq 0x5a09(%rip), %rdi      # 0x9fd8
leaq 0x32c3(%rip), %rsi      # 0x7899
movl $0x1f, %edx
jmp 0x45f0
movq 0x59f4(%rip), %rdi      # 0x9fd8
leaq 0x32ce(%rip), %rsi      # 0x78b9
movl $0x1a, %edx
callq 0x2130
movb $0x0, 0x2088(%rbx)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq