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