// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: do_fmtownsicn_icon
static void do_fmtownsicn_icon(deark *c, struct fmtownsicn_ctx *d,
	struct fmtownsicn_table_ctx *tbl, i64 idx, i64 pos1)
{
	int saved_indent_level;
	i64 pos = pos1;
	de_bitmap *img = NULL;
	UI createflags = 0;
	i64 w, h;
	i64 rowspan;
	i64 isize;
	i64 fg_pos;
	UI ncolors;
	UI bits_per_pixel;

	de_dbg_indent_save(c, &saved_indent_level);
	if(pos1>=c->infile->len) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}
	de_dbg(c, "icon header (table #%u icon #%u) at %"I64_FMT, (UI)tbl->idx,
		(UI)idx, pos1);
	de_dbg_indent(c, 1);
	pos += 2;
	pos += 4;
	w = de_getu16le_p(&pos);
	h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, w, h);
	ncolors = (UI)de_getu32le_p(&pos);
	de_dbg(c, "num colors: %u", ncolors);
	isize = de_getu16le_p(&pos);
	de_dbg(c, "size: %"I64_FMT, isize);
	pos += 4;
	fg_pos = de_getu32le_p(&pos);
	de_dbg(c, "fg pos: %"I64_FMT, fg_pos);

	if(fg_pos <= pos1 || fg_pos + isize > c->infile->len) {
		d->fatalerrflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	if(ncolors==2) {
		bits_per_pixel = 1;
	}
	else if(ncolors==16) {
		bits_per_pixel = 4;
	}
	else {
		de_err(c, "Unsupported image type");
		goto done;
	}

	if(!de_good_image_dimensions(c, w, h)) goto done;
	img = de_bitmap_create(c, w, h, 3);


	if(bits_per_pixel==1) {
		rowspan = de_pad_to_n(w*bits_per_pixel, 8) / 8;
		de_convert_image_paletted(c->infile, fg_pos, 1, rowspan,
			d->pal2, img, 0);
		createflags |= DE_CREATEFLAG_IS_BWIMG;
	}
	else { // 4
		rowspan = de_pad_to_n(w*bits_per_pixel, 32) / 8;
		de_convert_image_paletted(c->infile, fg_pos, 4, rowspan,
			d->pal16, img, 0x01);
		createflags |= DE_CREATEFLAG_OPT_IMAGE;
	}

	de_bitmap_write_to_file(img, NULL, createflags);

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x15b580
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x9cff9
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x9d276
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %r8
leaq 0x113d63(%rip), %rsi    # 0x1b0d7a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rdi
movl -0x74(%rbp), %edx
leaq 0x113d01(%rip), %rsi    # 0x1b0da2
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0x1282a2(%rip), %rsi    # 0x1c536e
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165580
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rdx
leaq 0x113cae(%rip), %rsi    # 0x1b0db1
movb $0x0, %al
callq 0x15b0d0
movq -0x70(%rbp), %rax
cmpq -0x28(%rbp), %rax
jle 0x9d12a
movq -0x70(%rbp), %rax
addq -0x68(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x9d13e
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x9d276
cmpl $0x2, -0x74(%rbp)
jne 0x9d14d
movl $0x1, -0x78(%rbp)
jmp 0x9d175
cmpl $0x10, -0x74(%rbp)
jne 0x9d15c
movl $0x4, -0x78(%rbp)
jmp 0x9d173
movq -0x8(%rbp), %rdi
leaq 0x106359(%rip), %rsi    # 0x1a34c0
movb $0x0, %al
callq 0x15bfb0
jmp 0x9d276
jmp 0x9d175
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x9d190
jmp 0x9d276
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x40(%rbp)
cmpl $0x1, -0x78(%rbp)
jne 0x9d20d
movq -0x50(%rbp), %rdi
movl -0x78(%rbp), %eax
imulq %rax, %rdi
movl $0x8, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %r8
addq $0x50, %r8
movq -0x40(%rbp), %r9
movl $0x1, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movl -0x44(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x44(%rbp)
jmp 0x9d266
movq -0x50(%rbp), %rdi
movl -0x78(%rbp), %eax
imulq %rax, %rdi
movl $0x20, %esi
callq 0x15ce90
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x70(%rbp), %rsi
movq -0x60(%rbp), %rcx
movq -0x10(%rbp), %r8
addq $0x10, %r8
movq -0x40(%rbp), %r9
movl $0x4, %edx
movl $0x1, (%rsp)
callq 0x16c520
movl -0x44(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x44(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %edx
xorl %eax, %eax
movl %eax, %esi
callq 0x16afd0
movq -0x40(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x15b5a0
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)