// File: /workspace/github_cpp_makefile_star2_O2/rapirent[P]JPEG/decoder.c
// Function: main
int main(int argc,char* argv[])
{
    if (argc != 2  && argc != 3) {
        printf("[ERROR]:\nusage: ./decoder <input_file> [<output_file_name>]");
        exit(1);
    }
    FILE* fp;
    if ((fp = fopen(argv[1], "r")) == NULL) {
        printf("%s can't be opened\n", argv[1]);
        exit(1);
    }
    const char* fwrite = argc ==3 ? argv[2] : "image.bmp";

    byte h;
    byte l;
    bool b_SOI = false;
    bool b_EOI = false;
    init_cos_cache();
    while (fread(&h,1, 1,fp)) {
        fread(&l,1, 1,fp);
        if (h == 0xff) {
            if (!b_SOI && l == SOI) {
                b_SOI = true;
            }
            assert(b_SOI == true);
            switch(l) {
            case DQT:
                read_qt(fp);
                break;
            case SOF0:
                read_frame(fp);
                break;
            case DHT:
                read_ht(fp);
                break;
            case SOS:
                read_sos(fp);
                calculate_mcu(fp,fwrite);
                break;
            case APP0:
            case APP1:
            case APP2:
            case APP3:
            case APP4:
            case APP5:
            case APP6:
            case APP7:
            case APP8:
            case APP9:
            case APP10:
            case APP11:
            case APP12:
            case APP13:
            case APP14:
            case APP15:
                break;
            case EOI:
                b_EOI = true;
                break;
            }
        }
    }
    assert(b_EOI==true);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r15d
leal -0x4(%r15), %eax
cmpl $-0x3, %eax
jbe 0x65ee
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x13f5(%rip), %rsi      # 0x78cd
callq 0x5150
testq %rax, %rax
je 0x65fe
movq %rax, %rbx
cmpl $0x3, %r15d
jne 0x64f5
movq 0x10(%r14), %rax
jmp 0x64fc
leaq 0x1486(%rip), %rax      # 0x7982
movq %rax, 0x10(%rsp)
callq 0x5417
xorl %ebp, %ebp
leaq 0xf(%rsp), %r15
pushq $0x1
popq %r12
leaq 0xe(%rsp), %r13
xorl %r14d, %r14d
movq %r15, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x51c0
testq %rax, %rax
je 0x65d7
movq %r13, %rdi
movq %r12, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x51c0
cmpb $-0x1, 0xf(%rsp)
jne 0x6519
movb 0xe(%rsp), %al
cmpb $-0x28, %al
movzbl %bpl, %ecx
pushq $0x1
popq %rbp
cmovnel %ecx, %ebp
testb $0x1, %cl
cmovnel %ecx, %ebp
testb $0x1, %bpl
je 0x6637
movzbl %al, %eax
cmpl $0xc0, %eax
je 0x65ca
cmpl $0xc4, %eax
je 0x6596
cmpl $0xda, %eax
je 0x65b0
cmpl $0xdb, %eax
je 0x65a3
cmpl $0xd9, %eax
jne 0x6519
movb $0x1, %r14b
jmp 0x6519
movq %rbx, %rdi
callq 0x5620
jmp 0x6519
movq %rbx, %rdi
callq 0x545d
jmp 0x6519
movq %rbx, %rdi
callq 0x578d
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x5d45
jmp 0x6519
movq %rbx, %rdi
callq 0x5503
jmp 0x6519
testb $0x1, %r14b
je 0x6618
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x133d(%rip), %rdi      # 0x7932
xorl %eax, %eax
callq 0x5030
jmp 0x6610
movq 0x8(%r14), %rsi
leaq 0x1365(%rip), %rdi      # 0x796e
xorl %eax, %eax
callq 0x5030
pushq $0x1
popq %rdi
callq 0x5170
leaq 0x1392(%rip), %rdi      # 0x79b1
leaq 0x12c2(%rip), %rsi      # 0x78e8
leaq 0x136d(%rip), %rcx      # 0x799a
movl $0x1c7, %edx            # imm = 0x1C7
callq 0x50c0
leaq 0x134e(%rip), %rdi      # 0x798c
leaq 0x12a3(%rip), %rsi      # 0x78e8
leaq 0x134e(%rip), %rcx      # 0x799a
movl $0x1a1, %edx            # imm = 0x1A1
callq 0x50c0