// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %rax
movq %rsi, %rbx
movslq 0x8(%rdi), %rax
movq %rdi, (%rsp)
movslq 0xc(%rdi), %r12
imulq %rax, %r12
xorl %r14d, %r14d
movq %r14, %r13
cmpq %r12, %r14
jae 0x3e1e
movq (%rsp), %rcx
movslq 0x10(%rcx), %rax
movq %r13, %r15
imulq %rax, %r15
movq (%rcx), %rsi
leaq (%rsi,%r15), %rcx
leaq 0x1(%r13), %rdx
imulq %rax, %rdx
addq %rsi, %rdx
movb $0x1, %bpl
pushq $0x1
popq %rsi
movl %esi, %r14d
testb %sil, %sil
js 0x3d9f
leaq (%rsi,%r13), %rdi
cmpq %r12, %rdi
jae 0x3d9f
movb $0x1, %dil
xorl %r8d, %r8d
testb $0x1, %dil
je 0x3d67
cmpq %rax, %r8
jge 0x3d67
movb (%rcx,%r8), %dil
cmpb (%rdx,%r8), %dil
sete %dil
incq %r8
jmp 0x3d4b
movl %edi, %r8d
notb %r8b
andb $0x1, %r8b
cmpq $0x1, %rsi
movzbl %bpl, %r9d
movzbl %r8b, %ebp
cmovnel %r9d, %ebp
movl %edi, %r8d
andb %bpl, %r8b
testb $0x1, %r8b
jne 0x3da9
orb %bpl, %dil
incq %rsi
addq %rax, %rcx
addq %rax, %rdx
testb $0x1, %dil
jne 0x3d34
movb $0x7f, %al
testb $0x1, %bpl
jne 0x3daf
jmp 0x3db1
decl %r14d
movb $0x1, %bpl
movb $-0x1, %al
addb %r14b, %al
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq (%rbx), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%rbx,%rax)
jne 0x3e0b
movzbl %r14b, %edx
leaq (%rdx,%r13), %r14
movq (%rsp), %rax
addq (%rax), %r15
movslq 0x10(%rax), %rax
testb $0x1, %bpl
pushq $0x1
popq %rcx
cmovel %ecx, %edx
imulq %rax, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x20(%rbx,%rax)
je 0x3d01
movq 0x41de(%rip), %rdi      # 0x7ff0
leaq 0x1317(%rip), %rsi      # 0x5130
callq 0x21a0
cmpq %r12, %r13
setae %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop