// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r15
movslq 0x8(%rdi), %rax
movq %rdi, 0x8(%rsp)
movslq 0xc(%rdi), %r12
imulq %rax, %r12
xorl %ebx, %ebx
movl $0xff, %ebp
movq %rbx, %r13
cmpq %r12, %rbx
jae 0x4347
movq 0x8(%rsp), %rcx
movslq 0x10(%rcx), %rax
movq %r13, %rdx
imulq %rax, %rdx
movq (%rcx), %rsi
movq %rdx, 0x10(%rsp)
leaq (%rsi,%rdx), %rcx
leaq 0x1(%r13), %rdx
imulq %rax, %rdx
addq %rsi, %rdx
movb $0x1, %sil
movb $0x1, %r14b
testb %sil, %sil
js 0x42c6
movzbl %sil, %edi
addq %r13, %rdi
cmpq %r12, %rdi
jae 0x42c6
testl %eax, %eax
jle 0x4271
movl $0x1, %r8d
movb -0x1(%rcx,%r8), %dil
cmpb -0x1(%rdx,%r8), %dil
sete %dil
jne 0x4274
leaq 0x1(%r8), %r9
cmpq %rax, %r8
movq %r9, %r8
jl 0x4253
jmp 0x4274
movb $0x1, %dil
movl %edi, %r8d
xorb $0x1, %r8b
cmpb $0x1, %sil
movzbl %r14b, %r9d
movzbl %r8b, %r14d
cmovnel %r9d, %r14d
movl %r14d, %r8d
andb $0x1, %r8b
movl %edi, %r9d
orb %r14b, %r9b
andb $0x1, %r9b
movzbl %r9b, %r9d
testb %r8b, %r8b
movl %r9d, %ebx
cmovnel %ebp, %ebx
testb %dil, %dil
cmovel %r9d, %ebx
addb %sil, %bl
addq %rax, %rcx
addq %rax, %rdx
movl %ebx, %esi
cmpb %r8b, %dil
jne 0x4234
jmp 0x42c8
movl %esi, %ebx
movl %r14d, %eax
shlb $0x7, %al
addb %bl, %al
addb $0x7f, %al
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%r15,%rax)
jne 0x432f
movq %r15, %rdi
movzbl %bl, %edx
leaq (%rdx,%r13), %rbx
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rsi
addq (%rax), %rsi
movslq 0x10(%rax), %rax
testb $0x1, %r14b
movl $0x1, %ecx
cmovel %ecx, %edx
imulq %rax, %rdx
callq 0x2210
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%r15,%rax)
je 0x41fb
movq 0x3cba(%rip), %rdi      # 0x7ff0
leaq 0xdf3(%rip), %rsi       # 0x5130
movl $0x18, %edx
callq 0x21d0
cmpq %r12, %r13
setae %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq