// File: dannye[P]jed/src/decoder.cpp
// Function: BitReader::readBit()
uint readBit() {
        if (nextBit == 0) {
            if (!hasBits()) {
                return -1;
            }
            nextByte = inFile.get();
            while (nextByte == 0xFF) {
                if (!hasBits()) {
                    return -1;
                }
                byte marker = inFile.peek();
                // ignore multiple 0xFF's in a row
                while (marker == 0xFF) {
                    inFile.get();
                    if (!hasBits()) {
                        return -1;
                    }
                    marker = inFile.peek();
                }
                // literal 0xFF's are encoded in the bitstream as 0xFF00
                if (marker == 0x00) {
                    inFile.get();
                    break;
                }
                // restart marker
                else if (marker >= RST0 && marker <= RST7) {
                    inFile.get();
                    nextByte = inFile.get();
                }
                else {
                    std::cout << "Error - Invalid marker: 0x" << std::hex << (uint)marker << std::dec << '\n';
                    return -1;
                }
            }
        }
        uint bit = (nextByte >> (7 - nextBit)) & 1;
        nextBit = (nextBit + 1) % 8;
        return bit;
    }

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movzbl 0x1(%rax), %eax
cmpl $0x0, %eax
jne 0x8075
movq 0x8(%rsp), %rdi
callq 0x7ae0
testb $0x1, %al
jne 0x7f1b
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x80b4
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2170
movl %eax, %ecx
movq 0x8(%rsp), %rax
movb %cl, (%rax)
movq 0x8(%rsp), %rax
movzbl (%rax), %eax
cmpl $0xff, %eax
jne 0x8073
movq 0x8(%rsp), %rdi
callq 0x7ae0
testb $0x1, %al
jne 0x7f60
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x80b4
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2060
movb %al, 0x17(%rsp)
movzbl 0x17(%rsp), %eax
cmpl $0xff, %eax
jne 0x7fbb
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2170
movq 0x8(%rsp), %rdi
callq 0x7ae0
testb $0x1, %al
jne 0x7fa7
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x80b4
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2060
movb %al, 0x17(%rsp)
jmp 0x7f72
movzbl 0x17(%rsp), %eax
cmpl $0x0, %eax
jne 0x7fd8
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2170
jmp 0x8073
movzbl 0x17(%rsp), %eax
cmpl $0xd0, %eax
jl 0x8017
movzbl 0x17(%rsp), %eax
cmpl $0xd7, %eax
jg 0x8017
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2170
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
callq 0x2170
movl %eax, %ecx
movq 0x8(%rsp), %rax
movb %cl, (%rax)
jmp 0x806c
movq 0x3fba(%rip), %rdi      # 0xbfd8
leaq 0x18a1(%rip), %rsi      # 0x98c6
callq 0x2110
movq %rax, %rdi
leaq -0x594(%rip), %rsi      # 0x7aa0
callq 0x2050
movq %rax, %rdi
movzbl 0x17(%rsp), %esi
callq 0x21b0
movq %rax, %rdi
leaq -0x590(%rip), %rsi      # 0x7ac0
callq 0x2050
movq %rax, %rdi
movl $0xa, %esi
callq 0x2160
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
jmp 0x80b4
jmp 0x806e
jmp 0x7f32
jmp 0x8075
movq 0x8(%rsp), %rax
movzbl (%rax), %edx
movzbl 0x1(%rax), %esi
movl $0x7, %ecx
subl %esi, %ecx
sarl %cl, %edx
movl %edx, %ecx
andl $0x1, %ecx
movl %ecx, 0x10(%rsp)
movzbl 0x1(%rax), %eax
addl $0x1, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movq 0x8(%rsp), %rax
movb %dl, %cl
movb %cl, 0x1(%rax)
movl 0x10(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
addq $0x28, %rsp
retq
nopl (%rax)