// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl $0x38464947, 0x351362(%rip) # imm = 0x38464947
movl $0x613738, 0x35135b(%rip) # imm = 0x613738
andq $0x0, 0x351364(%rip)    # 0x43f5dc
movq %rdx, -0x38(%rbp)
andl $0x0, 0x351355(%rip)    # 0x43f5d8
leaq 0x3512d6(%rip), %rax    # 0x43f560
movq %rax, 0x35133f(%rip)    # 0x43f5d0
leaq 0x114568(%rip), %rax    # 0x202800
movl (%rax), %eax
movl %eax, 0x3512c0(%rip)    # 0x43f560
leaq 0x11455d(%rip), %rax    # 0x202804
movl (%rax), %eax
movl %esi, -0x2c(%rbp)
subl %esi, %eax
movl %eax, 0x3512b0(%rip)    # 0x43f564
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x3512a3(%rip)    # 0x43f568
movl $0x4, 0x3512a5(%rip)    # 0x43f574
leaq 0x35127a(%rip), %rax    # 0x43f550
movq %rax, 0x3512a3(%rip)    # 0x43f580
movl $0x10, 0x351269(%rip)   # 0x43f550
leaq -0x80(%rbp), %rax
movq %rax, 0x351262(%rip)    # 0x43f554
xorl %eax, %eax
cmpq $0x10, %rax
je 0xee30a
movq 0x351253(%rip), %rcx    # 0x43f554
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0xee2f4
xorl %esi, %esi
leaq 0x23bd4d(%rip), %r8     # 0x32a060
leaq 0x3c(%r8), %r14
leaq 0x3d(%r8), %rbx
leaq 0x3e(%r8), %rdx
leaq 0x40(%r8), %r15
leaq 0x41(%r8), %r12
leaq 0x42(%r8), %r13
leaq 0x1(%r8), %rax
leaq 0x2(%r8), %rcx
cmpq $0x10, %rsi
je 0xee3cb
movzbl 0x2(%r8,%rsi,4), %r9d
movzbl 0x1(%r8,%rsi,4), %r10d
movzbl (%r8,%rsi,4), %r11d
shll $0x18, %r11d
shll $0x10, %r10d
shll $0x8, %r9d
orl %r10d, %r9d
orl %r11d, %r9d
movq 0x3511ed(%rip), %r10    # 0x43f554
movl %r9d, (%r10,%rsi,4)
testl %edi, %edi
je 0xee3c3
movq %rdx, %r11
movq %rbx, %r10
movq %r14, %r9
testl %esi, %esi
je 0xee39a
cmpl $0xe, %esi
je 0xee391
cmpl $0xf, %esi
jne 0xee3c3
movq %rcx, %r11
movq %rax, %r10
movq %r8, %r9
jmp 0xee39a
movq %r13, %r11
movq %r12, %r10
movq %r15, %r9
movzbl (%r11), %r11d
movzbl (%r10), %r10d
movzbl (%r9), %r9d
shll $0x18, %r9d
shll $0x10, %r10d
shll $0x8, %r11d
orl %r10d, %r11d
orl %r9d, %r11d
movq 0x351195(%rip), %r9     # 0x43f554
movl %r11d, (%r9,%rsi,4)
incq %rsi
jmp 0xee333
leaq 0x35117e(%rip), %rax    # 0x43f550
movq %rax, 0x3511d3(%rip)    # 0x43f5ac
leaq 0x3511a8(%rip), %r15    # 0x43f588
leaq 0x114419(%rip), %rax    # 0x202800
movl (%rax), %eax
movl %eax, 0x3511a1(%rip)    # 0x43f590
leaq 0x11440e(%rip), %rax    # 0x202804
movl (%rax), %eax
movl -0x2c(%rbp), %r14d
subl %r14d, %eax
movl %eax, 0x35118f(%rip)    # 0x43f594
andq $0x0, 0x35118b(%rip)    # 0x43f598
movl $0x4, 0x351191(%rip)    # 0x43f5a8
leaq 0x3511aa(%rip), %rsi    # 0x43f5c8
movq -0x38(%rbp), %rbx
movq %rbx, %rdi
callq 0xee069
movq %rbx, %rdi
movl %r14d, %esi
movq %r15, %rdx
callq 0xee15c
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq