// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movl %edi, %r15d
leal -0x4(%r15), %eax
cmpl $-0x3, %eax
jbe 0x6883
movq %rsi, %r14
movq 0x8(%rsi), %rdi
leaq 0x11ff(%rip), %rsi      # 0x78cd
callq 0x5140
testq %rax, %rax
je 0x6893
movq %rax, %rbx
cmpl $0x3, %r15d
jne 0x66eb
movq 0x10(%r14), %r14
jmp 0x66f2
leaq 0x1290(%rip), %r14      # 0x7982
leaq 0x4b27(%rip), %r15      # 0xb220
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
mulsd 0x8fc(%rip), %xmm0      # 0x7008
mulsd 0x8fc(%rip), %xmm0      # 0x7010
callq 0x5210
movsd %xmm0, (%r15)
incq %r12
addq $0x8, %r15
cmpq $0xc8, %r12
jne 0x66fc
leaq 0x6(%rsp), %rdi
movl $0x1, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x51b0
testq %rax, %rax
je 0x6864
xorl %ebp, %ebp
leaq 0x7(%rsp), %r15
leaq 0x6(%rsp), %r12
xorl %r13d, %r13d
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x51b0
cmpb $-0x1, 0x6(%rsp)
je 0x6798
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbx, %rcx
callq 0x51b0
testq %rax, %rax
jne 0x675d
jmp 0x682e
movb 0x7(%rsp), %al
cmpb $-0x28, %al
movzbl %r13b, %ecx
movl $0x1, %r13d
cmovnel %ecx, %r13d
testb $0x1, %cl
cmovnel %ecx, %r13d
testb $0x1, %r13b
je 0x6845
movzbl %al, %eax
cmpl $0xd8, %eax
jle 0x67e6
cmpl $0xd9, %eax
je 0x6801
cmpl $0xda, %eax
je 0x6809
cmpl $0xdb, %eax
jne 0x6779
movq %rbx, %rdi
callq 0x5470
jmp 0x6779
cmpl $0xc0, %eax
je 0x6821
cmpl $0xc4, %eax
jne 0x6779
movq %rbx, %rdi
callq 0x5662
jmp 0x6779
movb $0x1, %bpl
jmp 0x6779
movq %rbx, %rdi
callq 0x57fa
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5e53
jmp 0x6779
movq %rbx, %rdi
callq 0x552a
jmp 0x6779
testb $0x1, %bpl
je 0x6864
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1140(%rip), %rdi      # 0x798c
leaq 0x1095(%rip), %rsi      # 0x78e8
leaq 0x1140(%rip), %rcx      # 0x799a
movl $0x1a1, %edx            # imm = 0x1A1
callq 0x5090
leaq 0x1146(%rip), %rdi      # 0x79b1
leaq 0x1076(%rip), %rsi      # 0x78e8
leaq 0x1121(%rip), %rcx      # 0x799a
movl $0x1c7, %edx            # imm = 0x1C7
callq 0x5090
leaq 0x10a8(%rip), %rdi      # 0x7932
xorl %eax, %eax
callq 0x5040
jmp 0x68a5
movq 0x8(%r14), %rsi
leaq 0x10d0(%rip), %rdi      # 0x796e
xorl %eax, %eax
callq 0x5040
movl $0x1, %edi
callq 0x5160
nop