// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %r15
movslq 0x8(%rdi), %rax
movslq 0xc(%rdi), %r13
imulq %rax, %r13
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rsp, %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x21d0
movl %eax, %ebp
movq (%r14), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%r14,%rax)
jne 0x38cd
testb %bpl, %bpl
js 0x3825
incl %ebp
movl %ebp, 0x8(%rsp)
movzbl %bpl, %eax
movl %eax, 0xc(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x10(%rsp)
cmpl 0xc(%rsp), %ebp
je 0x3883
movslq 0x10(%r15), %rdx
movq %r14, %rdi
movq %rsp, %rsi
callq 0x21f0
movq (%r14), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%r14,%rax)
jne 0x38a9
xorl %eax, %eax
movslq 0x10(%r15), %rcx
cmpq %rcx, %rax
jge 0x3816
movb (%rsp,%rax), %cl
movq (%r15), %rdx
addq %r12, %rdx
movb %cl, (%rax,%rdx)
incq %rax
jmp 0x37fc
incq %rbx
incl %ebp
addq %rax, %r12
cmpq %r13, %rbx
jbe 0x37ce
jmp 0x38a0
movslq 0x10(%r15), %rdx
movq %r14, %rdi
movq %rsp, %rsi
callq 0x21f0
movq (%r14), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%r14,%rax)
jne 0x38a9
addl $0x81, %ebp
movzbl %bpl, %eax
xorl %ecx, %ecx
movq %rbx, %rdx
cmpl %eax, %ecx
je 0x388c
xorl %esi, %esi
movslq 0x10(%r15), %rdi
cmpq %rdi, %rsi
jge 0x3874
movb (%rsp,%rsi), %dil
movq (%r15), %r8
addq %r12, %r8
movb %dil, (%rsi,%r8)
incq %rsi
jmp 0x3858
incq %rdx
incl %ecx
addq %rsi, %r12
cmpq %r13, %rdx
jbe 0x3852
jmp 0x38a0
movq 0x10(%rsp), %rbx
movl 0x8(%rsp), %ebp
movzbl %bpl, %eax
addq %rax, %rbx
cmpq %r13, %rbx
jb 0x3797
movb $0x1, %al
jmp 0x38be
leaq 0x1873(%rip), %rsi      # 0x511a
jmp 0x38b0
leaq 0x17dc(%rip), %rsi      # 0x508c
movq 0x4739(%rip), %rdi      # 0x7ff0
callq 0x21a0
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1804(%rip), %rsi      # 0x50d8
jmp 0x38b0