// File: /workspace/github_cpp_makefile_star2_O2/qhan1028[P]JPEG-Decoder/jpeg-decoder.cpp
// Function: main
int main(int argc, char* argv[]) {
    if (argc < 2) {
        cout << "[Error] usage: ./jpeg-decoder [filename].jpg" << endl;
        return -1;
    }

    /* parse filename */
    string filename;
    for (int i = 0; i < argc; i++) {
        if (strcmp(argv[i], "--debug") == 0)
            debug = true;
        else
            filename = argv[i];
    }

    /* open jpeg */
    ifstream jpegfile;
    jpegfile.open(filename, ifstream::binary);
    if (!jpegfile.is_open()) {
        cout << "[Error] fail to open file: " << filename << endl;
        return -2;
    }

    /* read first tag */
    if (process_SOI(jpegfile))
        return -3;

    /* initialize */
    qtable_t qtable = init_qtable();
    sof_t sof;
    htable_t* htable = init_htable();
    sos_t sos;
    image_t rgb;

    char c1, c2;
    bool eoi = false; // end of image

    while(!eoi) {
        jpegfile.read(&c1, 1);
        jpegfile.read(&c2, 1);

        if (to_uint(c1) == TAG && to_uint(c2) != TAG) {

            unsigned int opt = to_uint(c2);

            switch(opt) {
                case DQT:
                    process_DQT(jpegfile, qtable);
                    break;
                
                case SOF0:
                    process_SOF0(jpegfile, sof);
                    break;
                
                case DHT:
                    process_DHT(jpegfile, htable);
                    break;
                
                case SOS:
                    process_SOS(jpegfile, sos);
                    if (debug) {
                        print_DQT(qtable);
                        print_SOF(sof);
                        print_DHT(htable);
                        print_SOS(sos);
                    }
                    rgb = process_scan(jpegfile, qtable, sof, htable, sos);
                    break;
                
                case EOI:
                    process_EOI(eoi);
                    break;
                
                default:
                    process_other(jpegfile, opt);
            }
        }
    }

    jpegfile.close();

    /* output bitmap */
    to_bitmap(filename, rgb, sof);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
cmpl $0x1, %edi
jg 0x42ae
movq 0x4d3e(%rip), %rdi      # 0x8fd0
leaq 0x207d(%rip), %rsi      # 0x6316
callq 0x2190
movq %rax, %rdi
callq 0x20e0
pushq $-0x1
popq %rbx
jmp 0x451a
movq %rsi, %rbx
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl %edi, %r13d
xorl %r15d, %r15d
leaq 0x2075(%rip), %r14      # 0x6343
cmpq %r15, %r13
je 0x4300
movq (%rbx,%r15,8), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2250
testl %eax, %eax
je 0x42f4
leaq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x22f0
jmp 0x42fb
movb $0x1, 0x4edf(%rip)      # 0x91da
incq %r15
jmp 0x42ce
leaq -0x2d0(%rbp), %rdi
callq 0x21e0
leaq -0x2d0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x2140
leaq -0x258(%rbp), %rdi
callq 0x2070
testb %al, %al
je 0x4343
leaq -0x2d0(%rbp), %rdi
callq 0x28f6
testb %al, %al
je 0x4372
pushq $-0x3
jmp 0x436c
movq 0x4c86(%rip), %rdi      # 0x8fd0
leaq 0x1ffa(%rip), %rsi      # 0x634b
callq 0x2190
leaq -0x68(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
pushq $-0x2
popq %rbx
jmp 0x4505
callq 0x297c
movq %rax, -0x40(%rbp)
callq 0x2eaa
movq %rax, -0x38(%rbp)
leaq -0x29(%rbp), %rax
movb $0x0, (%rax)
xorl %r14d, %r14d
leaq -0x2d0(%rbp), %r12
leaq -0x2b(%rbp), %r13
pushq $0x1
popq %r15
leaq -0x2a(%rbp), %rbx
testb $0x1, %r14b
jne 0x44c4
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x21f0
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x21f0
cmpb $-0x1, -0x2b(%rbp)
jne 0x43a1
movzbl -0x2a(%rbp), %esi
cmpl $0xc0, %esi
je 0x44b0
cmpl $0xc4, %esi
je 0x449f
cmpl $0xd9, %esi
je 0x4413
cmpl $0xda, %esi
je 0x4436
cmpl $0xdb, %esi
je 0x4425
cmpl $0xff, %esi
je 0x43a1
movq %r12, %rdi
callq 0x41a3
jmp 0x43a1
leaq -0x29(%rbp), %rdi
callq 0x416a
movb -0x29(%rbp), %r14b
jmp 0x43a1
movq %r12, %rdi
movq -0x40(%rbp), %rsi
callq 0x2aa2
jmp 0x43a1
movq %r12, %rdi
leaq -0xc8(%rbp), %rsi
callq 0x3264
cmpb $0x1, 0x4d8e(%rip)      # 0x91da
jne 0x4478
movq -0x40(%rbp), %rdi
callq 0x29bd
leaq -0xa8(%rbp), %rdi
callq 0x2b82
movq -0x38(%rbp), %rdi
callq 0x2ef6
leaq -0xc8(%rbp), %rdi
callq 0x3113
movq %r12, %rdi
movq -0x40(%rbp), %rsi
leaq -0xa8(%rbp), %rdx
movq -0x38(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0x365e
movq %rax, -0x48(%rbp)
jmp 0x43a1
movq %r12, %rdi
movq -0x38(%rbp), %rsi
callq 0x2fab
jmp 0x43a1
movq %r12, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x2cd3
jmp 0x43a1
leaq -0x2d0(%rbp), %rdi
callq 0x2060
leaq -0x88(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2240
leaq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %rsi
callq 0x3512
leaq -0x88(%rbp), %rdi
callq 0x20d0
xorl %ebx, %ebx
leaq -0x2d0(%rbp), %rdi
callq 0x2080
leaq -0x68(%rbp), %rdi
callq 0x20d0
movl %ebx, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x20d0
jmp 0x454e
jmp 0x454b
jmp 0x454b
jmp 0x454b
jmp 0x455c
jmp 0x454b
jmp 0x454b
movq %rax, %rbx
leaq -0x2d0(%rbp), %rdi
callq 0x2080
jmp 0x455f
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x22d0