// File: shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::unload_rle_data(std::basic_ofstream<char, std::char_traits<char>>&)
bool TGAImage::unload_rle_data(std::ofstream &out) {
    const unsigned char max_chunk_length = 128;
    unsigned long npixels = width*height;
    unsigned long curpix = 0;
    while (curpix<npixels) {
        unsigned long chunkstart = curpix*bytespp;
        unsigned long curbyte = curpix*bytespp;
        unsigned char run_length = 1;
        bool raw = true;
        while (curpix+run_length<npixels && run_length<max_chunk_length) {
            bool succ_eq = true;
            for (int t=0; succ_eq && t<bytespp; t++) {
                succ_eq = (data[curbyte+t]==data[curbyte+t+bytespp]);
            }
            curbyte += bytespp;
            if (1==run_length) {
                raw = !succ_eq;
            }
            if (raw && succ_eq) {
                run_length--;
                break;
            }
            if (!raw && !succ_eq) {
                break;
            }
            run_length++;
        }
        curpix += run_length;
        out.put(raw?run_length-1:run_length+127);
        if (!out.good()) {
            std::cerr << "can't dump the tga file\n";
            return false;
        }
        out.write((char *)(data+chunkstart), (raw?run_length*bytespp:bytespp));
        if (!out.good()) {
            std::cerr << "can't dump the tga file\n";
            return false;
        }
    }
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movb $-0x80, -0x19(%rbp)
movl 0x8(%rcx), %eax
imull 0xc(%rcx), %eax
cltq
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jae 0x7676
movq -0x50(%rbp), %rcx
movq -0x30(%rbp), %rax
movslq 0x10(%rcx), %rdx
imulq %rdx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movslq 0x10(%rcx), %rcx
imulq %rcx, %rax
movq %rax, -0x40(%rbp)
movb $0x1, -0x41(%rbp)
movb $0x1, -0x42(%rbp)
movq -0x30(%rbp), %rcx
movzbl -0x41(%rbp), %eax
addq %rax, %rcx
xorl %eax, %eax
cmpq -0x28(%rbp), %rcx
movb %al, -0x51(%rbp)
jae 0x74b3
movzbl -0x41(%rbp), %eax
cmpl $0x80, %eax
setl %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x74bf
jmp 0x758a
movb $0x1, -0x43(%rbp)
movl $0x0, -0x48(%rbp)
xorl %eax, %eax
testb $0x1, -0x43(%rbp)
movb %al, -0x52(%rbp)
je 0x74e5
movq -0x50(%rbp), %rcx
movl -0x48(%rbp), %eax
cmpl 0x10(%rcx), %eax
setl %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0x74ee
jmp 0x7532
movq -0x50(%rbp), %rsi
movq (%rsi), %rax
movq -0x40(%rbp), %rcx
movslq -0x48(%rbp), %rdx
addq %rdx, %rcx
movzbl (%rax,%rcx), %eax
movq (%rsi), %rcx
movq -0x40(%rbp), %rdx
movslq -0x48(%rbp), %rdi
addq %rdi, %rdx
movslq 0x10(%rsi), %rsi
addq %rsi, %rdx
movzbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movb %al, -0x43(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x74ca
movq -0x50(%rbp), %rax
movslq 0x10(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movzbl -0x41(%rbp), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x7559
movb -0x43(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x42(%rbp)
testb $0x1, -0x42(%rbp)
je 0x756f
testb $0x1, -0x43(%rbp)
je 0x756f
movb -0x41(%rbp), %al
addb $-0x1, %al
movb %al, -0x41(%rbp)
jmp 0x758a
testb $0x1, -0x42(%rbp)
jne 0x757d
testb $0x1, -0x43(%rbp)
jne 0x757d
jmp 0x758a
movb -0x41(%rbp), %al
addb $0x1, %al
movb %al, -0x41(%rbp)
jmp 0x748e
movzbl -0x41(%rbp), %eax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x60(%rbp)
testb $0x1, -0x42(%rbp)
je 0x75b0
movzbl -0x41(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x75ba
movzbl -0x41(%rbp), %eax
addl $0x7f, %eax
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %eax
movsbl %al, %esi
callq 0x2050
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x2320
testb $0x1, %al
jne 0x75f9
movq 0x4a0c(%rip), %rdi      # 0xbff0
leaq 0xb35(%rip), %rsi       # 0x8120
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x767a
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq (%rax), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
testb $0x1, -0x42(%rbp)
je 0x7627
movq -0x50(%rbp), %rcx
movzbl -0x41(%rbp), %eax
imull 0x10(%rcx), %eax
movl %eax, -0x7c(%rbp)
jmp 0x7631
movq -0x50(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x7c(%rbp)
movq -0x70(%rbp), %rsi
movq -0x78(%rbp), %rdi
movl -0x7c(%rbp), %eax
movslq %eax, %rdx
callq 0x2240
movq -0x18(%rbp), %rdi
movq (%rdi), %rax
addq -0x18(%rax), %rdi
callq 0x2320
testb $0x1, %al
jne 0x7671
movq 0x4991(%rip), %rdi      # 0xbff0
leaq 0xaba(%rip), %rsi       # 0x8120
callq 0x21c0
movb $0x0, -0x1(%rbp)
jmp 0x767a
jmp 0x7454
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)