// File: /workspace/github_cpp_makefile_star2_O3/shinerd[P]BriefRenderer/tgaimage.cpp
// Function: TGAImage::write_tga_file(char const*, bool)
bool TGAImage::write_tga_file(const char *filename, bool rle) {
    unsigned char developer_area_ref[4] = {0, 0, 0, 0};
    unsigned char extension_area_ref[4] = {0, 0, 0, 0};
    unsigned char footer[18] = {'T','R','U','E','V','I','S','I','O','N','-','X','F','I','L','E','.','\0'};
    std::ofstream out;
    out.open (filename, std::ios::binary);
    if (!out.is_open()) {
        std::cerr << "can't open file " << filename << "\n";
        out.close();
        return false;
    }
    TGA_Header header;
    memset((void *)&header, 0, sizeof(header));
    header.bitsperpixel = bytespp<<3;
    header.width  = width;
    header.height = height;
    header.datatypecode = (bytespp==GRAYSCALE?(rle?11:3):(rle?10:2));
    header.imagedescriptor = 0x20; // top-left origin
    out.write((char *)&header, sizeof(header));
    if (!out.good()) {
        out.close();
        std::cerr << "can't dump the tga file\n";
        return false;
    }
    if (!rle) {
        out.write((char *)data, width*height*bytespp);
        if (!out.good()) {
            std::cerr << "can't unload raw data\n";
            out.close();
            return false;
        }
    } else {
        if (!unload_rle_data(out)) {
            out.close();
            std::cerr << "can't unload rle data\n";
            return false;
        }
    }
    out.write((char *)developer_area_ref, sizeof(developer_area_ref));
    if (!out.good()) {
        std::cerr << "can't dump the tga file\n";
        out.close();
        return false;
    }
    out.write((char *)extension_area_ref, sizeof(extension_area_ref));
    if (!out.good()) {
        std::cerr << "can't dump the tga file\n";
        out.close();
        return false;
    }
    out.write((char *)footer, sizeof(footer));
    if (!out.good()) {
        std::cerr << "can't dump the tga file\n";
        out.close();
        return false;
    }
    out.close();
    return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
movdqa 0x12cb(%rip), %xmm0     # 0x5180
movdqa %xmm0, 0x10(%rsp)
movw $0x2e, 0x20(%rsp)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x22c0
movq %r12, %rdi
movq %r15, %rsi
movl $0x4, %edx
callq 0x2050
leaq 0xb8(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x3f83
leaq 0x30(%rsp), %rsi
movl $0x0, 0x8(%rsi)
movq $0x0, (%rsi)
movl 0x10(%rbx), %eax
leal (,%rax,8), %ecx
movb %cl, 0x10(%rsi)
movq 0x8(%rbx), %xmm0
pshuflw $0xe8, %xmm0, %xmm0     # xmm0 = xmm0[0,2,2,3,4,5,6,7]
movd %xmm0, 0xc(%rsi)
cmpl $0x1, %eax
sete %al
leal (,%r14,8), %ecx
orb %al, %cl
orb $0x2, %cl
movb %cl, 0x2(%rsi)
movb $0x20, 0x11(%rsi)
leaq 0x48(%rsp), %rdi
movl $0x12, %edx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
je 0x3fbf
leaq 0x48(%rsp), %rdi
callq 0x2240
movl $0x18, %edx
leaq 0x11be(%rip), %rsi      # 0x5130
movq 0x4077(%rip), %rdi      # 0x7ff0
callq 0x21d0
jmp 0x4102
movq 0x4066(%rip), %rbx      # 0x7ff0
leaq 0x10ea(%rip), %rsi      # 0x507b
movl $0x10, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r15, %r15
je 0x3ff1
movq %r15, %rdi
callq 0x20e0
movq 0x403e(%rip), %rdi      # 0x7ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x4008
testb %r14b, %r14b
je 0x402f
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x414c
testb %al, %al
jne 0x406e
leaq 0x48(%rsp), %rdi
callq 0x2240
movl $0x16, %edx
leaq 0x1171(%rip), %rsi      # 0x5160
jmp 0x3f72
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x3fe1(%rip), %rdi      # 0x7ff0
leaq 0x10e9(%rip), %rsi      # 0x50ff
movl $0x1, %edx
callq 0x21d0
leaq 0x48(%rsp), %rdi
callq 0x2240
jmp 0x4102
movq (%rbx), %rsi
movslq 0x8(%rbx), %rax
movslq 0xc(%rbx), %rcx
imulq %rax, %rcx
movslq 0x10(%rbx), %rdx
imulq %rcx, %rdx
leaq 0x48(%rsp), %rdi
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
je 0x406e
movl $0x16, %ebx
leaq 0x10dd(%rip), %r14      # 0x5149
jmp 0x40e6
leaq 0x48(%rsp), %rdi
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
movl $0x18, %ebx
leaq 0x1099(%rip), %r14      # 0x5130
cmpl $0x0, 0x68(%rsp,%rax)
jne 0x40e6
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
jne 0x40e6
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movl $0x12, %edx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
je 0x411f
movq 0x3f03(%rip), %rdi      # 0x7ff0
movq %r14, %rsi
movq %rbx, %rdx
callq 0x21d0
leaq 0x48(%rsp), %rdi
callq 0x2240
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
callq 0x2260
movl %ebx, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movb $0x1, %bl
leaq 0x48(%rsp), %rdi
callq 0x2240
jmp 0x4104
jmp 0x412f
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x2b9c