// File: 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
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x12(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movq -0x8(%rbp), %rdi
leaq -0x13(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
testb $0x1, 0xcb7f(%rip)      # 0x1021a
je 0x36dd
movq 0xc92c(%rip), %rdi      # 0xffd0
leaq 0x7b0a(%rip), %rsi      # 0xb1b5
callq 0x21b0
movq %rax, -0x30(%rbp)
movb -0x12(%rbp), %al
movsbl %al, %edi
movsbl -0x13(%rbp), %esi
callq 0x61d0
movq -0x30(%rbp), %rdi
movl %eax, %esi
callq 0x2260
movq %rax, %rdi
movq 0xc8e8(%rip), %rsi      # 0xffc0
callq 0x21f0
movq -0x8(%rbp), %rdi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movsbl -0x11(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
leaq -0x12(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movq -0x8(%rbp), %rdi
leaq -0x13(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movb -0x12(%rbp), %al
movsbl %al, %edi
movsbl -0x13(%rbp), %esi
callq 0x61d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
leaq -0x12(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movq -0x8(%rbp), %rdi
leaq -0x13(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movb -0x12(%rbp), %al
movsbl %al, %edi
movsbl -0x13(%rbp), %esi
callq 0x61d0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x8(%rbp), %rdi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movsbl -0x11(%rbp), %edi
callq 0x6210
movl %eax, %ecx
addl $0x1, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
movl $0xc, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2030
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x1c(%rax)
movl $0x1, -0x18(%rbp)
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x38ec
movq -0x8(%rbp), %rdi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movsbl -0x11(%rbp), %edi
callq 0x6210
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movsbl -0x11(%rbp), %edi
callq 0x6210
movb %al, -0x11(%rbp)
movsbl -0x11(%rbp), %eax
sarl $0x4, %eax
movl %eax, -0x20(%rbp)
movsbl -0x11(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movsbl -0x11(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %edx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jbe 0x38c6
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jbe 0x38dc
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x38de
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x37ef
xorl %eax, %eax
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)