// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/amigaicon.c
// Function: do_decode_newicons
static void do_decode_newicons(deark *c, lctx *d,
	dbuf *f, int newicons_num)
{
	u8 trns_code, width_code, height_code;
	u8 b0, b1;
	de_bitmap *img = NULL;
	int has_trns;
	i64 width, height;
	i64 ncolors;
	dbuf *decoded = NULL;
	i64 srcpos;
	i64 bitmap_start_pos = 0;
	i64 i;
	i64 rle_len;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "decoding NewIcons[%d], len=%"I64_FMT, newicons_num, f->len);
	de_dbg_indent(c, 1);

	d->newicons_largepalentry_flag = 0;
	trns_code = dbuf_getbyte(f, 0);
	has_trns = (trns_code=='B');
	width_code = dbuf_getbyte(f, 1);
	height_code = dbuf_getbyte(f, 2);

	b0 = dbuf_getbyte(f, 3);
	b1 = dbuf_getbyte(f, 4);
	if(width_code<0x21 || height_code<0x21 || b0<0x21 || b1<0x21) {
		de_err(c, "Bad NewIcons data");
		goto done;
	}
	ncolors = (i64)((((UI)b0-(UI)0x21)<<6) + ((UI)b1-(UI)0x21));
	if(ncolors<1 || ncolors>NEWICONS_MAX_PALENTRIES) {
		de_err(c, "Unsupported number of colors: %d", (int)ncolors);
		goto done;
	}

	width = (i64)width_code - 0x21;
	height = (i64)height_code - 0x21;

	de_dbg_dimensions(c, width, height);
	if(!de_good_image_dimensions(c, width, height)) goto done;
	de_dbg(c, "transparency: %d", has_trns);
	de_dbg(c, "colors: %d", (int)ncolors);

	decoded = dbuf_create_membuf(c, 2048, 0);

	d->newicons_bbll.is_lsb = 0;
	de_bitbuf_lowlevel_empty(&d->newicons_bbll);

	d->newicons_bits_per_pixel = (UI)de_log2_rounded_up(ncolors);

	// We decode both the palette and the bitmap into the same buffer, and
	// keep track of where in the buffer the bitmap starts.

	d->newicons_finished_pal = 0;

	for(srcpos=5; srcpos<f->len; srcpos++) {
		b0 = dbuf_getbyte(f, srcpos);
		if((b0>=0x20 && b0<=0x6f) || (b0>=0xa1 && b0<=0xd0)) {
			if(b0<=0x6f) b1 = b0-0x20;
			else b1 = 0x50 + (b0-0xa1);
			do_newicons_process_bits(c, d, decoded, b1, 7);
		}
		else if(b0>=0xd1) {
			// RLE compression for "0" bits
			rle_len = 7*(i64)(b0-0xd0);
			for(i=0; i<rle_len; i++) {
				do_newicons_process_bit(c, d, decoded, 0);
			}
		}
		else if(b0==0x00) {
			// End of a line.
			// Throw away any bits we've decoded that haven't been used yet.
			d->newicons_bbll.nbits_in_bitbuf = 0;

			// Presumably, the bitmap always starts at the beginning of a line.
			if(d->newicons_finished_pal==0 && decoded->len>=ncolors*3) {
				// The bitmap will start at this position. Remember that.
				bitmap_start_pos = decoded->len;
				d->newicons_finished_pal = 1;
			}
		}
	}

	de_dbg(c, "decompressed to %d bytes", (int)decoded->len);

	// The first ncolors*3 bytes are the palette
	de_dbg2(c, "NewIcons palette");
	de_dbg_indent(c, 1);
	de_zeromem(d->tmppal, sizeof(d->tmppal));
	for(i=0; i<ncolors; i++) {
		d->tmppal[i] = dbuf_getRGB(decoded, i*3, 0);

		// Educated guess: If the transparency flag is set, it means
		// palette entry 0 is transparent.
		if(i==0 && has_trns)
			d->tmppal[i] = DE_SET_ALPHA(d->tmppal[i], 0x00);

		de_dbg_pal_entry(c, i, d->tmppal[i]);
	}
	de_dbg_indent(c, -1);

	if(d->newicons_largepalentry_flag) {
		de_err(c, "Image has more than 256 colors; not supported");
		goto done;
	}

	img = de_bitmap_create(c, width, height, 4);
	de_convert_image_paletted(decoded, bitmap_start_pos,
		8, width, d->tmppal, img, 0);
	de_bitmap_write_to_file(img, c->filenames_from_file?"n":NULL,
		d->has_glowicons?DE_CREATEFLAG_IS_AUX:0);

done:
	dbuf_close(decoded);
	de_bitmap_destroy(img);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x7c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x17f1ef(%rip), %rsi    # 0x19d824
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movb $0x0, 0x55(%rax)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165060
movb %al, -0x1d(%rbp)
movzbl -0x1d(%rbp), %eax
cmpl $0x42, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x165060
movb %al, -0x1e(%rbp)
movq -0x18(%rbp), %rdi
movl $0x2, %esi
callq 0x165060
movb %al, -0x1f(%rbp)
movq -0x18(%rbp), %rdi
movl $0x3, %esi
callq 0x165060
movb %al, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movl $0x4, %esi
callq 0x165060
movb %al, -0x21(%rbp)
movzbl -0x1e(%rbp), %eax
cmpl $0x21, %eax
jl 0x1e6dc
movzbl -0x1f(%rbp), %eax
cmpl $0x21, %eax
jl 0x1e6dc
movzbl -0x20(%rbp), %eax
cmpl $0x21, %eax
jl 0x1e6dc
movzbl -0x21(%rbp), %eax
cmpl $0x21, %eax
jge 0x1e6f3
movq -0x8(%rbp), %rdi
leaq 0x17f15c(%rip), %rsi    # 0x19d843
movb $0x0, %al
callq 0x15bfb0
jmp 0x1ead3
movzbl -0x20(%rbp), %eax
subl $0x21, %eax
shll $0x6, %eax
movzbl -0x21(%rbp), %ecx
subl $0x21, %ecx
addl %ecx, %eax
movl %eax, %eax
movq %rax, -0x50(%rbp)
cmpq $0x1, -0x50(%rbp)
jl 0x1e71d
cmpq $0x200, -0x50(%rbp)     # imm = 0x200
jle 0x1e73a
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x17f127(%rip), %rsi    # 0x19d855
movb $0x0, %al
callq 0x15bfb0
jmp 0x1ead3
movzbl -0x1e(%rbp), %eax
subq $0x21, %rax
movq %rax, -0x40(%rbp)
movzbl -0x1f(%rbp), %eax
subq $0x21, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1e77e
jmp 0x1ead3
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %edx
leaq 0x17f193(%rip), %rsi    # 0x19d91f
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rax
movl %eax, %edx
leaq 0x180e2b(%rip), %rsi    # 0x19f5cf
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x168020
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movb $0x0, 0x58(%rax)
movq -0x10(%rbp), %rdi
addq $0x58, %rdi
callq 0x16a360
movq -0x50(%rbp), %rdi
callq 0x15cef0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x54(%rax)
movq $0x5, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x1e937
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x165060
movb %al, -0x20(%rbp)
movzbl -0x20(%rbp), %eax
cmpl $0x20, %eax
jl 0x1e82a
movzbl -0x20(%rbp), %eax
cmpl $0x6f, %eax
jle 0x1e840
movzbl -0x20(%rbp), %eax
cmpl $0xa1, %eax
jl 0x1e884
movzbl -0x20(%rbp), %eax
cmpl $0xd0, %eax
jg 0x1e884
movzbl -0x20(%rbp), %eax
cmpl $0x6f, %eax
jg 0x1e855
movzbl -0x20(%rbp), %eax
subl $0x20, %eax
movb %al, -0x21(%rbp)
jmp 0x1e864
movzbl -0x20(%rbp), %eax
subl $0xa1, %eax
addl $0x50, %eax
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl $0x7, %r8d
movzbl -0x21(%rbp), %ecx
callq 0x1f160
jmp 0x1e924
movzbl -0x20(%rbp), %eax
cmpl $0xd1, %eax
jl 0x1e8d7
movzbl -0x20(%rbp), %eax
subl $0xd0, %eax
cltq
imulq $0x7, %rax, %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
jge 0x1e8d5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1f1d0
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1e8aa
jmp 0x1e922
movzbl -0x20(%rbp), %eax
cmpl $0x0, %eax
jne 0x1e920
movq -0x10(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x10(%rbp), %rax
movzbl 0x54(%rax), %eax
cmpl $0x0, %eax
jne 0x1e91e
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
imulq $0x3, -0x50(%rbp), %rcx
cmpq %rcx, %rax
jl 0x1e91e
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x54(%rax)
jmp 0x1e920
jmp 0x1e922
jmp 0x1e924
jmp 0x1e926
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x1e7f6
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0x17ef2a(%rip), %rsi    # 0x19d876
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
leaq 0x17ef31(%rip), %rsi    # 0x19d88f
movb $0x0, %al
callq 0x15b350
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
addq $0x488, %rdi            # imm = 0x488
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x19140
movq $0x0, -0x70(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x50(%rbp), %rax
jge 0x1ea22
movq -0x58(%rbp), %rdi
imulq $0x3, -0x70(%rbp), %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, 0x488(%rax,%rcx,4)
cmpq $0x0, -0x70(%rbp)
jne 0x1e9f5
cmpl $0x0, -0x34(%rbp)
je 0x1e9f5
movq -0x10(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0x488(%rax,%rcx,4), %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
orl $0x0, %edx
movq -0x10(%rbp), %rax
movq -0x70(%rbp), %rcx
movl %edx, 0x488(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x10(%rbp), %rax
movq -0x70(%rbp), %rcx
movl 0x488(%rax,%rcx,4), %edx
callq 0x15bf00
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
jmp 0x1e992
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
cmpb $0x0, 0x55(%rax)
je 0x1ea51
movq -0x8(%rbp), %rdi
leaq 0x17ee5b(%rip), %rsi    # 0x19d8a0
movb $0x0, %al
callq 0x15bfb0
jmp 0x1ead3
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movl $0x4, %ecx
callq 0x16b750
movq %rax, -0x30(%rbp)
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x40(%rbp), %rcx
movq -0x10(%rbp), %r8
addq $0x488, %r8             # imm = 0x488
movq -0x30(%rbp), %r9
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %ecx
xorl %eax, %eax
movl %eax, %esi
leaq 0x18c5c2(%rip), %rax    # 0x1ab075
cmpl $0x0, %ecx
cmovneq %rax, %rsi
movq -0x10(%rbp), %rax
movl 0x68(%rax), %ecx
xorl %edx, %edx
movl $0x1, %eax
cmpl $0x0, %ecx
cmovnel %eax, %edx
callq 0x16afd0
movq -0x58(%rbp), %rdi
callq 0x1676a0
movq -0x30(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movl -0x7c(%rbp), %esi
callq 0x15b5a0
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)