// File: /workspace/github_cpp_makefile_star2_O1/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, 0x3512d9(%rip) # imm = 0x38464947
movl $0x613738, 0x3512d2(%rip) # imm = 0x613738
movq $0x0, 0x3512d8(%rip) # 0x4556bc
movl $0x0, 0x3512ca(%rip)    # 0x4556b8
leaq 0x35124b(%rip), %rax    # 0x455640
movq %rax, 0x3512b4(%rip)    # 0x4556b0
leaq 0x1143fd(%rip), %rax    # 0x218800
movl (%rax), %eax
movl %eax, 0x351235(%rip)    # 0x455640
leaq 0x1143f2(%rip), %rax    # 0x218804
movl (%rax), %eax
movl %esi, -0x2c(%rbp)
subl %esi, %eax
movl %eax, 0x351225(%rip)    # 0x455644
movabsq $0x400000001, %rax      # imm = 0x400000001
movq %rax, 0x351218(%rip)    # 0x455648
movl $0x4, 0x35121a(%rip)    # 0x455654
leaq 0x3511ef(%rip), %rax    # 0x455630
movq %rax, 0x351218(%rip)    # 0x455660
movl $0x10, 0x3511de(%rip)   # 0x455630
leaq -0x80(%rbp), %rax
movq %rax, 0x3511d7(%rip)    # 0x455634
xorl %eax, %eax
movq 0x3511ce(%rip), %rcx    # 0x455634
movl $0x0, (%rcx,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x10445f
xorl %esi, %esi
leaq 0x23bbe1(%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 0x35116b(%rip), %r10    # 0x455634
movl %r9d, (%r10,%rsi,4)
testl %edi, %edi
je 0x104525
movq %rdx, %r11
movq %r14, %r10
movq %rbx, %r9
testl %esi, %esi
je 0x1044fc
cmpl $0xe, %esi
je 0x1044f3
cmpl $0xf, %esi
jne 0x104525
movq %rcx, %r11
movq %rax, %r10
movq %r8, %r9
jmp 0x1044fc
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 0x351113(%rip), %r9     # 0x455634
movl %r11d, (%r9,%rsi,4)
incq %rsi
cmpq $0x10, %rsi
jne 0x10449f
leaq 0x3510f7(%rip), %rax    # 0x455630
movq %rax, 0x35114c(%rip)    # 0x45568c
leaq 0x351121(%rip), %r15    # 0x455668
leaq 0x1142b2(%rip), %rax    # 0x218800
movl (%rax), %eax
movl %eax, 0x35111a(%rip)    # 0x455670
leaq 0x1142a7(%rip), %rax    # 0x218804
movl (%rax), %eax
movl -0x2c(%rbp), %ebx
subl %ebx, %eax
movl %eax, 0x35110a(%rip)    # 0x455674
movq $0x0, 0x351103(%rip) # 0x455678
movl $0x4, 0x351109(%rip)    # 0x455688
leaq 0x351122(%rip), %rsi    # 0x4556a8
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x104166
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x104293
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq