// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
leaq -0x50(%rsp), %rsp
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
movq %r9, -0x20(%rbp)
cmpl $0x80, 0x10(%rbp)
jle 0x2c17
movl $0x8, -0x24(%rbp)
jmp 0x2c84
cmpl $0x40, 0x10(%rbp)
jle 0x2c26
movl $0x7, -0x24(%rbp)
jmp 0x2c82
cmpl $0x20, 0x10(%rbp)
jle 0x2c35
movl $0x6, -0x24(%rbp)
jmp 0x2c80
cmpl $0x10, 0x10(%rbp)
jle 0x2c44
movl $0x5, -0x24(%rbp)
jmp 0x2c7e
cmpl $0x8, 0x10(%rbp)
jle 0x2c53
movl $0x4, -0x24(%rbp)
jmp 0x2c7c
cmpl $0x4, 0x10(%rbp)
jle 0x2c62
movl $0x3, -0x24(%rbp)
jmp 0x2c7a
cmpl $0x2, 0x10(%rbp)
jle 0x2c71
movl $0x2, -0x24(%rbp)
jmp 0x2c78
movl $0x1, -0x24(%rbp)
jmp 0x2c7a
jmp 0x2c7c
jmp 0x2c7e
jmp 0x2c80
jmp 0x2c82
jmp 0x2c84
movb $0x0, -0x25(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x2caa
cmpl $0x0, 0x10(%rbp)
jle 0x2caa
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
orl $0x80, %ecx
movzbl -0x25(%rbp), %eax
orl %ecx, %eax
movb %al, -0x25(%rbp)
movb $0x2c, -0x2f(%rbp)
movl -0xc(%rbp), %eax
andl $0xff, %eax
movb %al, -0x2e(%rbp)
movl -0xc(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x2d(%rbp)
movl -0x10(%rbp), %eax
andl $0xff, %eax
movb %al, -0x2c(%rbp)
movl -0x10(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x2b(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, -0x2a(%rbp)
movl -0x14(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x29(%rbp)
movl -0x18(%rbp), %eax
andl $0xff, %eax
movb %al, -0x28(%rbp)
movl -0x18(%rbp), %eax
sarl $0x8, %eax
movb %al, -0x27(%rbp)
movb -0x25(%rbp), %al
movb %al, -0x26(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2f(%rbp), %rsi
movl $0xa, %edx
callq 0x2930
movl -0x24(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x2e7d
leaq -0x3b(%rbp), %rdi
xorl %esi, %esi
movl $0x3, %edx
callq 0x10f0
movl -0x38(%rbp), %eax
cmpl 0x10(%rbp), %eax
jge 0x2e5d
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x0, %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x2d71
xorl %eax, %eax
movl %eax, -0x40(%rbp)
jmp 0x2da5
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
imull $0xff, %eax, %eax
movq -0x20(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shrl $0x0, %ecx
andl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movb %al, -0x3b(%rbp)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x0, %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x2dca
xorl %eax, %eax
movl %eax, -0x44(%rbp)
jmp 0x2dfe
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
imull $0xff, %eax, %eax
movq -0x20(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shrl $0x0, %ecx
andl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movb %al, -0x3a(%rbp)
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x0, %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x2e23
xorl %eax, %eax
movl %eax, -0x48(%rbp)
jmp 0x2e57
movq -0x20(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x18, %eax
andl $0xff, %eax
imull $0xff, %eax, %eax
movq -0x20(%rbp), %rcx
movslq -0x38(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
shrl $0x0, %ecx
andl $0xff, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movb %al, -0x39(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x3b(%rbp), %rsi
movl $0x3, %edx
callq 0x2930
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x2d2a
cmpl $0x2, -0x24(%rbp)
jge 0x2e9e
movq -0x8(%rbp), %rdi
movq 0x18(%rbp), %rsi
movl -0x14(%rbp), %edx
imull -0x18(%rbp), %edx
movl $0x2, %ecx
callq 0x2ec0
jmp 0x2eb5
movq -0x8(%rbp), %rdi
movq 0x18(%rbp), %rsi
movl -0x14(%rbp), %edx
imull -0x18(%rbp), %edx
movl -0x24(%rbp), %ecx
callq 0x2ec0
leaq 0x50(%rsp), %rsp
popq %rbp
retq
nopl (%rax)