// File: /workspace/github_cpp_makefile_star2_O3/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 0x426c
movzbl %al, %eax
cmpl $0xd8, %eax
jne 0x426c
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 0x42c2
leaq 0x2088(%rbx), %r12
leaq 0x2f94(%rip), %r13      # 0x70d0
movq 0x8(%r14), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x28(%r14,%rcx)
jne 0x4290
cmpb $-0x1, %bpl
jne 0x42a5
movzbl %al, %ecx
cmpl $0xd9, %ecx
jg 0x418d
cmpl $0xc0, %ecx
je 0x41ad
cmpl $0xc2, %ecx
je 0x41b6
cmpl $0xc4, %ecx
jne 0x41e3
movq %r14, %rdi
movq %rbx, %rsi
callq 0x297d
jmp 0x422f
cmpl $0xdb, %ecx
je 0x41ca
cmpl $0xdd, %ecx
jne 0x41d7
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3616
jmp 0x422f
movb $-0x40, 0x2065(%rbx)
jmp 0x41bd
movb $-0x3e, 0x2065(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23cc
jmp 0x422f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x27bb
jmp 0x422f
cmpl $0xda, %ecx
je 0x42c2
movl %eax, %edx
andb $-0x10, %dl
cmpb $-0x20, %dl
jne 0x41fa
movq %r14, %rdi
movq %rbx, %rsi
callq 0x36ba
jmp 0x422f
cmpb $-0x2, %al
je 0x4224
leal 0x10(%rax), %esi
cmpb $0xf, %sil
jb 0x4224
cmpl $0xd7, %ecx
jle 0x4258
leal -0xd8(%rcx), %esi
cmpl $0x7, %esi
ja 0x425f
movslq (%r13,%rsi,4), %rcx
addq %r13, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3749
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movl %eax, %ebp
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
cmpb $0x0, (%r12)
jne 0x413c
jmp 0x42c2
cmpl $0x1, %ecx
je 0x422f
jmp 0x42d1
cmpl $0xff, %ecx
je 0x423c
jmp 0x42ee
movq 0x5d65(%rip), %rdi      # 0x9fd8
leaq 0x360a(%rip), %rsi      # 0x7884
movl $0x14, %edx
callq 0x2130
addq $0x2088, %rbx           # imm = 0x2088
movq %rbx, %r12
jmp 0x42bd
movq 0x5d41(%rip), %rdi      # 0x9fd8
leaq 0x35fb(%rip), %rsi      # 0x7899
movl $0x1f, %edx
jmp 0x42b8
movq 0x5d2c(%rip), %rdi      # 0x9fd8
leaq 0x3606(%rip), %rsi      # 0x78b9
movl $0x1a, %edx
callq 0x2130
movb $0x0, (%r12)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xcc, %ecx
jne 0x42ee
movq 0x5cf8(%rip), %rdi      # 0x9fd8
leaq 0x3633(%rip), %rsi      # 0x791a
movl $0x2d, %edx
jmp 0x42b8
movzbl %al, %ebp
cmpb $-0x40, %dl
jne 0x4383
movq 0x5cd7(%rip), %rbx      # 0x9fd8
leaq 0x3640(%rip), %rsi      # 0x7948
movl $0x24, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%rbx,%rax)
movl %ebp, %esi
movq %rbx, %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 0x43f5
movq 0x5c7e(%rip), %rdi      # 0x9fd8
leaq 0x3573(%rip), %rsi      # 0x78d4
movl $0x24, %edx
jmp 0x42b8
movq 0x5c66(%rip), %rdi      # 0x9fd8
leaq 0x3580(%rip), %rsi      # 0x78f9
movl $0x20, %edx
jmp 0x42b8
andb $-0x8, %al
cmpb $-0x30, %al
jne 0x43a1
movq 0x5c48(%rip), %rdi      # 0x9fd8
leaq 0x35d6(%rip), %rsi      # 0x796d
movl $0x21, %edx
jmp 0x42b8
movq 0x5c30(%rip), %rbx      # 0x9fd8
leaq 0x35e0(%rip), %rsi      # 0x798f
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x2130
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%rbx,%rax)
movl %ebp, %esi
movq %rbx, %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 0x42b8