// File: dannye[P]jed/src/decoder.cpp
// Function: readJPG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
JPGImage* readJPG(const std::string& filename) {
    // open file
    std::cout << "Reading " << filename << "...\n";
    BitReader bitReader(filename);
    if (!bitReader.hasBits()) {
        std::cout << "Error - Error opening input file\n";
        return nullptr;
    }

    JPGImage* image = new (std::nothrow) JPGImage;
    if (image == nullptr) {
        std::cout << "Error - Memory error\n";
        return nullptr;
    }

    readFrameHeader(bitReader, image);

    if (!image->valid) {
        return image;
    }

    printFrameInfo(image);

    image->blocks = new (std::nothrow) Block[image->blockHeightReal * image->blockWidthReal];
    if (image->blocks == nullptr) {
        std::cout << "Error - Memory error\n";
        image->valid = false;
        return image;
    }

    readScans(bitReader, image);

    return image;
}

// Assembly:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, 0x2a8(%rsp)
movq 0x7292(%rip), %rdi      # 0xbfd8
leaq 0x4b94(%rip), %rsi      # 0x98e1
callq 0x2110
movq %rax, %rdi
movq 0x2a8(%rsp), %rsi
callq 0x2100
movq %rax, %rdi
leaq 0x4b7e(%rip), %rsi      # 0x98ea
callq 0x2110
movq 0x2a8(%rsp), %rsi
leaq 0x98(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x7b10
movq 0x50(%rsp), %rdi
callq 0x7ae0
movb %al, 0x5e(%rsp)
jmp 0x4d9b
movb 0x5e(%rsp), %al
testb $0x1, %al
jne 0x4dfc
jmp 0x4da5
movq 0x722c(%rip), %rdi      # 0xbfd8
leaq 0x4b3c(%rip), %rsi      # 0x98ef
callq 0x2110
jmp 0x4dba
movq $0x0, 0x2b0(%rsp)
movl $0x1, 0x88(%rsp)
jmp 0x5063
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x7e60
jmp 0x5080
movl $0x20a0, %edi           # imm = 0x20A0
movq 0x71b8(%rip), %rsi      # 0xbfc0
callq 0x2280
movq %rax, %rcx
movq %rcx, 0x40(%rsp)
movb $0x0, 0x77(%rsp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, 0x48(%rsp)
je 0x4e45
movq 0x40(%rsp), %rdi
movq %rdi, 0x78(%rsp)
movb $0x1, 0x77(%rsp)
callq 0x7ba0
movq 0x40(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x4e8e
movq 0x7174(%rip), %rdi      # 0xbfd8
leaq 0x4aa6(%rip), %rsi      # 0x9911
callq 0x2110
jmp 0x4e72
movq $0x0, 0x2b0(%rsp)
movl $0x1, 0x88(%rsp)
jmp 0x5063
movq 0x80(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x4310
jmp 0x4ea5
movq 0x80(%rsp), %rax
testb $0x1, 0x2088(%rax)
jne 0x4ed6
movq 0x80(%rsp), %rax
movq %rax, 0x2b0(%rsp)
movl $0x1, 0x88(%rsp)
jmp 0x5063
movq 0x80(%rsp), %rdi
callq 0x39d0
jmp 0x4ee5
movq 0x80(%rsp), %rax
movl 0x2094(%rax), %eax
movq 0x80(%rsp), %rcx
imull 0x2098(%rcx), %eax
movl %eax, %eax
movq %rax, 0x20(%rsp)
movl $0x300, %ecx            # imm = 0x300
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x28(%rsp)
movq 0x708f(%rip), %rsi      # 0xbfc0
callq 0x2210
movq %rax, %rcx
movq %rcx, 0x30(%rsp)
movb $0x0, 0x5f(%rsp)
xorl %eax, %eax
cmpq $0x0, %rcx
movq %rax, 0x38(%rsp)
je 0x4fca
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rdx
movq %rdx, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movb $0x1, 0x5f(%rsp)
cmpq $0x0, %rax
je 0x4fbe
movq 0x30(%rsp), %rax
movq 0x20(%rsp), %rcx
imulq $0x300, %rcx, %rdx      # imm = 0x300
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x7d70
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
addq $0x300, %rax            # imm = 0x300
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0x4f95
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0x4fca
movq 0x38(%rsp), %rcx
movq 0x80(%rsp), %rax
movq %rcx, 0x2080(%rax)
movq 0x80(%rsp), %rax
cmpq $0x0, 0x2080(%rax)
jne 0x5031
movq 0x6fe1(%rip), %rdi      # 0xbfd8
leaq 0x4913(%rip), %rsi      # 0x9911
callq 0x2110
jmp 0x5005
movq 0x80(%rsp), %rax
movb $0x0, 0x2088(%rax)
movq 0x80(%rsp), %rax
movq %rax, 0x2b0(%rsp)
movl $0x1, 0x88(%rsp)
jmp 0x5063
movq 0x80(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x4770
jmp 0x5048
movq 0x80(%rsp), %rax
movq %rax, 0x2b0(%rsp)
movl $0x1, 0x88(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x7e60
movq 0x2b0(%rsp), %rax
addq $0x2b8, %rsp            # imm = 0x2B8
retq
movq 0x90(%rsp), %rdi
callq 0x2220
nopl (%rax)