// File: jkingan[P]ladyheather/heathmsc.cpp
// Function: dump_gif_file(int, int, _IO_FILE*)
int dump_gif_file(int invert, int top_line, FILE *file)
{
S32 i;
Gif *gif;
GifPalette *cmap;
GifPicture *pic;
#define BPP 4   // 4 bits per pixel = 16 colors
u08 ctable[(1 << BPP) * sizeof(Colour)];

    // setup GIF header
    gif = &gif_data;
    strcpy(gif->header, "GIF87a");
    gif->blocks      = NULL;
    gif->block_count = 0;
    gif->screen      = &screen_data;

    gif->screen->width      = SCREEN_WIDTH;
    gif->screen->height     = SCREEN_HEIGHT-top_line;
    gif->screen->has_cmap   = 1;
    gif->screen->color_res  = BPP;
    gif->screen->cmap_depth = BPP;
   
    // setup palette color map
    screen_data.cmap = &palette_data;
    cmap = gif->screen->cmap;
    cmap->length = (1 << BPP);
    cmap->colours = (Colour *) &ctable[0];

    for(i=0; i<(1 << BPP); i++) {  // clear all the palette table
       cmap->colours[i] = rgb(0, 0, 0);
    }
    for(i=0; i<16; i++) {   // define the colors that we use
       cmap->colours[i] = rgb(bmp_pal[i*4+2], bmp_pal[i*4+1], bmp_pal[i*4+0]);
       if(invert) {
          if     (i == BLACK)  cmap->colours[i] = rgb(bmp_pal[WHITE*4+2], bmp_pal[WHITE*4+1], bmp_pal[WHITE*4+0]);
          else if(i == WHITE)  cmap->colours[i] = rgb(bmp_pal[BLACK*4+2], bmp_pal[BLACK*4+1], bmp_pal[BLACK*4+0]);
          else if(i == YELLOW) cmap->colours[i] = rgb(bmp_pal[BMP_YELLOW*4+2], bmp_pal[BMP_YELLOW*4+1], bmp_pal[BMP_YELLOW*4+0]);
       }
    }

    // picture description
    pic = &pic_data;
    pic->cmap       = &palette_data;
    pic->width      = SCREEN_WIDTH;
    pic->height     = SCREEN_HEIGHT-top_line;
    pic->interlace  = 0; 
    pic->has_cmap   = 0;
    pic->cmap_depth = BPP;

    write_gif_header(file, gif);
    write_gif_picture(file, top_line, pic);

    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movq %rdx, -0x10(%rbp)
leaq 0x34cb9c(%rip), %rax    # 0x4b4bb8
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
leaq 0x30758(%rip), %rsi     # 0x198783
callq 0x9350
movq -0x20(%rbp), %rax
movq $0x0, 0x14(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x20(%rbp), %rax
leaq 0x34cafe(%rip), %rcx    # 0x4b4b50
movq %rcx, 0x8(%rax)
leaq 0x10f983(%rip), %rax    # 0x2779e0
movl (%rax), %ecx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, (%rax)
leaq 0x10f974(%rip), %rax    # 0x2779e4
movl (%rax), %ecx
subl -0x8(%rbp), %ecx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x1, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x4, 0xc(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x4, 0x14(%rax)
leaq 0x34ca8c(%rip), %rax    # 0x4b4b40
movq %rax, 0x34cab5(%rip)    # 0x4b4b70
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl $0x10, (%rax)
leaq -0x70(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x4(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x10, -0x14(%rbp)
jge 0x168119
xorl %edx, %edx
movl %edx, %edi
movl %edx, %esi
callq 0x167fd0
movl %eax, -0x74(%rbp)
movq -0x28(%rbp), %rax
movq 0x4(%rax), %rax
movslq -0x14(%rbp), %rcx
movl -0x74(%rbp), %edx
movl %edx, (%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1680e8
movl $0x0, -0x14(%rbp)
cmpl $0x10, -0x14(%rbp)
jge 0x168278
movl -0x14(%rbp), %eax
shll $0x2, %eax
addl $0x2, %eax
movslq %eax, %rcx
leaq 0x237103(%rip), %rax    # 0x39f240
movzbl (%rax,%rcx), %edi
movl -0x14(%rbp), %eax
shll $0x2, %eax
addl $0x1, %eax
movslq %eax, %rcx
leaq 0x2370ec(%rip), %rax    # 0x39f240
movzbl (%rax,%rcx), %esi
movl -0x14(%rbp), %eax
shll $0x2, %eax
addl $0x0, %eax
movslq %eax, %rcx
leaq 0x2370d5(%rip), %rax    # 0x39f240
movzbl (%rax,%rcx), %edx
callq 0x167fd0
movl %eax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movq 0x4(%rax), %rax
movslq -0x14(%rbp), %rcx
movl -0x78(%rbp), %edx
movl %edx, (%rax,%rcx,4)
cmpl $0x0, -0x4(%rbp)
je 0x168268
cmpl $0x0, -0x14(%rbp)
jne 0x1681d9
leaq 0x2370a0(%rip), %rax    # 0x39f240
movzbl 0x3e(%rax), %edi
leaq 0x237095(%rip), %rax    # 0x39f240
movzbl 0x3d(%rax), %esi
leaq 0x23708a(%rip), %rax    # 0x39f240
movzbl 0x3c(%rax), %edx
callq 0x167fd0
movl %eax, -0x7c(%rbp)
movq -0x28(%rbp), %rax
movq 0x4(%rax), %rax
movslq -0x14(%rbp), %rcx
movl -0x7c(%rbp), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x168266
cmpl $0xf, -0x14(%rbp)
jne 0x16821b
leaq 0x23705a(%rip), %rax    # 0x39f240
movzbl 0x2(%rax), %edi
leaq 0x23704f(%rip), %rax    # 0x39f240
movzbl 0x1(%rax), %esi
leaq 0x237044(%rip), %rax    # 0x39f240
movzbl (%rax), %edx
callq 0x167fd0
movl %eax, -0x80(%rbp)
movq -0x28(%rbp), %rax
movq 0x4(%rax), %rax
movslq -0x14(%rbp), %rcx
movl -0x80(%rbp), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x168264
cmpl $0xe, -0x14(%rbp)
jne 0x168262
leaq 0x237018(%rip), %rax    # 0x39f240
movzbl 0x42(%rax), %edi
leaq 0x23700d(%rip), %rax    # 0x39f240
movzbl 0x41(%rax), %esi
leaq 0x237002(%rip), %rax    # 0x39f240
movzbl 0x40(%rax), %edx
callq 0x167fd0
movl %eax, -0x84(%rbp)
movq -0x28(%rbp), %rax
movq 0x4(%rax), %rax
movslq -0x14(%rbp), %rcx
movl -0x84(%rbp), %edx
movl %edx, (%rax,%rcx,4)
jmp 0x168264
jmp 0x168266
jmp 0x168268
jmp 0x16826a
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x168120
leaq 0x34c8f9(%rip), %rax    # 0x4b4b78
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
leaq 0x34c8b2(%rip), %rcx    # 0x4b4b40
movq %rcx, 0x24(%rax)
leaq 0x10f747(%rip), %rax    # 0x2779e0
movl (%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
leaq 0x10f73b(%rip), %rax    # 0x2779e4
movl (%rax), %ecx
subl -0x8(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x10(%rax)
movq -0x30(%rbp), %rax
movl $0x4, 0x20(%rax)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x167d40
movq -0x10(%rbp), %rdi
movl -0x8(%rbp), %esi
movq -0x30(%rbp), %rdx
callq 0x167ea0
movl $0x1, %eax
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)