// File: /workspace/github_cpp_makefile_star2_O1/Alcaro[P]bridges/obj/resources.cpp
// Function: resources::init_inner_core()
static void decomp_images(image * imgs, uint8_t * inflate_buf, uint8_t * inflate_buf_end, const uint8_t * deflated, size_t deflated_size)
{
	__asm__("" : "+r"(imgs)); // iterating a pointer to a single object is undefined behavior, force gcc to forget it's not an array
	uint32_t palette[(IMG_8F || IMG_87) ? 256 : 16];
	inflator::inflate(bytesw(inflate_buf, inflate_buf_end-inflate_buf), bytesr(deflated, deflated_size));
	while (inflate_buf != inflate_buf_end)
	{
		uint8_t head = *inflate_buf++;
		
		uint32_t transp = 0;
		
		// these IMG_ constants allow dead code elimination of unused image formats
		// they're also why this function is in a Python string rather than a normal cpp - can't DCE across files
		if (head&0x80)
		{
			if (IMG_8F && head == 0x8F) // grayscale
			{
				for (int i=0;i<256;i++) palette[i] = 0xFF000000 + i*0x010101;
			}
			else if (IMG_80 || IMG_81 || IMG_83 || IMG_87)
			{
				int len;
				if (IMG_87 && head == 0x87) len = *inflate_buf++;
				else if (IMG_80 || IMG_81 || IMG_83) len = (head&0x7F)>>3;
				else __builtin_unreachable();
				
				for (int i=0;i<=len;i++)
				{
					palette[i] = readu_le32(inflate_buf);
					inflate_buf += 4;
				}
			}
			else __builtin_unreachable();
			head &= 0x87; // mask out palette length
		}
		else if (IMG_BARGB && imgs->fmt == ifmt_bargb8888)
		{
			transp = readu_le32(inflate_buf);
			inflate_buf += 4;
		}
		else if (IMG_NOT_BARGB) {}
		else __builtin_unreachable();
		
		size_t line_size_raw = (((head&0x1F)+1) * imgs->width + 7) >> 3;
		size_t filtered_size = (line_size_raw+1) * imgs->height;
		size_t defiltered_line_size = line_size_raw+4;
		size_t defiltered_size = defiltered_line_size * imgs->height;
		
		uint8_t * defilter_start = inflate_buf + filtered_size - defiltered_size;
		
		image::png_defilter(defilter_start, defiltered_line_size, inflate_buf, ((head&0x1F)+7)>>3, line_size_raw, imgs->height);
		defilter_start += 4;
		
		if(0);
		else if (IMG_0F && head == 0x0F) imgs->png_unpack_rgb<4>(defilter_start, defiltered_line_size);
		else if (IMG_17 && head == 0x17) imgs->png_unpack_rgb<2>(defilter_start, defiltered_line_size);
		else if (IMG_1F && head == 0x1F) imgs->png_unpack_rgb<6>(defilter_start, defiltered_line_size);
		else if (IMG_80 && head == 0x80) imgs->png_unpack_plte<1>(defilter_start, defiltered_line_size, palette, -1);
		else if (IMG_81 && head == 0x81) imgs->png_unpack_plte<2>(defilter_start, defiltered_line_size, palette, -1);
		else if (IMG_83 && head == 0x83) imgs->png_unpack_plte<4>(defilter_start, defiltered_line_size, palette, -1);
		else if ((IMG_87 || IMG_8F) && head == 0x87) imgs->png_unpack_plte<8>(defilter_start, defiltered_line_size, palette, -1);
		else __builtin_unreachable();
		
		if (transp != 0)
		{
			uint32_t * px = imgs->pixels32;
			size_t n = imgs->width * imgs->height;
			while (n--)
			{
				if (px[n] == transp) px[n] &= 0x00FFFFFF;
			}
		}
		
		inflate_buf += filtered_size;
		imgs++;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
leaq 0x28299(%rip), %rbx     # 0x46010
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x252e1, %ebp          # imm = 0x252E1
addq 0x28252(%rip), %rbp     # 0x45fe0
movq %rbp, (%rdi)
movq $0xbce7, 0x8(%rdi)      # imm = 0xBCE7
leaq 0x12450(%rip), %rsi     # 0x301f0
movl $0x1a36, %edx           # imm = 0x1A36
callq 0x8e50
movl $0x30fc8, %eax          # imm = 0x30FC8
addq 0x2822a(%rip), %rax     # 0x45fe0
movq %rax, 0x8(%rsp)
leaq 0x1(%rbp), %rdx
movb (%rbp), %r13b
testb %r13b, %r13b
js 0x1ddde
xorl %r14d, %r14d
cmpl $0x8, 0x8(%rbx)
jne 0x1de32
movl 0x1(%rbp), %r14d
addq $0x5, %rbp
movq %rbp, %rdx
jmp 0x1de32
cmpb $-0x71, %r13b
jne 0x1de02
movl $0xff000000, %eax       # imm = 0xFF000000
xorl %ecx, %ecx
movl %eax, 0x20(%rsp,%rcx,4)
incq %rcx
addl $0x10101, %eax          # imm = 0x10101
cmpq $0x100, %rcx            # imm = 0x100
jne 0x1ddeb
jmp 0x1de2b
movl %r13d, %eax
shrb $0x3, %al
movzbl %al, %eax
andl $0xf, %eax
leaq 0x4(,%rax,4), %rcx
xorl %eax, %eax
movl (%rdx,%rax), %esi
movl %esi, 0x20(%rsp,%rax)
addq $0x4, %rax
cmpq %rax, %rcx
jne 0x1de18
addq %rax, %rdx
andb $-0x79, %r13b
xorl %r14d, %r14d
movl %r13d, %eax
andb $0x1f, %al
movzbl %al, %ecx
leal 0x1(%rcx), %r8d
imull (%rbx), %r8d
addl $0x7, %r8d
shrl $0x3, %r8d
leaq 0x1(%r8), %rbp
movl 0x4(%rbx), %r9d
imulq %r9, %rbp
leaq 0x4(%r8), %r15
addq %rdx, %rbp
movq %r15, %rax
imulq %r9, %rax
movq %rbp, %r12
subq %rax, %r12
addl $0x7, %ecx
shrl $0x3, %ecx
movq %r12, %rdi
movq %r15, %rsi
callq 0xb41e
addq $0x4, %r12
movzbl %r13b, %eax
cmpl $0x1e, %eax
jle 0x1deb6
cmpl $0x1f, %eax
je 0x1ded0
cmpl $0x80, %eax
je 0x1dee0
cmpl $0x83, %eax
jne 0x1df0b
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x39d8
jmp 0x1df24
cmpl $0xf, %eax
je 0x1defb
cmpl $0x17, %eax
jne 0x1df0b
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xb93a
jmp 0x1df24
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xb9de
jmp 0x1df24
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0xb80e
jmp 0x1df24
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xb988
jmp 0x1df24
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x3a7e
testl %r14d, %r14d
je 0x1df51
movl 0x4(%rbx), %ecx
imull (%rbx), %ecx
testl %ecx, %ecx
je 0x1df51
movq 0x10(%rbx), %rax
movl %ecx, %ecx
movl -0x4(%rax,%rcx,4), %edx
cmpl %r14d, %edx
jne 0x1df4c
andl $0xffffff, %edx         # imm = 0xFFFFFF
movl %edx, -0x4(%rax,%rcx,4)
decq %rcx
jne 0x1df39
addq $0x18, %rbx
cmpq 0x8(%rsp), %rbp
jne 0x1ddbb
leaq 0x594a1(%rip), %rdi     # 0x77408
leaq 0x2832a(%rip), %rsi     # 0x46298
callq 0x1dfc4
movb $0x2, 0x59916(%rip)     # 0x77890
movb $0x9, 0x59907(%rip)     # 0x77888
addb $0x2, 0x598a0(%rip)     # 0x77828
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq