// File: qhan1028[P]JPEG-Decoder/jpeg-decoder.cpp
// Function: process_SOS(std::basic_ifstream<char, std::char_traits<char>>&, sos&)
bool process_SOS(ifstream& jpegfile, sos_t& sos) {
    unsigned int pointer = 0;
    char c, c1, c2;

    /* SOS length */
    jpegfile.read(&c1, 1); pointer++;
    jpegfile.read(&c2, 1); pointer++;
    unsigned int len = to_num(c1, c2);

    if (debug)
        cout << "[SOS] len = " << len << endl;

    while (pointer < len) {
        /* number of channels */
        jpegfile.read(&c, 1); pointer++;
        sos.ns = to_uint(c);

        sos.cda = new cda_t[sos.ns];

        /* information of color channels */
        for (unsigned int i = 0; i < sos.ns; i++) {
            jpegfile.read(&c, 1); pointer++;
            unsigned int cs = to_uint(c);

            jpegfile.read(&c, 1); pointer++;
            unsigned int tdta = to_uint(c);
            sos.cda[cs].td = tdta >> 4;
            sos.cda[cs].ta = tdta & 0x0F;
        }

        /* other information */
        jpegfile.read(&c, 1); pointer++;
        sos.ss = to_uint(c);

        jpegfile.read(&c, 1); pointer++;
        sos.se = to_uint(c);

        jpegfile.read(&c, 1); pointer++;
        unsigned int ahal = to_uint(c);
        sos.ah = ahal >> 4;
        sos.al = ahal & 0x0F;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x16(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x17(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movb -0x16(%rbp), %al
movsbl %al, %edi
movsbl -0x17(%rbp), %esi
callq 0x61d0
movl %eax, -0x1c(%rbp)
testb $0x1, 0xc304(%rip)      # 0x1021a
je 0x3f45
movq 0xc0b1(%rip), %rdi      # 0xffd0
leaq 0x7335(%rip), %rsi      # 0xb25b
callq 0x21b0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x2260
movq %rax, %rdi
movq 0xc080(%rip), %rsi      # 0xffc0
callq 0x21f0
jmp 0x3f47
movl -0x14(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x40ec
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl $0x8, %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, 0x8(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jae 0x404c
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %edx
shrl $0x4, %edx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
movl %edx, (%rax,%rcx,8)
movl -0x28(%rbp), %edx
andl $0xf, %edx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x24(%rbp), %ecx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3fb8
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
shrl $0x4, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
movl -0x2c(%rbp), %ecx
andl $0xf, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x1c(%rax)
jmp 0x3f47
xorl %eax, %eax
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)