// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdx, -0x38(%rbp)
movl $0x38464947, 0x350b19(%rip) # imm = 0x38464947
movl $0x613738, 0x350b12(%rip) # imm = 0x613738
movq $0x0, 0x350b18(%rip) # 0x4555dc
movl $0x0, 0x350b0a(%rip)    # 0x4555d8
leaq 0x350a8b(%rip), %rax    # 0x455560
movq %rax, 0x350af4(%rip)    # 0x4555d0
leaq 0x113d1d(%rip), %rax    # 0x218800
movl (%rax), %eax
movl %eax, 0x350a75(%rip)    # 0x455560
leaq 0x113d12(%rip), %rax    # 0x218804
movl (%rax), %eax
movl %esi, -0x2c(%rbp)
subl %esi, %eax
movl %eax, 0x350a65(%rip)    # 0x455564
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x350a58(%rip)    # 0x455568
movl $0x4, 0x350a5a(%rip)    # 0x455574
leaq 0x350a2f(%rip), %rax    # 0x455550
movq %rax, 0x350a58(%rip)    # 0x455580
movl $0x10, 0x350a1e(%rip)   # 0x455550
leaq -0x80(%rbp), %rax
movq %rax, 0x350a17(%rip)    # 0x455554
xorl %eax, %eax
movq 0x350a0e(%rip), %rcx    # 0x455554
movl $0x0, (%rcx,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x104b3f
xorl %esi, %esi
leaq 0x23b501(%rip), %r8     # 0x340060
leaq 0x3c(%r8), %rbx
leaq 0x3d(%r8), %r14
leaq 0x3e(%r8), %rdx
leaq 0x40(%r8), %r15
leaq 0x41(%r8), %r12
leaq 0x42(%r8), %r13
leaq 0x1(%r8), %rax
leaq 0x2(%r8), %rcx
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 0x3509ab(%rip), %r10    # 0x455554
movl %r9d, (%r10,%rsi,4)
testl %edi, %edi
je 0x104c05
movq %rdx, %r11
movq %r14, %r10
movq %rbx, %r9
testl %esi, %esi
je 0x104bdc
cmpl $0xe, %esi
je 0x104bd3
cmpl $0xf, %esi
jne 0x104c05
movq %rcx, %r11
movq %rax, %r10
movq %r8, %r9
jmp 0x104bdc
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 0x350953(%rip), %r9     # 0x455554
movl %r11d, (%r9,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x104b7f
leaq 0x350937(%rip), %rax    # 0x455550
movq %rax, 0x35098c(%rip)    # 0x4555ac
leaq 0x350961(%rip), %r15    # 0x455588
leaq 0x113bd2(%rip), %rax    # 0x218800
movl (%rax), %eax
movl %eax, 0x35095a(%rip)    # 0x455590
leaq 0x113bc7(%rip), %rax    # 0x218804
movl (%rax), %eax
movl -0x2c(%rbp), %ebx
subl %ebx, %eax
movl %eax, 0x35094a(%rip)    # 0x455594
movq $0x0, 0x350943(%rip) # 0x455598
movl $0x4, 0x350949(%rip)    # 0x4555a8
leaq 0x350962(%rip), %rsi    # 0x4555c8
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x104846
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x104973
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq