// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/icns.c
// Function: do_icon
static void do_icon(deark *c, lctx *d, struct page_ctx *pg)
{
	i64 expected_image_size;
	int is_compressed;

	if(!pg->type_info) return; // Shouldn't happen.

	de_strlcpy(pg->filename_token, "", sizeof(pg->filename_token));

	if(pg->type_info->image_type==IMGTYPE_MASK) {
		de_dbg(c, "[transparency mask]");
		return;
	}

	if(pg->type_info->image_type==IMGTYPE_EMBEDDED_FILE ||
		pg->type_info->image_type==IMGTYPE_ARGB_ETC)
	{
		do_argb_png_or_jp2(c, d, pg);
		return;
	}

	if(pg->type_info->image_type!=IMGTYPE_IMAGE &&
		pg->type_info->image_type!=IMGTYPE_IMAGE_AND_MASK)
	{
		return;
	}

	// At this point we know it's a regular image (or an image+mask)

	// Note - This pg->rowspan is arguably incorrect for 24-bit images, since
	// rows aren't stored contiguously.
	pg->rowspan = ((pg->type_info->bpp * pg->type_info->width)+7)/8;

	expected_image_size = pg->rowspan * pg->type_info->height;
	if(pg->type_info->image_type==IMGTYPE_IMAGE_AND_MASK) {
		expected_image_size *= 2;
	}

	is_compressed = (pg->type_info->bpp==24) ? 1 : 0;

	if(!is_compressed) {
		if(pg->image_len < expected_image_size) {
			de_err(c, "(Image #%d) Premature end of image (expected %d bytes, found %d)",
				pg->image_num, (int)expected_image_size, (int)pg->image_len);
			return;
		}
		if(pg->image_len > expected_image_size) {
			de_warn(c, "(Image #%d) Extra image data found (expected %d bytes, found %d)",
				pg->image_num, (int)expected_image_size, (int)pg->image_len);
		}
	}

	pg->mask_ref = find_mask(c, d, pg);

	de_snprintf(pg->filename_token, sizeof(pg->filename_token), "%dx%dx%d",
		(int)pg->type_info->width, (int)pg->type_info->height, (int)pg->type_info->bpp);

	de_dbg(c, "image dimensions: %d"DE_CHAR_TIMES"%d, bpp: %d",
		pg->type_info->width, pg->type_info->height, pg->type_info->bpp);

	if(pg->type_info->bpp==1 || pg->type_info->bpp==4 || pg->type_info->bpp==8) {
		do_decode_1_4_8bit(c, d, pg);
		return;
	}
	else if(pg->type_info->bpp==24) {
		do_decode_24bit(c, d, pg);
		return;
	}

	de_warn(c, "(Image #%d) Image type '%s' is not supported", pg->image_num, pg->code4cc.id_sanitized_sz);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x30(%rax)
jne 0xa7d34
jmp 0xa7fcf
movq -0x18(%rbp), %rdi
addq $0x6c, %rdi
leaq 0x128e8d(%rip), %rsi    # 0x1d0bd0
movl $0x20, %edx
callq 0x15aa30
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x2, 0x10(%rax)
jne 0xa7d72
movq -0x8(%rbp), %rdi
leaq 0x10ad26(%rip), %rsi    # 0x1b2a8c
movb $0x0, %al
callq 0x15b0d0
jmp 0xa7fcf
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x1, 0x10(%rax)
je 0xa7d8e
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x5, 0x10(%rax)
jne 0xa7da4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xa8080
jmp 0xa7fcf
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x3, 0x10(%rax)
je 0xa7dc5
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x4, 0x10(%rax)
je 0xa7dc5
jmp 0xa7fcf
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0xc(%rax), %eax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
imull 0x4(%rcx), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
movslq %eax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq 0x8(%rcx), %rcx
imulq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x4, 0x10(%rax)
jne 0xa7e27
movq -0x20(%rbp), %rax
shlq %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0xc(%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x18, %edx
cmovel %ecx, %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0xa7ebd
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
jge 0xa7e84
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %r8d
leaq 0x10ac28(%rip), %rsi    # 0x1b2aa0
movb $0x0, %al
callq 0x15bfb0
jmp 0xa7fcf
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x20(%rbp), %rax
jle 0xa7ebb
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %r8d
leaq 0x10ac2d(%rip), %rsi    # 0x1b2ae1
movb $0x0, %al
callq 0x15bc10
jmp 0xa7ebd
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xa8140
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rdi
addq $0x6c, %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x4(%rax), %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x8(%rax), %r8d
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0xc(%rax), %r9d
movl $0x20, %esi
leaq 0x10ac12(%rip), %rdx    # 0x1b2b22
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x4(%rax), %edx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x8(%rax), %ecx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
movl 0xc(%rax), %r8d
leaq 0x10abe7(%rip), %rsi    # 0x1b2b2b
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x1, 0xc(%rax)
je 0xa7f75
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x4, 0xc(%rax)
je 0xa7f75
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x8, 0xc(%rax)
jne 0xa7f88
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xa83f0
jmp 0xa7fcf
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rax
cmpl $0x18, 0xc(%rax)
jne 0xa7fa9
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xa8570
jmp 0xa7fcf
jmp 0xa7fab
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
movq -0x18(%rbp), %rcx
addq $0x38, %rcx
addq $0x8, %rcx
leaq 0x10ab85(%rip), %rsi    # 0x1b2b4d
movb $0x0, %al
callq 0x15bc10
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)