// File: /workspace/github_cpp_makefile_star2_O1/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 0x2bdc
cmpb $0x1, 0x2088(%rbx)
jne 0x46c1
movq %rbx, %rdi
callq 0x3bac
movq %r14, %rdi
movq %rbx, %rsi
callq 0x46ce
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 0x46c1
xorl %r12d, %r12d
movzbl %bpl, %ebp
movq 0x8(%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x28(%r14,%rcx)
jne 0x4625
cmpl $0xff, %ebp
jne 0x463a
movzbl %al, %ecx
cmpl $0xd9, %ecx
jle 0x453e
cmpl $0xda, %ecx
je 0x455f
cmpl $0xdd, %ecx
je 0x456c
jmp 0x4583
cmpl $0xc4, %ecx
jne 0x4577
cmpb $-0x3e, 0x2065(%rbx)
je 0x45ee
cmpb $-0x23, %al
je 0x456c
cmpl $0xda, %ecx
jne 0x4583
cmpb $-0x3e, 0x2065(%rbx)
je 0x45c1
cmpb $-0x23, %al
jne 0x4583
cmpb $-0x3e, 0x2065(%rbx)
jne 0x4583
jmp 0x45b4
cmpl $0xd9, %ecx
je 0x46c1
movl %eax, %ecx
andb $-0x8, %cl
cmpb $-0x30, %cl
je 0x45f9
cmpb $-0x1, %al
jne 0x464f
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x0, 0x2088(%rbx)
jne 0x4501
jmp 0x46c1
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3691
jmp 0x45f9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2bdc
cmpb $0x1, 0x2088(%rbx)
jne 0x46c1
movq %rbx, %rdi
callq 0x3bac
movq %r14, %rdi
movq %rbx, %rsi
callq 0x46ce
jmp 0x45f9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x29a4
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb %r12b, 0x2088(%rbx)
jne 0x44fd
jmp 0x46c1
movq 0x59ac(%rip), %rdi      # 0x9fd8
leaq 0x3266(%rip), %rsi      # 0x7899
movl $0x1f, %edx
jmp 0x46b5
movq 0x5997(%rip), %rdi      # 0x9fd8
leaq 0x3271(%rip), %rsi      # 0x78b9
movl $0x1a, %edx
jmp 0x46b5
movzbl %al, %r14d
movq 0x597e(%rip), %r15      # 0x9fd8
leaq 0x3349(%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
callq 0x2130
movb $0x0, 0x2088(%rbx)
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq