// File: 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 %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x278(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movaps 0x11e6(%rip), %xmm0     # 0x8170
movaps %xmm0, -0x40(%rbp)
movw $0x2e, -0x30(%rbp)
leaq -0x240(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x2330
movq -0x270(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x2060
jmp 0x6fbe
leaq -0x240(%rbp), %rdi
callq 0x2170
movb %al, -0x279(%rbp)
jmp 0x6fd2
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0x7070
jmp 0x6fe2
movq 0x5007(%rip), %rdi      # 0xbff0
leaq 0x107b(%rip), %rsi      # 0x806b
callq 0x21c0
movq %rax, -0x288(%rbp)
jmp 0x6ffe
movq -0x288(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x21c0
movq %rax, -0x290(%rbp)
jmp 0x7017
movq -0x290(%rbp), %rdi
leaq 0x10ca(%rip), %rsi      # 0x80ef
callq 0x21c0
jmp 0x702c
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x703a
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x248(%rbp)
movl %eax, -0x24c(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x22b0
jmp 0x7408
leaq -0x262(%rbp), %rdi
xorl %esi, %esi
movl $0x12, %edx
callq 0x2130
movq -0x278(%rbp), %rax
movl 0x10(%rax), %ecx
shll $0x3, %ecx
movb %cl, -0x252(%rbp)
movl 0x8(%rax), %ecx
movw %cx, -0x256(%rbp)
movl 0xc(%rax), %ecx
movw %cx, -0x254(%rbp)
cmpl $0x1, 0x10(%rax)
jne 0x70cb
movb -0x19(%rbp), %dl
movl $0x3, %eax
movl $0xb, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x294(%rbp)
jmp 0x70e4
movb -0x19(%rbp), %dl
movl $0x2, %eax
movl $0xa, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x294(%rbp)
movl -0x294(%rbp), %eax
movb %al, -0x260(%rbp)
movb $0x20, -0x251(%rbp)
leaq -0x240(%rbp), %rdi
leaq -0x262(%rbp), %rsi
movl $0x12, %edx
callq 0x2240
jmp 0x7111
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x240(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x295(%rbp)
jmp 0x7131
movb -0x295(%rbp), %al
testb $0x1, %al
jne 0x7173
jmp 0x713d
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x714b
movq 0x4e9e(%rip), %rdi      # 0xbff0
leaq 0xfc7(%rip), %rsi       # 0x8120
callq 0x21c0
jmp 0x7160
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
testb $0x1, -0x19(%rbp)
jne 0x720b
movq -0x278(%rbp), %rcx
movq (%rcx), %rsi
movl 0x8(%rcx), %eax
movl 0xc(%rcx), %edx
imull %edx, %eax
movl 0x10(%rcx), %ecx
imull %ecx, %eax
movslq %eax, %rdx
leaq -0x240(%rbp), %rdi
callq 0x2240
jmp 0x71a7
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x240(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x296(%rbp)
jmp 0x71c7
movb -0x296(%rbp), %al
testb $0x1, %al
jne 0x7209
jmp 0x71d3
movq 0x4e16(%rip), %rdi      # 0xbff0
leaq 0xf58(%rip), %rsi       # 0x8139
callq 0x21c0
jmp 0x71e8
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x71f6
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
jmp 0x726a
movq -0x278(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x7420
movb %al, -0x297(%rbp)
jmp 0x7226
movb -0x297(%rbp), %al
testb $0x1, %al
jne 0x7268
jmp 0x7232
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x7240
movq 0x4da9(%rip), %rdi      # 0xbff0
leaq 0xf02(%rip), %rsi       # 0x8150
callq 0x21c0
jmp 0x7255
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
jmp 0x726a
leaq -0x240(%rbp), %rdi
leaq -0x20(%rbp), %rsi
movl $0x4, %edx
callq 0x2240
jmp 0x7281
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x240(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x298(%rbp)
jmp 0x72a1
movb -0x298(%rbp), %al
testb $0x1, %al
jne 0x72e3
jmp 0x72ad
movq 0x4d3c(%rip), %rdi      # 0xbff0
leaq 0xe65(%rip), %rsi       # 0x8120
callq 0x21c0
jmp 0x72c2
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x72d0
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
leaq -0x240(%rbp), %rdi
leaq -0x24(%rbp), %rsi
movl $0x4, %edx
callq 0x2240
jmp 0x72fa
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x240(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x299(%rbp)
jmp 0x731a
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x735c
jmp 0x7326
movq 0x4cc3(%rip), %rdi      # 0xbff0
leaq 0xdec(%rip), %rsi       # 0x8120
callq 0x21c0
jmp 0x733b
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x7349
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
leaq -0x240(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl $0x12, %edx
callq 0x2240
jmp 0x7373
movq -0x240(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x240(%rbp,%rax), %rdi
callq 0x2320
movb %al, -0x29a(%rbp)
jmp 0x7393
movb -0x29a(%rbp), %al
testb $0x1, %al
jne 0x73d2
jmp 0x739f
movq 0x4c4a(%rip), %rdi      # 0xbff0
leaq 0xd73(%rip), %rsi       # 0x8120
callq 0x21c0
jmp 0x73b4
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x73c2
movb $0x0, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
jmp 0x73ee
leaq -0x240(%rbp), %rdi
callq 0x2290
jmp 0x73e0
movb $0x1, -0x1(%rbp)
movl $0x1, -0x250(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x22b0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
jmp 0x740a
movq -0x248(%rbp), %rdi
callq 0x2350
movq %rax, %rdi
callq 0x30f0
nop