// File: dannye[P]jed/src/decoder.cpp
// Function: main
int main(int argc, char** argv) {
    // validate arguments
    if (argc < 2) {
        std::cout << "Error - Invalid arguments\n";
        return 1;
    }

    for (int i = 1; i < argc; ++i) {
        const std::string filename(argv[i]);

        // read image
        JPGImage* image = readJPG(filename);
        // validate image
        if (image == nullptr) {
            continue;
        }
        if (image->blocks == nullptr) {
            delete image;
            continue;
        }
        if (image->valid == false) {
            delete[] image->blocks;
            delete image;
            continue;
        }

        // dequantize DCT coefficients
        dequantize(image);

        // Inverse Discrete Cosine Transform
        inverseDCT(image);

        // color conversion
        YCbCrToRGB(image);

        // write BMP file
        const std::size_t pos = filename.find_last_of('.');
        const std::string outFilename = (pos == std::string::npos) ?
            (filename + ".bmp") :
            (filename.substr(0, pos) + ".bmp");
        writeBMP(image, outFilename);

        delete[] image->blocks;
        delete image;
    }
    return 0;
}

// Assembly:
subq $0xf8, %rsp
movl $0x0, 0xf4(%rsp)
movl %edi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
cmpl $0x2, 0xf0(%rsp)
jge 0x768e
movq 0x4966(%rip), %rdi      # 0xbfd8
leaq 0x23d3(%rip), %rsi      # 0x9a4c
callq 0x2110
movl $0x1, 0xf4(%rsp)
jmp 0x79fd
movl $0x1, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jge 0x79f2
movq 0xe8(%rsp), %rax
movslq 0xe4(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
leaq 0xbf(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x2230
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0x21c0
jmp 0x76f1
leaq 0xbf(%rsp), %rdi
callq 0x2140
leaq 0xc0(%rsp), %rdi
callq 0x4d30
movq %rax, 0x30(%rsp)
jmp 0x7712
movq 0x30(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x7779
movl $0x4, 0x9c(%rsp)
jmp 0x7968
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xbf(%rsp), %rdi
callq 0x2140
jmp 0x7a0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x79e3
movq 0xa0(%rsp), %rax
cmpq $0x0, 0x2080(%rax)
jne 0x77bd
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x77ad
movq 0x28(%rsp), %rdi
movl $0x20a0, %esi           # imm = 0x20A0
callq 0x2120
movl $0x4, 0x9c(%rsp)
jmp 0x7968
movq 0xa0(%rsp), %rax
movb 0x2088(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x782b
movq 0xa0(%rsp), %rax
movq 0x2080(%rax), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x77f9
movq 0x20(%rsp), %rdi
callq 0x2190
movq 0xa0(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x781b
movq 0x18(%rsp), %rdi
movl $0x20a0, %esi           # imm = 0x20A0
callq 0x2120
movl $0x4, 0x9c(%rsp)
jmp 0x7968
movq 0xa0(%rsp), %rdi
callq 0x5eb0
jmp 0x783a
movq 0xa0(%rsp), %rdi
callq 0x6c20
jmp 0x7849
movq 0xa0(%rsp), %rdi
callq 0x6fc0
leaq 0xc0(%rsp), %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x2260
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movb $0x0, 0x4f(%rsp)
cmpq $-0x1, %rax
jne 0x78a7
leaq 0x21d6(%rip), %rdx      # 0x9a67
leaq 0x70(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x81f0
jmp 0x78a5
jmp 0x78e6
movq 0x90(%rsp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq 0x50(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2130
jmp 0x78c7
movb $0x1, 0x4f(%rsp)
leaq 0x2194(%rip), %rdx      # 0x9a67
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x8280
jmp 0x78e4
jmp 0x78e6
testb $0x1, 0x4f(%rsp)
jne 0x78ef
jmp 0x78f9
leaq 0x50(%rsp), %rdi
callq 0x20b0
movq 0xa0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x7200
jmp 0x790d
movq 0xa0(%rsp), %rax
movq 0x2080(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x7931
movq 0x10(%rsp), %rdi
callq 0x2190
movq 0xa0(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x7953
movq 0x8(%rsp), %rdi
movl $0x20a0, %esi           # imm = 0x20A0
callq 0x2120
leaq 0x70(%rsp), %rdi
callq 0x20b0
movl $0x0, 0x9c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x20b0
movl 0x9c(%rsp), %eax
testl %eax, %eax
je 0x7984
jmp 0x7982
jmp 0x7986
jmp 0x7986
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x7699
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
testb $0x1, 0x4f(%rsp)
jne 0x79b9
jmp 0x79c3
leaq 0x50(%rsp), %rdi
callq 0x20b0
jmp 0x79e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x20b0
leaq 0xc0(%rsp), %rdi
callq 0x20b0
jmp 0x7a0c
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
movq 0xb0(%rsp), %rdi
callq 0x2220
nopl (%rax)