// File: dannye[P]jed/src/decoder.cpp
// Function: decodeHuffmanData(BitReader&, JPGImage*)
void decodeHuffmanData(BitReader& bitReader, JPGImage* const image) {
    int previousDCs[3] = { 0 };
    uint skips = 0;

    const bool luminanceOnly = image->componentsInScan == 1 && image->colorComponents[0].usedInScan;
    const uint yStep = luminanceOnly ? 1 : image->verticalSamplingFactor;
    const uint xStep = luminanceOnly ? 1 : image->horizontalSamplingFactor;
    const uint restartInterval = image->restartInterval * xStep * yStep;

    for (uint y = 0; y < image->blockHeight; y += yStep) {
        for (uint x = 0; x < image->blockWidth; x += xStep) {
            if (restartInterval != 0 && (y * image->blockWidthReal + x) % restartInterval == 0) {
                previousDCs[0] = 0;
                previousDCs[1] = 0;
                previousDCs[2] = 0;
                skips = 0;
                bitReader.align();
            }

            for (uint i = 0; i < image->numComponents; ++i) {
                const ColorComponent& component = image->colorComponents[i];
                if (component.usedInScan) {
                    const uint vMax = luminanceOnly ? 1 : component.verticalSamplingFactor;
                    const uint hMax = luminanceOnly ? 1 : component.horizontalSamplingFactor;
                    for (uint v = 0; v < vMax; ++v) {
                        for (uint h = 0; h < hMax; ++h) {
                            if (!decodeBlockComponent(
                                    image,
                                    bitReader,
                                    image->blocks[(y + v) * image->blockWidthReal + (x + h)][i],
                                    previousDCs[i],
                                    skips,
                                    image->huffmanDCTables[component.huffmanDCTableID],
                                    image->huffmanACTables[component.huffmanACTableID])) {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
leaq 0x6c(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x2090
movl $0x0, 0x68(%rsp)
movq 0x78(%rsp), %rax
movzbl 0x2072(%rax), %ecx
xorl %eax, %eax
cmpl $0x1, %ecx
movb %al, 0x2f(%rsp)
jne 0x4a33
movq 0x78(%rsp), %rax
movb 0x2056(%rax), %al
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
andb $0x1, %al
movb %al, 0x67(%rsp)
testb $0x1, 0x67(%rsp)
je 0x4a4f
movl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x4a5f
movq 0x78(%rsp), %rax
movzbl 0x209d(%rax), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x60(%rsp)
testb $0x1, 0x67(%rsp)
je 0x4a79
movl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x4a89
movq 0x78(%rsp), %rax
movzbl 0x209c(%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x78(%rsp), %rax
movl 0x2078(%rax), %eax
imull 0x5c(%rsp), %eax
imull 0x60(%rsp), %eax
movl %eax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
movq 0x78(%rsp), %rcx
cmpl 0x208c(%rcx), %eax
jae 0x4d1d
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movq 0x78(%rsp), %rcx
cmpl 0x2090(%rcx), %eax
jae 0x4d0a
cmpl $0x0, 0x58(%rsp)
je 0x4b37
movl 0x54(%rsp), %eax
movq 0x78(%rsp), %rcx
imull 0x2098(%rcx), %eax
addl 0x50(%rsp), %eax
xorl %edx, %edx
divl 0x58(%rsp)
cmpl $0x0, %edx
jne 0x4b37
movl $0x0, 0x6c(%rsp)
movl $0x0, 0x70(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x68(%rsp)
movq 0x80(%rsp), %rdi
callq 0x8140
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x78(%rsp), %rcx
movzbl 0x2070(%rcx), %ecx
cmpl %ecx, %eax
jae 0x4cf7
movq 0x78(%rsp), %rax
addq $0x2050, %rax           # imm = 0x2050
movl 0x4c(%rsp), %ecx
imulq $0x7, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
testb $0x1, 0x6(%rax)
je 0x4ce5
testb $0x1, 0x67(%rsp)
je 0x4b93
movl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x4ba0
movq 0x40(%rsp), %rax
movzbl 0x1(%rax), %eax
movl %eax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x3c(%rsp)
testb $0x1, 0x67(%rsp)
je 0x4bba
movl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x4bc6
movq 0x40(%rsp), %rax
movzbl (%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jae 0x4ce3
movl $0x0, 0x30(%rsp)
movl 0x30(%rsp), %eax
cmpl 0x38(%rsp), %eax
jae 0x4cd1
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rax
movq 0x2080(%rax), %rdi
movl 0x54(%rsp), %eax
addl 0x34(%rsp), %eax
movq 0x78(%rsp), %rcx
imull 0x2098(%rcx), %eax
movl 0x50(%rsp), %ecx
addl 0x30(%rsp), %ecx
addl %ecx, %eax
movl %eax, %eax
imulq $0x300, %rax, %rax      # imm = 0x300
addq %rax, %rdi
movl 0x4c(%rsp), %esi
callq 0x8150
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
movl 0x4c(%rsp), %eax
leaq 0x6c(%rsp), %rcx
shlq $0x2, %rax
addq %rax, %rcx
movq 0x78(%rsp), %r9
addq $0x410, %r9             # imm = 0x410
movq 0x40(%rsp), %rax
movzbl 0x3(%rax), %eax
imulq $0x388, %rax, %rax      # imm = 0x388
addq %rax, %r9
movq 0x78(%rsp), %rax
addq $0x1230, %rax           # imm = 0x1230
movq 0x40(%rsp), %r8
movzbl 0x4(%r8), %r8d
imulq $0x388, %r8, %r8        # imm = 0x388
addq %r8, %rax
leaq 0x68(%rsp), %r8
movq %rax, (%rsp)
callq 0x5170
testb $0x1, %al
jne 0x4cbf
jmp 0x4d1d
jmp 0x4cc1
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x4bec
jmp 0x4cd3
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x4bd6
jmp 0x4ce5
jmp 0x4ce7
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x4b3f
jmp 0x4cf9
movl 0x5c(%rsp), %eax
addl 0x50(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x4acf
jmp 0x4d0c
movl 0x60(%rsp), %eax
addl 0x54(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x4ab2
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)