// File: /workspace/github_cpp_makefile_star2_O2/qhan1028[P]JPEG-Decoder/jpeg-decoder.cpp
// Function: process_SOF0(std::basic_ifstream<char, std::char_traits<char>>&, sof&)
bool process_SOF0(ifstream& jpegfile, sof_t& sof) {
    char c, c1, c2;

    /* SOF length */
    jpegfile.read(&c1, 1);
    jpegfile.read(&c2, 1);

    if (debug)
        cout << "[SOF0] len = " << to_num(c1, c2) << endl;

    /* precision */
    jpegfile.read(&c, 1);
    sof.P = to_uint(c);

    /* height */
    jpegfile.read(&c1, 1);
    jpegfile.read(&c2, 1);
    sof.Y = to_num(c1, c2);

    /* width */
    jpegfile.read(&c1, 1);
    jpegfile.read(&c2, 1);
    sof.X = to_num(c1, c2);

    /* number of channels */
    jpegfile.read(&c, 1);
    sof.N = to_uint(c) + 1; // id starts from 1 to 3, not from 0

    sof.hvq = new hvq_t[sof.N];
    sof.hmax = 0;
    sof.vmax = 0;

    for (unsigned int i = 1; i < sof.N; i++) { // channel id starts from 1
        /* channel id */
        jpegfile.read(&c, 1);
        unsigned int ci = to_uint(c);

        /* sample factors */
        jpegfile.read(&c, 1);
        c = to_uint(c);
        unsigned int h = c >> 4;
        unsigned int v = c & 0x0F;
        sof.hvq[ci].H = h;
        sof.hvq[ci].V = v;

        /* qtable id */
        jpegfile.read(&c, 1);
        sof.hvq[ci].Q = to_uint(c);

        /* update max value */
        if (h > sof.hmax)
            sof.hmax = h;
        if (v > sof.vmax)
            sof.vmax = v;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x2b(%rbp), %rsi
pushq $0x1
popq %r15
movq %r15, %rdx
callq 0x21f0
leaq -0x2a(%rbp), %rsi
movq %r14, -0x38(%rbp)
movq %r14, %rdi
movq %r15, %rdx
callq 0x21f0
cmpb $0x1, 0x64c6(%rip)      # 0x91da
jne 0x2d47
movq 0x62b3(%rip), %rdi      # 0x8fd0
leaq 0x3461(%rip), %rsi      # 0x6185
callq 0x2190
movzbl -0x2b(%rbp), %esi
movzbl -0x2a(%rbp), %ecx
shll $0x8, %esi
orq %rcx, %rsi
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x20e0
leaq -0x29(%rbp), %r15
pushq $0x1
popq %r12
movq -0x38(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21f0
movzbl (%r15), %eax
movl %eax, (%rbx)
leaq -0x2b(%rbp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21f0
leaq -0x2a(%rbp), %r13
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x21f0
movzbl (%r15), %eax
movzbl (%r13), %ecx
shll $0x8, %eax
orl %ecx, %eax
movl %eax, 0x4(%rbx)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21f0
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x21f0
movzbl (%r15), %eax
movzbl (%r13), %ecx
shll $0x8, %eax
orl %ecx, %eax
movl %eax, 0x8(%rbx)
movq %r14, %rdi
leaq -0x29(%rbp), %r14
movq %r14, %rsi
movq %r12, %rdx
callq 0x21f0
movzbl (%r14), %r15d
incq %r15
movl %r15d, 0xc(%rbx)
imulq $0xc, %r15, %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
andq $0x0, 0x18(%rbx)
pushq $0x1
popq %rax
cmpl %r15d, %eax
jae 0x2e99
movl %eax, -0x30(%rbp)
movq %rbx, %r13
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %r15
movq %r15, %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
callq 0x21f0
movzbl -0x29(%rbp), %r14d
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21f0
movsbl -0x29(%rbp), %r15d
movl %r15d, %r12d
sarl $0x4, %r12d
andl $0xf, %r15d
movq 0x10(%r13), %rax
imulq $0xc, %r14, %r14
movl %r12d, (%rax,%r14)
movl %r15d, 0x4(%rax,%r14)
movq %rbx, %rdi
movq %r13, %rbx
leaq -0x29(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x21f0
movzbl -0x29(%rbp), %eax
movq 0x10(%r13), %rcx
movl %eax, 0x8(%rcx,%r14)
cmpl 0x18(%r13), %r12d
jbe 0x2e81
movl %r12d, 0x18(%rbx)
cmpl 0x1c(%rbx), %r15d
movl -0x30(%rbp), %eax
jbe 0x2e8e
movl %r15d, 0x1c(%rbx)
incl %eax
movl 0xc(%rbx), %r15d
jmp 0x2dfb
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq