// File: /workspace/github_cpp_makefile_star2_O2/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 0x28611(%rip), %rbx     # 0x41010
movl $0x252e1, %ebp          # imm = 0x252E1
addq 0x285cd(%rip), %rbp     # 0x40fd8
leaq 0x10(%rsp), %rdi
movq %rbp, (%rdi)
movq $0xbce7, 0x8(%rdi)      # imm = 0xBCE7
leaq 0x1075e(%rip), %rsi     # 0x29180
movl $0x1a36, %edx           # imm = 0x1A36
callq 0x7886
movl $0x30fc8, %eax          # imm = 0x30FC8
addq 0x285a0(%rip), %rax     # 0x40fd8
movq %rax, 0x8(%rsp)
cmpq 0x8(%rsp), %rbp
je 0x18bbe
leaq 0x1(%rbp), %rdx
movb (%rbp), %r13b
testb %r13b, %r13b
js 0x18a6b
xorl %r14d, %r14d
cmpl $0x8, 0x8(%rbx)
jne 0x18aca
movl 0x1(%rbp), %r14d
addq $0x5, %rbp
movq %rbp, %rdx
jmp 0x18aca
cmpb $-0x71, %r13b
jne 0x18a98
leaq 0x20(%rsp), %rax
xorl %ecx, %ecx
cmpq $0x1010100, %rcx        # imm = 0x1010100
je 0x18ac3
movl %ecx, %esi
orl $0xff000000, %esi       # imm = 0xFF000000
movl %esi, (%rax)
addq $0x10101, %rcx          # imm = 0x10101
addq $0x4, %rax
jmp 0x18a78
movl %r13d, %eax
shrb $0x3, %al
movzbl %al, %eax
andl $0xf, %eax
leaq 0x4(,%rax,4), %rcx
xorl %eax, %eax
cmpq %rax, %rcx
je 0x18ac0
movl (%rdx,%rax), %esi
movl %esi, 0x20(%rsp,%rax)
addq $0x4, %rax
jmp 0x18aae
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 0x997c
addq $0x4, %r12
cmpb $-0x7d, %r13b
je 0x18b75
movzbl %r13b, %eax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
cmpl $0x17, %eax
je 0x18b47
cmpl $0x1f, %eax
je 0x18b5e
cmpl $0x80, %eax
je 0x18b4e
cmpl $0xf, %eax
jne 0x18b65
callq 0xa08a
jmp 0x18b8c
callq 0x9fe8
jmp 0x18b8c
leaq 0x20(%rsp), %rcx
pushq $-0x1
popq %r8
callq 0x9d78
jmp 0x18b8c
callq 0xa0da
jmp 0x18b8c
leaq 0x20(%rsp), %rcx
pushq $-0x1
popq %r8
callq 0xa036
jmp 0x18b8c
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
leaq 0x20(%rsp), %rcx
pushq $-0x1
popq %r8
callq 0x9f06
testl %r14d, %r14d
je 0x18bb5
movq 0x10(%rbx), %rax
movl 0x4(%rbx), %ecx
imull (%rbx), %ecx
movl %r14d, %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
subq $0x1, %rcx
jb 0x18bb5
cmpl %r14d, (%rax,%rcx,4)
jne 0x18ba4
movl %edx, (%rax,%rcx,4)
jmp 0x18ba4
addq $0x18, %rbx
jmp 0x18a3d
leaq 0x59843(%rip), %rdi     # 0x72408
leaq 0x286cc(%rip), %rsi     # 0x41298
callq 0x18c34
movb $0x2, 0x59cb8(%rip)     # 0x72890
movb $0x9, 0x59ca9(%rip)     # 0x72888
addb $0x2, 0x59c42(%rip)     # 0x72828
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq