// File: /workspace/github_c_makefile_star2_O3/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xb602b(%rip), %rsi     # 0x12337f
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0x8, %esi
callq 0xeef43
movq %rax, %r15
leaq 0xb85c8(%rip), %rsi     # 0x12594d
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xa, %esi
callq 0xeef43
movq %rax, %r13
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xc, %esi
callq 0xeef43
movq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xe, %esi
callq 0xeef43
movq %rax, %r12
leaq 0xb857d(%rip), %rsi     # 0x125960
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x4, %r15d
jne 0x6d440
cmpq $0x20, %r13
jne 0x6d440
cmpq $0x20, %rbp
jne 0x6d440
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
cmpq $0x10, %rax
jl 0x6d440
addq $-0x10, %rax
movabsq $-0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF01
mulq %rcx
rorq %rax
movabsq $0x7f807f807f807f, %rcx # imm = 0x7F807F807F807F
cmpq %rcx, %rax
jbe 0x6d453
movb $0x1, 0x1(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r15
shrq $0x9, %r15
cmpq %r12, %r15
jge 0x6d478
leaq 0xb8509(%rip), %rsi     # 0x12596f
movq %r14, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8faa
jmp 0x6d47b
movq %r12, %r15
testq %r15, %r15
jle 0x6d444
leaq 0x10(%rbx), %r12
movl $0x12, %r13d
leaq -0x2(%r13), %rbp
movq %r14, %rdi
leaq 0xb841d(%rip), %rsi     # 0x1258b5
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r14, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movq %rbp, %rsi
callq 0xeef43
movq %r14, %rdi
leaq 0xad267(%rip), %rsi     # 0x11a72f
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x20, %esi
movl $0x20, %edx
movq %r14, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, %rbp
movq 0x18(%r14), %rdi
movl $0x1, (%rsp)
movl $0x4, %edx
movl $0x10, %ecx
movq %r13, %rsi
movq %r12, %r8
movq %rax, %r9
callq 0xf2f79
movq %rbp, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf22dc
movq %rbp, %rdi
callq 0xf228d
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x202, %r13            # imm = 0x202
decq %r15
jne 0x6d48a
jmp 0x6d444
nop