// File: /workspace/github_cpp_makefile_star2_O3/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 0x3799
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 0x380d
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 0x3849
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x2a, %edx
leaq 0x1884(%rip), %rsi      # 0x508c
jmp 0x3894
movq 0x47dc(%rip), %rbx      # 0x7ff0
leaq 0x1860(%rip), %rsi      # 0x507b
movl $0x10, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r14, %r14
je 0x38a2
movq %r14, %rdi
callq 0x20e0
movq 0x47b4(%rip), %rdi      # 0x7ff0
movq %r14, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x38b9
movswl 0x12(%rsp), %eax
testl %eax, %eax
setle %sil
movl %eax, 0x8(%rbx)
movzwl 0x14(%rsp), %ecx
movswl %cx, %r14d
movl %r14d, 0xc(%rbx)
movb 0x16(%rsp), %dl
sarb $0x3, %dl
movsbl %dl, %ecx
movl %ecx, 0x10(%rbx)
testw %r14w, %r14w
setle %dil
orb %sil, %dil
je 0x38f6
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x20, %edx
leaq 0x1823(%rip), %rsi      # 0x50b7
movq 0x4755(%rip), %rdi      # 0x7ff0
callq 0x21d0
jmp 0x38db
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x4730(%rip), %rdi      # 0x7ff0
leaq 0x1838(%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, %dl
ja 0x387e
movzbl %dl, %edx
movl $0x1a, %esi
btl %edx, %esi
jae 0x387e
imull %eax, %r14d
imull %ecx, %r14d
movq %r14, %rdi
callq 0x2030
movq %rax, (%rbx)
movzbl 0x8(%rsp), %ecx
andl $-0x2, %ecx
cmpl $0xa, %ecx
je 0x3977
cmpl $0x2, %ecx
jne 0x3a23
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 0x3988
leaq 0x18(%rsp), %rdi
callq 0x2060
movl $0x28, %edx
leaq 0x176d(%rip), %rsi      # 0x50d8
movq 0x467e(%rip), %rdi      # 0x7ff0
jmp 0x3a65
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x3a90
testb %al, %al
je 0x3955
movb 0x17(%rsp), %al
testb $0x20, %al
jne 0x399c
movq %rbx, %rdi
callq 0x3c50
movb 0x17(%rsp), %al
testb $0x10, %al
je 0x39a8
movq %rbx, %rdi
callq 0x3d34
movl 0x8(%rbx), %esi
movq 0x463e(%rip), %rdi      # 0x7ff0
callq 0x22d0
movq %rax, %r14
leaq 0x1755(%rip), %rsi      # 0x5116
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl 0xc(%rbx), %esi
movq %r14, %rdi
callq 0x22d0
movq %rax, %r14
leaq 0x1735(%rip), %rsi      # 0x5118
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movl 0x10(%rbx), %esi
shll $0x3, %esi
movq %r14, %rdi
callq 0x22d0
leaq 0x16fa(%rip), %rsi      # 0x50ff
movl $0x1, %edx
movq %rax, %rdi
callq 0x21d0
movb $0x1, %bl
leaq 0x18(%rsp), %rdi
callq 0x2060
jmp 0x38dd
leaq 0x18(%rsp), %rdi
callq 0x2060
movq 0x45bc(%rip), %rdi      # 0x7ff0
leaq 0x16c6(%rip), %rsi      # 0x5101
movl $0x14, %edx
callq 0x21d0
movsbl 0x8(%rsp), %esi
movq 0x459f(%rip), %rdi      # 0x7ff0
callq 0x22d0
movq %rax, %rdi
movl $0x1, %edx
leaq 0x169a(%rip), %rsi      # 0x50ff
callq 0x21d0
jmp 0x38db
jmp 0x3a73
jmp 0x3a73
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x2b9c