// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0xc(%rsp)
movq %rsi, %r15
andl $0x0, 0x8(%rsp)
movq %rdi, %rbx
movdqa 0x16cf(%rip), %xmm0     # 0x5180
movdqa %xmm0, 0x10(%rsp)
movw $0x2e, 0x20(%rsp)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x22b0
pushq $0x4
popq %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2050
leaq 0xb8(%rsp), %rdi
callq 0x2080
testb %al, %al
je 0x3b70
leaq 0x30(%rsp), %rsi
andl $0x0, 0x8(%rsi)
andq $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
pushq $0x12
popq %rdx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
je 0x3bac
leaq 0x48(%rsp), %rdi
callq 0x2240
leaq 0x15d1(%rip), %rsi      # 0x5130
movq 0x448a(%rip), %rdi      # 0x7ff0
callq 0x21a0
jmp 0x3c95
movq 0x4479(%rip), %rdi      # 0x7ff0
leaq 0x14fd(%rip), %rsi      # 0x507b
callq 0x21a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x21a0
leaq 0x156a(%rip), %rsi      # 0x50ff
movq %rax, %rdi
callq 0x21a0
leaq 0x48(%rsp), %rdi
callq 0x2240
jmp 0x3c95
testb %r14b, %r14b
je 0x3bd5
leaq 0x48(%rsp), %rsi
movq %rbx, %rdi
callq 0x3ce0
testb %al, %al
jne 0x3c0f
leaq 0x48(%rsp), %rdi
callq 0x2240
leaq 0x158d(%rip), %rsi      # 0x5160
jmp 0x3b5f
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 0x3c0f
leaq 0x153c(%rip), %rbx      # 0x5149
jmp 0x3c7c
leaq 0x48(%rsp), %rdi
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x14ff(%rip), %rbx      # 0x5130
cmpl $0x0, 0x68(%rsp,%rax)
jne 0x3c7c
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
jne 0x3c7c
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x12
popq %rdx
callq 0x2210
movq 0x48(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x68(%rsp,%rax)
je 0x3cb2
movq 0x436d(%rip), %rdi      # 0x7ff0
movq %rbx, %rsi
callq 0x21a0
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 0x3c97
jmp 0x3cc2
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2260
movq %rbx, %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x2a25
nop