// File: /workspace/github_cpp_makefile_star2_O1/shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::read_tga_file(char const*)
bool TGAImage::read_tga_file(const char *filename) {
    if (data) delete [] data;
    data = NULL;
    std::ifstream in;
    in.open (filename, std::ios::binary);
    if (!in.is_open()) {
        std::cerr << "can't open file " << filename << "\n";
        in.close();
        return false;
    }
    TGA_Header header;
    in.read((char *)&header, sizeof(header));
    if (!in.good()) {
        in.close();
        std::cerr << "an error occured while reading the header\n";
        return false;
    }
    width   = header.width;
    height  = header.height;
    bytespp = header.bitsperpixel>>3;
    if (width<=0 || height<=0 || (bytespp!=GRAYSCALE && bytespp!=RGB && bytespp!=RGBA)) {
        in.close();
        std::cerr << "bad bpp (or width/height) value\n";
        return false;
    }
    unsigned long nbytes = bytespp*width*height;
    data = new unsigned char[nbytes];
    if (3==header.datatypecode || 2==header.datatypecode) {
        in.read((char *)data, nbytes);
        if (!in.good()) {
            in.close();
            std::cerr << "an error occured while reading the data\n";
            return false;
        }
    } else if (10==header.datatypecode||11==header.datatypecode) {
        if (!load_rle_data(in)) {
            in.close();
            std::cerr << "an error occured while reading the data\n";
            return false;
        }
    } else {
        in.close();
        std::cerr << "unknown file format " << (int)header.datatypecode << "\n";
        return false;
    }
    if (!(header.imagedescriptor & 0x20)) {
        flip_vertically();
    }
    if (header.imagedescriptor & 0x10) {
        flip_horizontally();
    }
    std::cerr << width << "x" << height << "/" << bytespp*8 << "\n";
    in.close();
    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %rdi
testq %rdi, %rdi
je 0x3789
callq 0x2200
movq $0x0, (%rbx)
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x21c0
movq %r15, %rdi
movq %r14, %rsi
movl $0x4, %edx
callq 0x2130
leaq 0x90(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x37fd
leaq 0x18(%rsp), %rdi
leaq 0x6(%rsp), %rsi
movl $0x12, %edx
callq 0x21f0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x38(%rsp,%rax)
je 0x3839
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x2a, %edx
leaq 0x1894(%rip), %rsi      # 0x508c
jmp 0x3884
movq 0x47ec(%rip), %rbx      # 0x7ff0
leaq 0x1870(%rip), %rsi      # 0x507b
movl $0x10, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r14, %r14
je 0x3892
movq %r14, %rdi
callq 0x20e0
movq 0x47c4(%rip), %rdi      # 0x7ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x38a9
movswl 0x12(%rsp), %eax
testl %eax, %eax
setle %dil
movl %eax, 0x8(%rbx)
movzwl 0x14(%rsp), %ecx
movswl %cx, %ecx
movl %ecx, 0xc(%rbx)
movb 0x16(%rsp), %sil
sarb $0x3, %sil
movsbl %sil, %edx
movl %edx, 0x10(%rbx)
testw %cx, %cx
setle %r8b
orb %dil, %r8b
je 0x38e6
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x20, %edx
leaq 0x1833(%rip), %rsi      # 0x50b7
movq 0x4765(%rip), %rdi      # 0x7ff0
callq 0x21d0
jmp 0x38cb
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x4740(%rip), %rdi      # 0x7ff0
leaq 0x1848(%rip), %rsi      # 0x50ff
movl $0x1, %edx
callq 0x21d0
leaq 0x18(%rsp), %rdi
callq 0x2060
xorl %ebx, %ebx
leaq 0x18(%rsp), %rdi
callq 0x2080
movl %ebx, %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
cmpb $0x4, %sil
ja 0x386e
movzbl %sil, %esi
movl $0x1a, %edi
btl %esi, %edi
jae 0x386e
imull %eax, %ecx
imull %edx, %ecx
movslq %ecx, %r14
movq %r14, %rdi
callq 0x2030
movq %rax, (%rbx)
movzbl 0x8(%rsp), %ecx
andl $-0x2, %ecx
cmpl $0xa, %ecx
je 0x396a
cmpl $0x2, %ecx
jne 0x3a14
leaq 0x18(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x21f0
movq 0x18(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x38(%rsp,%rax)
je 0x397b
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x28, %edx
leaq 0x177a(%rip), %rsi      # 0x50d8
movq 0x468b(%rip), %rdi      # 0x7ff0
jmp 0x3a56
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x3a82
testb %al, %al
je 0x3948
testb $0x20, 0x17(%rsp)
jne 0x398a
movq %rbx, %rdi
callq 0x3cfe
testb $0x10, 0x17(%rsp)
je 0x3999
movq %rbx, %rdi
callq 0x3de2
movl 0x8(%rbx), %esi
movq 0x464d(%rip), %rdi      # 0x7ff0
callq 0x22d0
movq %rax, %r14
leaq 0x1764(%rip), %rsi      # 0x5116
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl 0xc(%rbx), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x1744(%rip), %rsi      # 0x5118
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl 0x10(%rbx), %esi
shll $0x3, %esi
movq %r14, %rdi
callq 0x22d0
leaq 0x1709(%rip), %rsi      # 0x50ff
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x2060
jmp 0x38cd
leaq 0x18(%rsp), %rdi
callq 0x2060
movq 0x45cb(%rip), %rdi      # 0x7ff0
leaq 0x16d5(%rip), %rsi      # 0x5101
movl $0x14, %edx
callq 0x21d0
movsbl 0x8(%rsp), %esi
movq 0x45ae(%rip), %rdi      # 0x7ff0
callq 0x22d0
movq %rax, %rdi
movl $0x1, %edx
leaq 0x16a9(%rip), %rsi      # 0x50ff
callq 0x21d0
jmp 0x38cb
jmp 0x3a64
jmp 0x3a64
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x2bb2
nop