// File: /workspace/github_cpp_makefile_star2_O3/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, -0x38(%rbp)
movq %rdi, %rbx
leaq -0x2b(%rbp), %rsi
movl $0x1, %edx
callq 0x2220
leaq -0x2a(%rbp), %rsi
movl $0x1, %edx
movq %rbx, -0x40(%rbp)
movq %rbx, %rdi
callq 0x2220
cmpb $0x1, 0x624c(%rip)      # 0x91fa
jne 0x300b
movq 0x6019(%rip), %r15      # 0x8fd0
leaq 0x31c7(%rip), %rsi      # 0x6185
movl $0xd, %edx
movq %r15, %rdi
callq 0x2210
movzbl -0x2b(%rbp), %esi
movzbl -0x2a(%rbp), %eax
shll $0x8, %esi
orq %rax, %rsi
movq %r15, %rdi
callq 0x2110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2120
leaq -0x29(%rbp), %r15
movl $0x1, %edx
movq -0x40(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x2220
movzbl (%r15), %eax
movq -0x38(%rbp), %rbx
movl %eax, (%rbx)
leaq -0x2b(%rbp), %r12
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2220
leaq -0x2a(%rbp), %r13
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2220
movzbl (%r12), %eax
movzbl (%r13), %ecx
shll $0x8, %eax
orl %ecx, %eax
movl %eax, 0x4(%rbx)
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2220
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2220
movzbl (%r12), %eax
movzbl (%r13), %ecx
shll $0x8, %eax
orl %ecx, %eax
movl %eax, 0x8(%rbx)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2220
movzbl (%r15), %r14d
leal 0x1(%r14), %eax
movl %eax, 0xc(%rbx)
leal 0x4(,%r14,4), %eax
leaq (%rax,%rax,2), %rdi
callq 0x2030
movq %rax, 0x10(%rbx)
movq $0x0, 0x18(%rbx)
testq %r14, %r14
je 0x3179
movl $0x1, %r12d
movl $0x1, %edx
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
leaq -0x29(%rbp), %r14
movq %r14, %rsi
callq 0x2220
movzbl -0x29(%rbp), %r15d
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2220
movsbl -0x29(%rbp), %r13d
movl %r13d, %r14d
sarl $0x4, %r14d
andl $0xf, %r13d
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
shll $0x2, %r15d
leaq (%r15,%r15,2), %r15
movl %r14d, (%rax,%r15)
movl %r13d, 0x4(%rax,%r15)
movl $0x1, %edx
movq %rbx, %rdi
movq -0x38(%rbp), %rbx
leaq -0x29(%rbp), %rsi
callq 0x2220
movzbl -0x29(%rbp), %eax
movq 0x10(%rbx), %rcx
movl %eax, 0x8(%rcx,%r15)
cmpl 0x18(%rbx), %r14d
jbe 0x3162
movl %r14d, 0x18(%rbx)
cmpl 0x1c(%rbx), %r13d
jbe 0x316c
movl %r13d, 0x1c(%rbx)
incl %r12d
cmpl 0xc(%rbx), %r12d
jb 0x30e0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq