// File: /workspace/github_c_makefile_star2_O3/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 %esi, %eax
movq %rdi, %r15
movl %r8d, %ebx
movl $0x8, %esi
movl $0x8, %r12d
movl 0x70(%rsp), %r13d
cmpl $0x80, %r13d
jg 0x2737
cmpl $0x40, %r13d
movl $0x7, %esi
movl $0x7, %r12d
jg 0x2737
cmpl $0x20, %r13d
movl $0x6, %esi
movl $0x6, %r12d
jg 0x2737
cmpl $0x10, %r13d
movl $0x5, %esi
movl $0x5, %r12d
jg 0x2737
cmpl $0x8, %r13d
movl $0x4, %esi
movl $0x4, %r12d
jg 0x2737
cmpl $0x4, %r13d
movl $0x3, %esi
movl $0x3, %r12d
jg 0x2737
xorl %r12d, %r12d
movl $0x2, %esi
cmpl $0x3, %r13d
setge %r12b
incl %r12d
movl %esi, 0x10(%rsp)
movq %r15, %rdi
movq 0x78(%rsp), %rsi
movq %rsi, 0x30(%rsp)
leaq 0x1e(%rsp), %rsi
movb $0x2c, (%rsi)
movb %al, 0x1(%rsi)
movb %ah, 0x20(%rsp)
leal -0x1(%r12), %eax
movb %dl, 0x3(%rsi)
movb %dh, 0x22(%rsp)
xorl %edx, %edx
movb %cl, 0x5(%rsi)
movb %ch, 0x24(%rsp)
movb %bl, 0x7(%rsi)
movb %bh, 0x26(%rsp)
orb $-0x80, %al
testl %r13d, %r13d
movzbl %al, %eax
cmovlel %edx, %eax
cmpq $0x0, 0x28(%rsp)
movl %ecx, 0x18(%rsp)
movl %ebx, 0x14(%rsp)
cmovel %edx, %eax
movl $0xa, %edx
movb %al, 0x9(%rsi)
callq 0x2531
movslq %r13d, %rbp
movl $0xff, %ebx
leaq 0xc(%rsp), %r13
xorl %r14d, %r14d
cmpq %rbp, %r14
movb $0x0, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
jge 0x2819
movq 0x28(%rsp), %rax
movl (%rax,%r14,4), %edi
movl %edi, %esi
andl $0xff, %esi
je 0x2813
movl %edi, %ecx
xorl %edx, %edx
shrl $0x8, %ecx
andl %ebx, %ecx
movl %ecx, %eax
shll $0x8, %eax
subl %ecx, %eax
divw %si
movl %edi, %edx
shrl $0x18, %edi
shrl $0x10, %edx
andl %ebx, %edx
movl %edx, %ecx
shll $0x8, %ecx
subl %edx, %ecx
xorl %edx, %edx
movb %al, 0xc(%rsp)
movl %ecx, %eax
movl %edi, %ecx
divw %si
shll $0x8, %ecx
subl %edi, %ecx
xorl %edx, %edx
movb %al, 0xd(%rsp)
movl %ecx, %eax
divw %si
jmp 0x2815
xorl %eax, %eax
movb %al, 0xe(%rsp)
movl $0x3, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2531
incq %r14
movl %r12d, %ecx
movl %r14d, %eax
shrl %cl, %eax
testl %eax, %eax
je 0x27ac
movl 0x14(%rsp), %edx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movl 0x10(%rsp), %ecx
imull 0x18(%rsp), %edx
leaq 0x38(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2862