// File: /workspace/github_cpp_makefile_star2_O3/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 0x4298
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
movl $0x1, %esi
movl %esi, %r14d
testb %sil, %sil
js 0x4212
leaq (%rsi,%r13), %rdi
cmpq %r12, %rdi
jae 0x4212
testl %eax, %eax
jle 0x41db
movl $0x1, %r8d
movb -0x1(%rcx,%r8), %dil
cmpb -0x1(%rdx,%r8), %dil
sete %dil
jne 0x41de
leaq 0x1(%r8), %r9
cmpq %rax, %r8
movq %r9, %r8
jl 0x41bd
jmp 0x41de
movb $0x1, %dil
movl %edi, %r8d
xorb $0x1, %r8b
cmpq $0x1, %rsi
movzbl %bpl, %ebp
movzbl %r8b, %r8d
cmovel %r8d, %ebp
testb %r8b, %r8b
jne 0x4200
testb $0x1, %bpl
jne 0x421c
orb %bpl, %dil
incq %rsi
addq %rax, %rcx
addq %rax, %rdx
testb $0x1, %dil
jne 0x41a2
movb $0x7f, %al
testb $0x1, %bpl
jne 0x4222
jmp 0x4224
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 0x4280
movzbl %r14b, %edx
leaq (%rdx,%r13), %r14
movq (%rsp), %rax
addq (%rax), %r15
movslq 0x10(%rax), %rax
testb $0x1, %bpl
movl $0x1, %ecx
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 0x416d
movq 0x3d69(%rip), %rdi      # 0x7ff0
leaq 0xea2(%rip), %rsi       # 0x5130
movl $0x18, %edx
callq 0x21d0
cmpq %r12, %r13
setae %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop