// File: /workspace/github_cpp_makefile_star2_O1/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)
movaps 0x1244(%rip), %xmm0     # 0x5180
movaps %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 0x4008
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rsi
movaps %xmm0, (%rsi)
movw $0x0, 0x10(%rsi)
movl 0x10(%rbx), %eax
leal (,%rax,8), %ecx
movb %cl, 0x10(%rsi)
movzwl 0x8(%rbx), %ecx
movw %cx, 0xc(%rsi)
movzwl 0xc(%rbx), %ecx
movw %cx, 0xe(%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 0x4044
leaq 0x48(%rsp), %rdi
callq 0x2240
movl $0x18, %edx
leaq 0x1139(%rip), %rsi      # 0x5130
movq 0x3ff2(%rip), %rdi      # 0x7ff0
callq 0x21d0
jmp 0x4187
movq 0x3fe1(%rip), %rbx      # 0x7ff0
leaq 0x1065(%rip), %rsi      # 0x507b
movl $0x10, %edx
movq %rbx, %rdi
callq 0x21d0
testq %r15, %r15
je 0x4076
movq %r15, %rdi
callq 0x20e0
movq 0x3fb9(%rip), %rdi      # 0x7ff0
movq %r15, %rsi
movq %rax, %rdx
callq 0x21d0
jmp 0x408d
testb %r14b, %r14b
je 0x40b4
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x41d2
testb %al, %al
jne 0x40f3
leaq 0x48(%rsp), %rdi
callq 0x2240
movl $0x16, %edx
leaq 0x10ec(%rip), %rsi      # 0x5160
jmp 0x3ff7
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22b0
movq 0x3f5c(%rip), %rdi      # 0x7ff0
leaq 0x1064(%rip), %rsi      # 0x50ff
movl $0x1, %edx
callq 0x21d0
leaq 0x48(%rsp), %rdi
callq 0x2240
jmp 0x4187
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 0x40f3
movl $0x16, %ebx
leaq 0x1058(%rip), %r14      # 0x5149
jmp 0x416b
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 0x1014(%rip), %r14      # 0x5130
cmpl $0x0, 0x68(%rsp,%rax)
jne 0x416b
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 0x416b
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 0x41a4
movq 0x3e7e(%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 0x4189
jmp 0x41b4
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x2bb2
nop