// File: OpenFirework[P]MyJPEG_Decoder/./jpeg/jpeg_decoder.cpp
// Function: MyJPEG::jpegDecoder::imgread(char const*, int&, int&, unsigned char*&)
int MyJPEG::jpegDecoder::imgread(const char *filename, int &width, int &height, byte *&bgrdata) {
    //读文件
    std::ifstream infile;
    infile.open(filename, std::ios::in|std::ios::binary);

    infile.seekg(0, infile.end);
    int jpgdatalength = infile.tellg();
    infile.seekg(0, infile.beg);
    std::cout << "encoded jpeg data length is " << jpgdatalength<<"\n";

    byte *decoded = new byte[jpgdatalength]; //decode会不断变换，拷贝一份到copyptr，然后删除
    byte *copyptr = decoded;
    infile.read((char*)decoded,jpgdatalength);  //读取编码的jpeg文件数据到内存中
    infile.close();

    int ret = readinfo(decoded, bgrdata, width, height);
    width = bmpwidth;
    height = bmpheight;
    if(copyptr != nullptr) {
        delete[] copyptr;
    }
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2c0, %rsp            # imm = 0x2C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x278(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2170
movq -0x10(%rbp), %rax
movq %rax, -0x270(%rbp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x4a50
movl %eax, -0x268(%rbp)
jmp 0x3b28
movl -0x268(%rbp), %edx
movq -0x270(%rbp), %rsi
leaq -0x230(%rbp), %rdi
callq 0x20d0
jmp 0x3b43
xorl %eax, %eax
movl %eax, %esi
leaq -0x230(%rbp), %rdi
movl $0x2, %edx
callq 0x21c0
jmp 0x3b5a
leaq -0x230(%rbp), %rdi
callq 0x2260
movq %rdx, -0x288(%rbp)
movq %rax, -0x280(%rbp)
jmp 0x3b76
movq -0x288(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, -0x250(%rbp)
movq %rax, -0x248(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x4a70
movq %rax, -0x290(%rbp)
jmp 0x3ba7
movq -0x290(%rbp), %rax
movl %eax, -0x240(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x230(%rbp), %rdi
callq 0x21c0
jmp 0x3bc6
movq 0x7403(%rip), %rdi      # 0xafd0
leaq 0x45d0(%rip), %rsi      # 0x81a4
callq 0x2130
movq %rax, -0x298(%rbp)
jmp 0x3be2
movq -0x298(%rbp), %rdi
movl -0x240(%rbp), %esi
callq 0x2250
movq %rax, -0x2a0(%rbp)
jmp 0x3bfd
movq -0x2a0(%rbp), %rdi
leaq 0x4d9f(%rip), %rsi      # 0x89aa
callq 0x2130
jmp 0x3c12
movslq -0x240(%rbp), %rdi
callq 0x2030
movq %rax, -0x2a8(%rbp)
jmp 0x3c27
movq -0x2a8(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x258(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x258(%rbp), %rsi
movslq -0x240(%rbp), %rdx
leaq -0x230(%rbp), %rdi
callq 0x2190
jmp 0x3c5f
leaq -0x230(%rbp), %rdi
callq 0x2060
jmp 0x3c6d
movq -0x278(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq -0x258(%rbp), %rsi
callq 0x2680
movl %eax, -0x2ac(%rbp)
jmp 0x3c94
movl -0x2ac(%rbp), %eax
movl %eax, -0x264(%rbp)
movl 0x83ba(%rip), %ecx      # 0xc060
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl 0x83b2(%rip), %ecx      # 0xc064
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x260(%rbp)
je 0x3d04
movq -0x260(%rbp), %rax
movq %rax, -0x2b8(%rbp)
cmpq $0x0, %rax
je 0x3ce2
movq -0x2b8(%rbp), %rdi
callq 0x21a0
jmp 0x3d04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2070
jmp 0x3d2b
movl -0x264(%rbp), %eax
movl %eax, -0x2bc(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2070
movl -0x2bc(%rbp), %eax
addq $0x2c0, %rsp            # imm = 0x2C0
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x2280
nopw (%rax,%rax)