// File: /workspace/github_cpp_makefile_star2_O2/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
callq 0x5000
movl %eax, %ebp
movq %r14, %rdi
callq 0x5000
cmpb $-0x1, %bpl
jne 0x36fb
cmpb $-0x28, %al
jne 0x36fb
movq %r14, %rdi
callq 0x5000
movl %eax, %ebp
movq %r14, %rdi
callq 0x5000
movl %eax, %r15d
leaq 0x2088(%rbx), %r13
cmpb $0x1, (%r13)
jne 0x3742
movq 0x8(%r14), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x28(%r14,%rax)
jne 0x371a
cmpb $-0x1, %bpl
jne 0x372a
cmpb $-0x40, %r15b
je 0x3654
movzbl %r15b, %r12d
cmpl $0xc2, %r12d
je 0x366a
cmpl $0xc4, %r12d
je 0x363a
cmpl $0xdb, %r12d
je 0x365d
cmpl $0xdd, %r12d
je 0x3647
cmpl $0xda, %r12d
je 0x3742
movl %r15d, %eax
andb $-0x10, %al
cmpb $-0x20, %al
jne 0x369a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2db3
jmp 0x367c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2800
jmp 0x367c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2d5a
jmp 0x367c
movb $-0x40, 0x2065(%rbx)
jmp 0x3671
movq %r14, %rdi
movq %rbx, %rsi
callq 0x26c1
jmp 0x367c
movb $-0x3e, 0x2065(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x23cc
movq %r14, %rdi
callq 0x5000
movl %eax, %r15d
movq %r14, %rdi
callq 0x5000
movl %r15d, %ebp
movl %eax, %r15d
jmp 0x35c0
cmpb $-0x2, %r15b
je 0x36c6
leal 0x10(%r15), %ecx
cmpb $0xf, %cl
jb 0x36c6
leal -0xd8(%r12), %ecx
cmpl $0x7, %ecx
ja 0x36d3
leaq 0x2973(%rip), %rdx      # 0x6030
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2e11
jmp 0x367c
cmpl $0x1, %r12d
je 0x367c
cmpl $0xff, %r12d
je 0x3687
cmpl $0xcc, %r12d
jne 0x3751
movq 0x58e6(%rip), %rdi      # 0x8fd8
leaq 0x3181(%rip), %rsi      # 0x687a
jmp 0x3738
movq 0x58d6(%rip), %rdi      # 0x8fd8
leaq 0x30db(%rip), %rsi      # 0x67e4
callq 0x20e0
addq $0x2088, %rbx           # imm = 0x2088
movq %rbx, %r13
jmp 0x373d
movq 0x58b7(%rip), %rdi      # 0x8fd8
leaq 0x30d1(%rip), %rsi      # 0x67f9
jmp 0x3738
movq 0x58a7(%rip), %rdi      # 0x8fd8
leaq 0x30e1(%rip), %rsi      # 0x6819
callq 0x20e0
movb $0x0, (%r13)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $-0x40, %al
jne 0x3785
movq 0x587c(%rip), %rdi      # 0x8fd8
leaq 0x3145(%rip), %rsi      # 0x68a8
jmp 0x37a6
movq 0x586c(%rip), %rdi      # 0x8fd8
leaq 0x30e6(%rip), %rsi      # 0x6859
jmp 0x3738
movq 0x585c(%rip), %rdi      # 0x8fd8
leaq 0x30b1(%rip), %rsi      # 0x6834
jmp 0x3738
andb $-0x8, %r15b
movq 0x5848(%rip), %rdi      # 0x8fd8
cmpb $-0x30, %r15b
jne 0x379f
leaq 0x3130(%rip), %rsi      # 0x68cd
jmp 0x3738
leaq 0x3149(%rip), %rsi      # 0x68ef
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 %r12, %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
jmp 0x373d