// File: shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::load_rle_data(std::basic_ifstream<char, std::char_traits<char>>&)
bool TGAImage::load_rle_data(std::ifstream &in) {
    unsigned long pixelcount = width*height;
    unsigned long currentpixel = 0;
    unsigned long currentbyte  = 0;
    TGAColor colorbuffer;
    do {
        unsigned char chunkheader = 0;
        chunkheader = in.get();
        if (!in.good()) {
            std::cerr << "an error occured while reading the data\n";
            return false;
        }
        if (chunkheader<128) {
            chunkheader++;
            for (int i=0; i<chunkheader; i++) {
                in.read((char *)colorbuffer.raw, bytespp);
                if (!in.good()) {
                    std::cerr << "an error occured while reading the header\n";
                    return false;
                }
                for (int t=0; t<bytespp; t++)
                    data[currentbyte++] = colorbuffer.raw[t];
                currentpixel++;
                if (currentpixel>pixelcount) {
                    std::cerr << "Too many pixels read\n";
                    return false;
                }
            }
        } else {
            chunkheader -= 127;
            in.read((char *)colorbuffer.raw, bytespp);
            if (!in.good()) {
                std::cerr << "an error occured while reading the header\n";
                return false;
            }
            for (int i=0; i<chunkheader; i++) {
                for (int t=0; t<bytespp; t++)
                    data[currentbyte++] = colorbuffer.raw[t];
                currentpixel++;
                if (currentpixel>pixelcount) {
                    std::cerr << "Too many pixels read\n";
                    return false;
                }
            }
        }
    } while (currentpixel < pixelcount);
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movl 0x8(%rcx), %eax
imull 0xc(%rcx), %eax
cltq
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7a90
movb $0x0, -0x39(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2210
movb %al, -0x39(%rbp)
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x2320
testb $0x1, %al
jne 0x6b2e
movq 0x54d7(%rip), %rdi      # 0xbff0
leaq 0x15a8(%rip), %rsi      # 0x80c8
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x6d1b
movzbl -0x39(%rbp), %eax
cmpl $0x80, %eax
jge 0x6c26
movb -0x39(%rbp), %al
addb $0x1, %al
movb %al, -0x39(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
movzbl -0x39(%rbp), %ecx
cmpl %ecx, %eax
jge 0x6c21
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movslq 0x10(%rax), %rdx
callq 0x2220
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x2320
testb $0x1, %al
jne 0x6ba0
movq 0x5465(%rip), %rdi      # 0xbff0
leaq 0x14ea(%rip), %rsi      # 0x807c
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x6d1b
movl $0x0, -0x44(%rbp)
movq -0x58(%rbp), %rcx
movl -0x44(%rbp), %eax
cmpl 0x10(%rcx), %eax
jge 0x6bdf
movq -0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb -0x38(%rbp,%rcx), %dl
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x30(%rbp)
movb %dl, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x6ba7
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x6c11
movq 0x53f4(%rip), %rdi      # 0xbff0
leaq 0x1507(%rip), %rsi      # 0x810a
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x6d1b
jmp 0x6c13
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x6b4c
jmp 0x6d07
movq -0x58(%rbp), %rax
movzbl -0x39(%rbp), %ecx
subl $0x7f, %ecx
movb %cl, -0x39(%rbp)
movq -0x18(%rbp), %rdi
leaq -0x38(%rbp), %rsi
movslq 0x10(%rax), %rdx
callq 0x2220
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x2320
testb $0x1, %al
jne 0x6c75
movq 0x5390(%rip), %rdi      # 0xbff0
leaq 0x1415(%rip), %rsi      # 0x807c
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x6d1b
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movzbl -0x39(%rbp), %ecx
cmpl %ecx, %eax
jge 0x6d05
movl $0x0, -0x4c(%rbp)
movq -0x58(%rbp), %rcx
movl -0x4c(%rbp), %eax
cmpl 0x10(%rcx), %eax
jge 0x6cc6
movq -0x58(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movb -0x38(%rbp,%rcx), %dl
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x30(%rbp)
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x6c8e
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jbe 0x6cf5
movq 0x530d(%rip), %rdi      # 0xbff0
leaq 0x1420(%rip), %rsi      # 0x810a
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x6d1b
jmp 0x6cf7
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x6c7c
jmp 0x6d07
jmp 0x6d09
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jb 0x6aee
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)