// File: /workspace/github_c_makefile_star2_O1/halcy[P]quasi_crystals_bot/Engine/PNG.c
// Function: LoadPNG
Bitmap *LoadPNG(const char *filename)
{
	FILE *fh=fopen(filename,"rb");
	if(!fh)
	{
		fprintf(stderr,"Could not open file \"%s\".\n",filename);
		exit(1);
	}

	fseek(fh,0,SEEK_END);
	long size=ftell(fh);
	fseek(fh,0,SEEK_SET);

	void *bytes=malloc(size);
	if(!bytes)
	{
		fprintf(stderr,"Out of memory.\n");
		exit(1);
	}

	if(fread(bytes,1,size,fh)!=size)
	{
		fprintf(stderr,"Reading from file \"%s\" failed.\n",filename);
		exit(1);
	}

	fclose(fh);

	PNGLoader loader;
	InitializePNGLoader(&loader,bytes,size);

	if(!LoadPNGHeader(&loader))
	{
		fprintf(stderr,"File \"%s\" is not a valid PNG file.\n",filename);
		exit(1);
	}

	Bitmap *bitmap=AllocateBitmap(loader.width,loader.height);
	if(!bitmap)
	{
		fprintf(stderr,"Out of memory.\n");
		exit(1);
	}

	if(!LoadPNGImageData(&loader,bitmap->pixels,bitmap->bytesperrow,PixelFunc))
	{
		fprintf(stderr,"Failed while loading PNG data from file \"%s\".\n",filename);
		exit(1);
	}

	free(bytes);

	return bitmap;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x58(%rsp), %rsp
movq %rdi, %rbx
leaq 0xacff(%rip), %rsi      # 0xe44c
callq 0x11b0
testq %rax, %rax
je 0x3a8e
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1180
movq %r12, %rdi
callq 0x1130
movq %rax, %r15
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1180
movq %r15, %rdi
callq 0x1150
testq %rax, %rax
je 0x3a96
movq %rax, %r14
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1060
cmpq %r15, %rax
jne 0x3a9b
movq %r12, %rdi
callq 0x1090
movq %r14, (%rsp)
xorl %eax, %eax
movq %r15, 0x8(%rsp)
movq %rax, 0x38(%rsp)
movl %eax, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movl %eax, 0x50(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
cmpq $0x8, %r15
movups %xmm0, 0x20(%rsp)
movl %eax, 0x30(%rsp)
jl 0x3a86
cmpb $-0x77, (%r14)
jne 0x3a86
cmpb $0x50, 0x1(%r14)
jne 0x3a86
cmpb $0x4e, 0x2(%r14)
jne 0x3a86
cmpb $0x47, 0x3(%r14)
jne 0x3a86
cmpb $0xd, 0x4(%r14)
jne 0x3a86
cmpb $0xa, 0x5(%r14)
jne 0x3a86
cmpb $0x1a, 0x6(%r14)
jne 0x3a86
cmpb $0xa, 0x7(%r14)
jne 0x3a86
addq %r14, %r15
movq %r14, %r11
addq $0x8, %r11
leaq 0xaa54(%rip), %rax      # 0xe2b0
movl $0x116, %ecx            # imm = 0x116
movl $0xaaaaaaab, %edx       # imm = 0xAAAAAAAB
leaq 0xc(%r11), %rsi
cmpq %r15, %rsi
jae 0x3a86
movl %r10d, %r8d
movbel (%r11), %r10d
leaq 0xc(%r11,%r10), %r9
cmpq %r15, %r9
jbe 0x3892
xorl %esi, %esi
movq %r11, %rdi
xorl %r10d, %r10d
jmp 0x391e
movbel 0x4(%r11), %ebp
cmpl $0x504c5444, %ebp       # imm = 0x504C5444
leaq 0x8(%r11), %rdi
movb $0x1, %sil
jg 0x38d6
cmpl $0x49444154, %ebp       # imm = 0x49444154
je 0x38f2
cmpl $0x49484452, %ebp       # imm = 0x49484452
jne 0x3918
cmpl $0xd, %r10d
jb 0x38d2
cmpb $0x0, 0x12(%r11)
jne 0x38d2
cmpb $0x0, 0x13(%r11)
jne 0x38d2
cmpb $0x0, 0x14(%r11)
je 0x392f
xorl %esi, %esi
jmp 0x388a
cmpl $0x504c5445, %ebp       # imm = 0x504C5445
je 0x3903
cmpl $0x74524e53, %ebp       # imm = 0x74524E53
jne 0x3918
movq %rdi, 0x38(%rsp)
movl %r10d, 0x40(%rsp)
jmp 0x3918
movq %rdi, 0x48(%rsp)
movl %r10d, 0x50(%rsp)
movb $0x1, %r10b
xorl %esi, %esi
jmp 0x391e
movl %r10d, %r10d
imulq %rdx, %r10
shrq $0x21, %r10
movq %rdi, 0x28(%rsp)
movl %r10d, 0x30(%rsp)
movq %r9, %rdi
movl %r8d, %r10d
testb %sil, %sil
movq %rdi, %r11
jne 0x3866
jmp 0x3a11
movl 0x8(%r11), %r12d
movl 0xc(%r11), %r10d
movbel %r12d, 0x10(%rsp)
movbel %r10d, 0x14(%rsp)
movzbl 0x11(%r11), %r13d
xorl %r10d, %r10d
movzbl 0x10(%r11), %r11d
cmpq $0x6, %r13
movl %r11d, 0x18(%rsp)
movl %r13d, 0x1c(%rsp)
ja 0x38ff
movslq (%rax,%r13,4), %r13
addq %rax, %r13
bswapl %r12d
jmpq *%r13
xorl %esi, %esi
cmpl $0x8, %r11d
ja 0x388a
btl %r11d, %ecx
movl $0x0, %r10d
jae 0x391e
imull %r11d, %r12d
leal 0x7(%r12), %esi
addl $0xe, %r12d
testl %esi, %esi
cmovnsl %esi, %r12d
sarl $0x3, %r12d
movl %r12d, 0x20(%rsp)
movl $0x1, 0x24(%rsp)
movb $0x1, %sil
jmp 0x3918
cmpl $0x8, %r11d
jne 0x38d2
leal (%r12,%r12,2), %edi
movl %edi, 0x20(%rsp)
movl $0x3, 0x24(%rsp)
jmp 0x3918
cmpl $0x8, %r11d
jne 0x38d2
addl %r12d, %r12d
movl %r12d, 0x20(%rsp)
movl $0x2, 0x24(%rsp)
jmp 0x3918
cmpl $0x8, %r11d
jne 0x38d2
shll $0x2, %r12d
movl %r12d, 0x20(%rsp)
movl $0x4, 0x24(%rsp)
jmp 0x3918
testb $0x1, %r10b
je 0x3a86
movl 0x10(%rsp), %r12d
leal (,%r12,4), %ebp
movl 0x14(%rsp), %r13d
movl %ebp, %eax
imull %r13d, %eax
movslq %eax, %rdi
addq $0x10, %rdi
callq 0x1150
movq %rax, %r15
leaq 0x10(%rax), %r8
movq %rax, %rdi
movl %r12d, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x53dc
testq %r15, %r15
je 0x3aa3
movq 0x8(%r15), %rsi
movl 0x4(%r15), %edx
movq %rsp, %rdi
callq 0x3ab0
testb %al, %al
je 0x3aa8
movq %r14, %rdi
callq 0x1040
movq %r15, %rax
leaq 0x58(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x12d8
movq %rbx, %rdi
callq 0x1321
callq 0x12fc
movq %rbx, %rdi
callq 0x126b
callq 0x12b3
movq %rbx, %rdi
callq 0x128f