// File: /workspace/github_cpp_makefile_star2_O1/dannye[P]jed/src/decoder.cpp
// Function: readFrameHeader(BitReader&, JPGImage*)
void readFrameHeader(BitReader& bitReader, JPGImage* const image) {
    // first two bytes must be 0xFF, SOI
    byte last = bitReader.readByte();
    byte current = bitReader.readByte();
    if (last != 0xFF || current != SOI) {
        std::cout << "Error - SOI invalid\n";
        image->valid = false;
        return;
    }
    last = bitReader.readByte();
    current = bitReader.readByte();

    // read markers until first scan
    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;
        }

        if (current == SOF0) {
            image->frameType = SOF0;
            readStartOfFrame(bitReader, image);
        }
        else if (current == SOF2) {
            image->frameType = SOF2;
            readStartOfFrame(bitReader, image);
        }
        else if (current == DQT) {
            readQuantizationTable(bitReader, image);
        }
        else if (current == DHT) {
            readHuffmanTable(bitReader, image);
        }
        else if (current == SOS) {
            // break from while loop at SOS
            break;
        }
        else if (current == DRI) {
            readRestartInterval(bitReader, image);
        }
        else if (current >= APP0 && current <= APP15) {
            readAPPN(bitReader, image);
        }
        else if (current == COM) {
            readComment(bitReader, image);
        }
        // unused markers that can be skipped
        else if ((current >= JPG0 && current <= JPG13) ||
                current == DNL ||
                current == DHP ||
                current == EXP) {
            readComment(bitReader, image);
        }
        else if (current == TEM) {
            // TEM has no size
        }
        // any number of 0xFF in a row is allowed and should be ignored
        else if (current == 0xFF) {
            current = bitReader.readByte();
            continue;
        }

        else if (current == SOI) {
            std::cout << "Error - Embedded JPGs not supported\n";
            image->valid = false;
            return;
        }
        else if (current == EOI) {
            std::cout << "Error - EOI detected before SOS\n";
            image->valid = false;
            return;
        }
        else if (current == DAC) {
            std::cout << "Error - Arithmetic Coding mode not supported\n";
            image->valid = false;
            return;
        }
        else if (current >= SOF0 && current <= SOF15) {
            std::cout << "Error - SOF marker not supported: 0x" << std::hex << (uint)current << std::dec << '\n';
            image->valid = false;
            return;
        }
        else if (current >= RST0 && current <= RST7) {
            std::cout << "Error - RSTN detected before SOS\n";
            image->valid = false;
            return;
        }
        else {
            std::cout << "Error - Unknown 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 %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
xorl %r12d, %r12d
movb %r12b, 0x1(%rdi)
leaq 0x8(%rdi), %r15
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $-0x1, %bpl
jne 0x4305
movzbl %al, %eax
cmpl $0xd8, %eax
jne 0x4305
xorl %r12d, %r12d
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb %r12b, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x1, 0x2088(%rbx)
jne 0x4324
xorl %r12d, %r12d
leaq 0x2f22(%rip), %r13      # 0x70d0
movzbl %bpl, %ebp
movq 0x8(%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x28(%r14,%rcx)
jne 0x4333
cmpl $0xff, %ebp
jne 0x4365
movzbl %al, %ecx
cmpl $0xd9, %ecx
jg 0x4202
cmpl $0xc0, %ecx
je 0x42a7
cmpl $0xc2, %ecx
je 0x42bd
cmpl $0xc4, %ecx
jne 0x4222
jmp 0x429a
cmpl $0xda, %ecx
je 0x4324
cmpl $0xdb, %ecx
je 0x42b0
cmpl $0xdd, %ecx
je 0x428d
movl %eax, %edx
andb $-0x10, %dl
cmpb $-0x20, %dl
je 0x42d1
cmpb $-0x2, %al
je 0x4275
leal 0x10(%rax), %esi
cmpb $0xf, %sil
jb 0x4275
cmpb $-0x1, %al
jne 0x4260
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x0, 0x2088(%rbx)
jne 0x41b2
jmp 0x4324
leal -0xd8(%rcx), %esi
cmpl $0x7, %esi
ja 0x4282
movslq (%r13,%rsi,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x37c4
jmp 0x42dc
cmpl $0x1, %ecx
jne 0x4348
jmp 0x42dc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3691
jmp 0x42dc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x29a4
jmp 0x42dc
movb $-0x40, 0x2065(%rbx)
jmp 0x42c4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x27cd
jmp 0x42dc
movb $-0x3e, 0x2065(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23cc
jmp 0x42dc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3735
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 0x41ae
jmp 0x4324
movq 0x5ccc(%rip), %rdi      # 0x9fd8
leaq 0x3571(%rip), %rsi      # 0x7884
movl $0x14, %edx
callq 0x2130
movb $0x0, 0x2088(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5c9e(%rip), %rdi      # 0x9fd8
leaq 0x3558(%rip), %rsi      # 0x7899
movl $0x1f, %edx
jmp 0x4318
cmpl $0xcc, %ecx
jne 0x437a
movq 0x5c81(%rip), %rdi      # 0x9fd8
leaq 0x35bc(%rip), %rsi      # 0x791a
movl $0x2d, %edx
jmp 0x4318
movq 0x5c6c(%rip), %rdi      # 0x9fd8
leaq 0x3546(%rip), %rsi      # 0x78b9
movl $0x1a, %edx
jmp 0x4318
movzbl %al, %ebp
cmpb $-0x40, %dl
jne 0x4411
movq 0x5c4b(%rip), %r14      # 0x9fd8
leaq 0x35b4(%rip), %rsi      # 0x7948
movl $0x24, %edx
movq %r14, %rdi
callq 0x2130
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl %ebp, %esi
movq %r14, %rdi
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 0x6(%rsp), %rsi
jmp 0x4485
movq 0x5bf0(%rip), %rdi      # 0x9fd8
leaq 0x350a(%rip), %rsi      # 0x78f9
movl $0x20, %edx
jmp 0x4318
movq 0x5bd8(%rip), %rdi      # 0x9fd8
leaq 0x34cd(%rip), %rsi      # 0x78d4
movl $0x24, %edx
jmp 0x4318
andb $-0x8, %al
cmpb $-0x30, %al
jne 0x442f
movq 0x5bba(%rip), %rdi      # 0x9fd8
leaq 0x3548(%rip), %rsi      # 0x796d
movl $0x21, %edx
jmp 0x4318
movq 0x5ba2(%rip), %r14      # 0x9fd8
leaq 0x3552(%rip), %rsi      # 0x798f
movl $0x1a, %edx
movq %r14, %rdi
callq 0x2130
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl %ebp, %esi
movq %r14, %rdi
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 0x7(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
jmp 0x4318