// File: /workspace/github_c_makefile_star2_O2/halcy[P]quasi_crystals_bot/Engine/GIF.c
// Function: WriteGIFImage
void WriteGIFImage(FILE *fh,int x,int y,int width,int height,Palette palette,int numcolours,uint8_t *pixels)
{
	int exp;
	if(numcolours>128) exp=8;
	else if(numcolours>64) exp=7;
	else if(numcolours>32) exp=6;
	else if(numcolours>16) exp=5;
	else if(numcolours>8) exp=4;
	else if(numcolours>4) exp=3;
	else if(numcolours>2) exp=2;
	else exp=1;

	uint8_t flags=0;
	if(palette && numcolours>0)
	{
		flags|=0x80|0x00|0x00|(exp-1); // LCT present, not interlaced, not sorted, number of entries.
	}

	uint8_t header[]=
	{
		0x2c, // Image Separator
		x&0xff,x>>8,
		y&0xff,y>>8,
		width&0xff,width>>8,
		height&0xff,height>>8,
		flags,
	};

	Write(fh,header,sizeof(header));

	int palettesize=1<<exp;
	for(int i=0;i<palettesize;i++)
	{
		uint8_t entry[3]={0,0,0};
		if(i<numcolours)
		{
			entry[0]=ExtractRed(palette[i]);
			entry[1]=ExtractGreen(palette[i]);
			entry[2]=ExtractBlue(palette[i]);
		}
		Write(fh,entry,3);
	}

	// Code lengths are limited to 3 bits or longer.
	if(exp<2) WriteLZWData(fh,pixels,width*height,2);
	else WriteLZWData(fh,pixels,width*height,exp);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x38(%rsp), %rsp
movq %r9, 0x28(%rsp)
movl %ecx, 0x18(%rsp)
movl %esi, %eax
movq %rdi, %r15
movl 0x70(%rsp), %ebp
movl %r8d, 0x14(%rsp)
cmpl $0x80, %ebp
jle 0x2081
pushq $0x8
jmp 0x20b0
cmpl $0x40, %ebp
jle 0x208a
pushq $0x7
jmp 0x20b0
cmpl $0x20, %ebp
jle 0x2093
pushq $0x6
jmp 0x20b0
cmpl $0x10, %ebp
jle 0x209c
pushq $0x5
jmp 0x20b0
cmpl $0x8, %ebp
jle 0x20a5
pushq $0x4
jmp 0x20b0
cmpl $0x4, %ebp
jle 0x21e0
pushq $0x3
popq %rcx
movl %ecx, 0xc(%rsp)
movl %ecx, %r12d
leaq 0x1e(%rsp), %rsi
movq 0x78(%rsp), %rcx
movb $0x2c, (%rsi)
movb %al, 0x1(%rsi)
movb %ah, 0x20(%rsp)
leal -0x1(%r12), %eax
movq %rcx, 0x30(%rsp)
xorl %ecx, %ecx
movb %dl, 0x3(%rsi)
movb %dh, 0x22(%rsp)
orb $-0x80, %al
testl %ebp, %ebp
movzbl %al, %eax
cmovlel %ecx, %eax
cmpq $0x0, 0x28(%rsp)
cmovel %ecx, %eax
movl 0x18(%rsp), %ecx
movb %cl, 0x5(%rsi)
movb %ch, 0x24(%rsp)
movl 0x14(%rsp), %ecx
movb %cl, 0x7(%rsi)
movb %ch, 0x26(%rsp)
movb %al, 0x9(%rsi)
pushq $0xa
popq %rdx
movq %r15, %rdi
callq 0x1ed3
movslq %ebp, %r13
leaq 0x10(%rsp), %rbx
pushq $0x3
popq %rbp
xorl %r14d, %r14d
movl %r14d, %eax
movl %r12d, %ecx
shrl %cl, %eax
testl %eax, %eax
jne 0x21ba
andw $0x0, 0x10(%rsp)
movb $0x0, 0x12(%rsp)
cmpq %r13, %r14
jge 0x21a4
movq 0x28(%rsp), %rax
movq %rbx, %rdi
movl (%rax,%r14,4), %ebx
movl %ebx, %esi
andl $0xff, %esi
je 0x219b
movzbl %bh, %eax
xorl %edx, %edx
movl %ebx, %ecx
shrl $0x18, %ebx
imull $0xff, %eax, %eax
shrl $0x10, %ecx
divw %si
movl $0xff, %edx
andl %edx, %ecx
xorl %edx, %edx
imull $0xff, %ecx, %ecx
movb %al, 0x10(%rsp)
movl %ecx, %eax
imull $0xff, %ebx, %ecx
divw %si
xorl %edx, %edx
movb %al, 0x11(%rsp)
movl %ecx, %eax
divw %si
jmp 0x219d
xorl %eax, %eax
movq %rdi, %rbx
movb %al, 0x12(%rsp)
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x1ed3
incq %r14
jmp 0x2124
movl 0x14(%rsp), %edx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movl 0xc(%rsp), %ecx
imull 0x18(%rsp), %edx
leaq 0x38(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21f9
xorl %r12d, %r12d
cmpl $0x3, %ebp
setge %r12b
incl %r12d
pushq $0x2
popq %rcx
movl %ecx, 0xc(%rsp)
jmp 0x20b8