// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/amigaicon.c
// Function: do_glowicons_IMAG
static void do_glowicons_IMAG(deark *c, lctx *d,
	i64 pos, i64 len)
{
	de_bitmap *img = NULL;
	u8 trns_color;
	i64 num_colors;
	u8 flags;
	int has_trns;
	int has_palette;
	u8 cmpr_type;
	u8 pal_cmpr_type = 0;
	i64 bits_per_pixel;
	i64 image_size_in_bytes;
	i64 pal_size_in_bytes;
	i64 image_pos;
	i64 pal_pos;
	i64 k;
	dbuf *tmpbuf = NULL;

	if(d->glowicons_width<1) {
		// We must not have found a FACE chunk yet.
		de_err(c, "Invalid GlowIcons data");
		goto done;
	}

	trns_color = de_getbyte(pos);
	de_dbg(c, "transparent color: 0x%02x", (int)trns_color);
	num_colors = 1+(i64)de_getbyte(pos+1);
	de_dbg(c, "number of colors: %d", (int)num_colors);
	flags = de_getbyte(pos+2);
	has_trns = (flags & 0x01)?1:0;
	has_palette = (flags & 0x02)?1:0;
	de_dbg(c, "has transparency: %d", has_trns);
	de_dbg(c, "has palette: %d", has_palette);

	cmpr_type = de_getbyte(pos+3);
	de_dbg(c, "compression type: %d", cmpr_type);
	if(cmpr_type!=0 && cmpr_type!=1) {
		de_err(c, "Unsupported compression type");
		goto done;
	}

	if(has_palette) {
		pal_cmpr_type = de_getbyte(pos+4);
		de_dbg(c, "palette compression type: %d", pal_cmpr_type);
		if(pal_cmpr_type!=0 && pal_cmpr_type!=1) {
			de_err(c, "Unsupported palette compression type");
			goto done;
		}
	}

	bits_per_pixel = (i64)de_getbyte(pos+5);
	de_dbg(c, "bits per pixel: %d", (int)bits_per_pixel);

	if(bits_per_pixel<1 || bits_per_pixel>8) {
		de_err(c, "Invalid or unsupported bits per pixel (%d)", (int)bits_per_pixel);
		goto done;
	}

	image_size_in_bytes = 1+de_getu16be(pos+6);
	pal_size_in_bytes = 1+de_getu16be(pos+8);
	pos+=10;

	tmpbuf = dbuf_create_membuf(c, 10240, 0);
	dbuf_set_length_limit(tmpbuf, 1048576);

	image_pos = pos;
	pal_pos = image_pos+image_size_in_bytes;
	de_dbg(c, "image data at %d, len=%d", (int)image_pos, (int)image_size_in_bytes);

	if(has_palette) {
		de_dbg(c, "palette data at %d, len=%d", (int)pal_pos, (int)pal_size_in_bytes);
		de_dbg_indent(c, 1);

		if(pal_cmpr_type==1) {
			glowdata_decompress(c->infile, pal_pos, pal_size_in_bytes, tmpbuf, 8);
		}
		else {
			dbuf_copy(c->infile, pal_pos, pal_size_in_bytes, tmpbuf);
		}

		for(k=0; k<256; k++) {
			if(k<num_colors) {
				d->glowicons_palette[k] = dbuf_getRGB(tmpbuf, k*3, 0);
				if(has_trns && k==(i64)trns_color) {
					d->glowicons_palette[k] = DE_SET_ALPHA(d->glowicons_palette[k], 0x00);
				}
				de_dbg_pal_entry(c, k, d->glowicons_palette[k]);
			}
			else {
				d->glowicons_palette[k] = DE_STOCKCOLOR_BLACK;
			}
		}

		de_dbg_indent(c, -1);
	}

	// Decompress the pixels
	dbuf_empty(tmpbuf);
	if(cmpr_type==0) {
		dbuf_copy(c->infile, image_pos, image_size_in_bytes, tmpbuf);
	}
	else {
		glowdata_decompress(c->infile, image_pos, image_size_in_bytes, tmpbuf, (UI)bits_per_pixel);
	}

	img = de_bitmap_create(c, d->glowicons_width, d->glowicons_height, has_trns?4:3);

	de_convert_image_paletted(tmpbuf, 0,
		8, d->glowicons_width, d->glowicons_palette, img, 0);

	de_bitmap_write_to_file(img, c->filenames_from_file?"g":NULL, 0);

done:
	if(tmpbuf) dbuf_close(tmpbuf);
	if(img) de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movb $0x0, -0x46(%rbp)
movq $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x1, 0x78(%rax)
jge 0x1f461
movq -0x8(%rbp), %rdi
leaq 0x17e495(%rip), %rsi    # 0x19d8ea
movb $0x0, %al
callq 0x15bfb0
jmp 0x1f904
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x29(%rbp), %edx
leaq 0x17e47d(%rip), %rsi    # 0x19d901
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x17e3a5(%rip), %rsi    # 0x19d861
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x2, %rsi
callq 0x165060
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %edx
andl $0x1, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x40(%rbp)
movzbl -0x39(%rbp), %edx
andl $0x2, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0x40(%rbp), %edx
leaq 0x17e404(%rip), %rsi    # 0x19d91b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %edx
leaq 0x17e404(%rip), %rsi    # 0x19d930
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x3, %rsi
callq 0x165060
movb %al, -0x45(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x45(%rbp), %edx
leaq 0x188a59(%rip), %rsi    # 0x1a7fb3
movb $0x0, %al
callq 0x15b0d0
movzbl -0x45(%rbp), %eax
cmpl $0x0, %eax
je 0x1f58a
movzbl -0x45(%rbp), %eax
cmpl $0x1, %eax
je 0x1f58a
movq -0x8(%rbp), %rdi
leaq 0x17e3c2(%rip), %rsi    # 0x19d940
movb $0x0, %al
callq 0x15bfb0
jmp 0x1f904
cmpl $0x0, -0x44(%rbp)
je 0x1f5e9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x4, %rsi
callq 0x165060
movb %al, -0x46(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x46(%rbp), %edx
leaq 0x17e3a6(%rip), %rsi    # 0x19d95d
movb $0x0, %al
callq 0x15b0d0
movzbl -0x46(%rbp), %eax
cmpl $0x0, %eax
je 0x1f5e7
movzbl -0x46(%rbp), %eax
cmpl $0x1, %eax
je 0x1f5e7
movq -0x8(%rbp), %rdi
leaq 0x17e39f(%rip), %rsi    # 0x19d97a
movb $0x0, %al
callq 0x15bfb0
jmp 0x1f904
jmp 0x1f5e9
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x5, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x17e389(%rip), %rsi    # 0x19d99f
movb $0x0, %al
callq 0x15b0d0
cmpq $0x1, -0x50(%rbp)
jl 0x1f62b
cmpq $0x8, -0x50(%rbp)
jle 0x1f648
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x17e376(%rip), %rsi    # 0x19d9b2
movb $0x0, %al
callq 0x15bfb0
jmp 0x1f904
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x6, %rsi
callq 0x1651d0
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
callq 0x1651d0
addq $0x1, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x2800, %esi           # imm = 0x2800
xorl %edx, %edx
callq 0x168020
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x168120
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rax
movl %eax, %edx
movq -0x58(%rbp), %rax
movl %eax, %ecx
leaq 0x17e302(%rip), %rsi    # 0x19d9dd
movb $0x0, %al
callq 0x15b0d0
cmpl $0x0, -0x44(%rbp)
je 0x1f828
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rax
movl %eax, %edx
movq -0x60(%rbp), %rax
movl %eax, %ecx
leaq 0x17e2f3(%rip), %rsi    # 0x19d9f6
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movzbl -0x46(%rbp), %eax
cmpl $0x1, %eax
jne 0x1f742
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x80(%rbp), %rcx
movl $0x8, %r8d
callq 0x1f930
jmp 0x1f75b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0x166070
movq $0x0, -0x78(%rbp)
cmpq $0x100, -0x78(%rbp)     # imm = 0x100
jge 0x1f81a
movq -0x78(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x1f7f4
movq -0x80(%rbp), %rdi
imulq $0x3, -0x78(%rbp), %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movl %edx, 0x88(%rax,%rcx,4)
cmpl $0x0, -0x40(%rbp)
je 0x1f7d6
movq -0x78(%rbp), %rax
movzbl -0x29(%rbp), %ecx
cmpq %rcx, %rax
jne 0x1f7d6
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x88(%rax,%rcx,4), %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
orl $0x0, %edx
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movl %edx, 0x88(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0x78(%rbp), %rsi
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movl 0x88(%rax,%rcx,4), %edx
callq 0x15bf00
jmp 0x1f807
movq -0x10(%rbp), %rax
movq -0x78(%rbp), %rcx
movl $0xff000000, 0x88(%rax,%rcx,4) # imm = 0xFF000000
jmp 0x1f809
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x1f763
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x80(%rbp), %rdi
callq 0x169310
movzbl -0x45(%rbp), %eax
cmpl $0x0, %eax
jne 0x1f855
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rcx
callq 0x166070
jmp 0x1f875
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x68(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x50(%rbp), %rax
movl %eax, %r8d
callq 0x1f930
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rdx
movl -0x40(%rbp), %r8d
movl $0x3, %ecx
movl $0x4, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, -0x28(%rbp)
movq -0x80(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x10(%rbp), %r8
addq $0x88, %r8
movq -0x28(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %ecx
xorl %eax, %eax
movl %eax, %esi
leaq 0x19f09c(%rip), %rax    # 0x1be992
cmpl $0x0, %ecx
cmovneq %rax, %rsi
xorl %edx, %edx
callq 0x16afd0
cmpq $0x0, -0x80(%rbp)
je 0x1f914
movq -0x80(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x28(%rbp)
je 0x1f924
movq -0x28(%rbp), %rdi
callq 0x16af60
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)