// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: do_fmtownsicn_FJ
static void do_fmtownsicn_FJ(deark *c, struct fmtownsicn_ctx *d)
{
	i64 num_icons;
	i64 num_icons_capacity;
	i64 w, h;
	i64 rowspan;
	i64 icon_size;
	UI bits_per_pixel;
	i64 i;
	i64 pos1 = 0;
	i64 pos = pos1;

	de_dbg(c, "header at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);
	pos += 8;

	bits_per_pixel = (UI)dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg(c, "bits per pixel: %u", bits_per_pixel);

	w = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	h = dbuf_getu16x(c->infile, pos, d->is_le);
	pos += 2;
	de_dbg_dimensions(c, w, h);

	num_icons = dbuf_getu16x(c->infile, pos, d->is_le);
	//pos += 2;
	de_dbg(c, "num icons: %"I64_FMT, num_icons);

	de_dbg_indent(c, -1);

	if(w!=32 || h!=32 || bits_per_pixel!=4) {
		d->need_errmsg = 1;
		goto done;
	}
	rowspan = 16;
	icon_size = 2+rowspan*h;

	if(c->infile->len<16 ||
		((c->infile->len-16) % icon_size)!=0)
	{
		d->need_errmsg = 1;
		goto done;
	}

	num_icons_capacity = (c->infile->len-16) / icon_size;
	if(num_icons_capacity < num_icons) {
		de_warn(c, "Expected %"I64_FMT" icons, only found %"I64_FMT,
			num_icons, num_icons_capacity);
		num_icons = num_icons_capacity;
	}

	for(i=0; i<num_icons; i++) {
		de_bitmap *img;
		UI id;

		pos = 16+i*icon_size;
		de_dbg(c, "icon at %"I64_FMT, pos);
		de_dbg_indent(c, 1);
		id = (UI)dbuf_getu16x(c->infile, pos, d->is_le);
		de_dbg(c, "id: 0x%04x", id);

		img = de_bitmap_create(c, w, h, 3);
		de_convert_image_paletted(c->infile, pos+2, 4, rowspan,
			d->pal16, img, 0x01);
		de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);
		de_bitmap_destroy(img);
		de_dbg_indent(c, -1);
	}
done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xacb14(%rip), %rsi     # 0x1161bf
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xdeba7
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
pushq $0x8
popq %rsi
callq 0xe41b1
movq %rax, %r12
leaq 0xaf075(%rip), %rsi     # 0x11874d
movq %r14, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
pushq $0xa
popq %rsi
callq 0xe41b1
movq %rax, %r13
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
pushq $0xc
popq %rsi
callq 0xe41b1
movq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xdf038
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
pushq $0xe
popq %rsi
callq 0xe41b1
movq %rax, %r15
leaq 0xaf030(%rip), %rsi     # 0x118760
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %r14, %rdi
callq 0xdeba7
cmpl $0x4, %r12d
jne 0x6977b
cmpq $0x20, %r13
jne 0x6977b
cmpq $0x20, %rbp
jne 0x6977b
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
cmpq $0x10, %rax
jl 0x6977b
addq $-0x10, %rax
movl $0x202, %ecx            # imm = 0x202
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
je 0x6978e
movb $0x1, 0x1(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
cmpq %r15, %rax
jge 0x697b0
leaq 0xaefd2(%rip), %rsi     # 0x11876f
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0xdf05a
movq %r12, %r15
leaq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testq %r15, %r15
cmovleq %rax, %r15
pushq $0x12
popq %r13
pushq $0x20
popq %rbp
subq $0x1, %r15
jb 0x6977f
leaq -0x2(%r13), %r12
movq %r14, %rdi
leaq 0xaeed8(%rip), %rsi     # 0x1186b5
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movq %r12, %rsi
callq 0xe41b1
movq %r14, %rdi
leaq 0xa3e34(%rip), %rsi     # 0x10d63f
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %r14, %rdi
movq %rbp, %rsi
movq %rbp, %rdx
pushq $0x3
popq %rcx
callq 0xe7437
movq %rax, %r12
movq 0x18(%r14), %rdi
movl $0x1, (%rsp)
movq %r13, %rsi
pushq $0x4
popq %rdx
pushq $0x10
popq %rcx
movq 0x10(%rsp), %r8
movq %rax, %r9
callq 0xe7cbc
movq %r12, %rdi
xorl %esi, %esi
pushq $0x2
popq %rdx
callq 0xe70b7
movq %r12, %rdi
callq 0xe7084
movq %r14, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
addq $0x202, %r13            # imm = 0x202
jmp 0x697c9