// File: /workspace/github_c_makefile_star2_O1/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 0xb5c1a(%rip), %rsi     # 0x12319f
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0x8, %esi
callq 0xef570
movq %rax, %r15
leaq 0xb8167(%rip), %rsi     # 0x12571d
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xa, %esi
callq 0xef570
movq %rax, %r13
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xc, %esi
callq 0xef570
movq %rax, %rbp
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movl $0xe, %esi
callq 0xef570
movq %rax, %r12
leaq 0xb811c(%rip), %rsi     # 0x125730
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x4, %r15d
jne 0x6d671
cmpq $0x20, %r13
jne 0x6d671
cmpq $0x20, %rbp
jne 0x6d671
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
cmpq $0x10, %rax
jl 0x6d671
addq $-0x10, %rax
movabsq $-0xff00ff00ff00ff, %rcx # imm = 0xFF00FF00FF00FF01
mulq %rcx
rorq %rax
movabsq $0x7f807f807f807f, %rcx # imm = 0x7F807F807F807F
cmpq %rcx, %rax
jbe 0x6d684
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 0x6d6a9
leaq 0xb80a8(%rip), %rsi     # 0x12573f
movq %r14, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9b27
jmp 0x6d6ac
movq %r12, %r15
testq %r15, %r15
jle 0x6d675
leaq 0x10(%rbx), %r12
movl $0x12, %r13d
leaq -0x2(%r13), %rbp
movq %r14, %rdi
leaq 0xb7fbc(%rip), %rsi     # 0x125685
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r14, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r14), %rdi
movl 0x4(%rbx), %edx
movq %rbp, %rsi
callq 0xef570
movq %r14, %rdi
leaq 0xacf16(%rip), %rsi     # 0x11a60f
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x20, %esi
movl $0x20, %edx
movq %r14, %rdi
movl $0x3, %ecx
callq 0xf2cab
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 0xf35b8
movq %rbp, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0xf2923
movq %rbp, %rdi
callq 0xf28d4
movq %r14, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x202, %r13            # imm = 0x202
decq %r15
jne 0x6d6bb
jmp 0x6d675